# Setting Up a Practical Ansible Lab on AWS: A Step-by-Step Guide

Ansible is a powerful, easy-to-use and easy-to-learn configuration management tool. If you are trying to learn Ansible then the first hurdle for you will be setting up a lab environment to practice Ansible.

In this article, we are going to create a lab environment on AWS. You can use any other cloud platform like Azure, or Google Cloud or you can create vm on Virtual Box. I would suggest using AWS or Azure (here we will use AWS) as they offer 12 months of free trial you can play with lots of different OSs also. not suggesting Google Cloud as they don't offer a Windows server on a free trial.

---

Now Let's get started, We are going to create 3 servers. All of them will be Ubuntu servers. We are going to use one server as the control node and the remaining two as target nodes.

we can use Windows servers as the target node but not as the control node. here for setting up the lab, we are going to use Ubuntu servers only, as using a Windows server as a target has some extra steps. you can learn how to set up a Windows server for Ansible management [here](https://anujdube.com/setting-up-a-windows-server-for-ansible-management).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696161048416/dcae42f5-1ca2-461a-a97e-efff942bead5.png align="center")

## 1\. Get an AWS Free Tier Account

To get this account you might need to provide them with a credit/debit card. No need to worry they will not charge but remember to put a billing alert and close all resources after using them so you don't get any charges.

## 2\. Create EC2 Instances

Search EC2 on the Search Bar and click on that

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696161821700/03da2a22-e4f1-4bd3-8a0c-10d006589023.png align="center")

you will see windows like the above. Click on Instances(running)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696161909893/467c3764-f5ee-416b-9fa7-f857764c4e88.png align="center")

now click on the Launch Instances Button at the right-top corner. and enter details below

**Name and tags:** Server1

**Application and OS Images:** Select Ubuntu do not change anything

**Instance type:** Keep Default Free Tier instance type

**Key pair:** Click on "Create new key pair" and create a new key it will be downloaded automatically after creation keep it safe

**Network settings:** allows SSH, HTTP, HTTPS

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696162757381/4d181c11-9fe4-405a-857f-525342864c40.png align="center")

Keep Every other thing Default.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696163080022/1110c16d-4ede-4654-a020-52a306132b92.png align="center")

on the right-hand side, you will see the summary. In "Number of instances" enter 3. then click on "Launch instance".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696163219947/78228416-3349-4e8f-8571-0e0a446c5da5.png align="center")

Now change their names as above

## 3\. Setting Up EC2 Instances

I am using Windows So I will be using MobaXtrem as an SSH Client. If you are on Mac or Linux you don't need to install anything SSH Client is pre-installed on those OS.

I have Stored My Key Pair named "newUbuntu.pem" which was generated while creating instances on location "C:\\temp\\aws".

First, we will go through the steps for Mac/Linux users and then for Windows.

### Steps For Mac/Linux Users

Go to the location where you have stored your key pair and open the terminal there

Now On the AWS console, Select "Client" Server and Click on the Connect button at the top. then Go to SSH Client and copy the example SSH command

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696164123614/ae662d32-d2de-4361-99c8-2ffe7a4b1d3e.png align="center")

Paste That command in the terminal and press enter. it will ask for yes/no enter yes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696164761874/56902403-889f-41e4-a5d0-eb1e0f2b084f.png align="center")

Now you are logged in to the control node. Now logout enter "exit"

We are going to copy our private SSH Key into the control node. for that use the below command. do not forget to update the command with your values

```bash
# scp -i path/to/your/private/key.pem path/to/your/local/file user@public-dns-name:/path/to/destination/directory
scp -i newUbuntu.pem newUbuntu.pem ubuntu@ec2-3-110-197-196.ap-south-1.compute.amazonaws.com:~/
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696170480911/66612e9a-5af8-436e-9621-62eaea767fd7.png align="center")

now again log into the server and you can see your private key is copied like below

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696169948814/2864ac44-8a32-4b9d-89ec-a929b5f4091c.png align="center")

Now Type the below command to ensure your key is not publicly viewable

```bash
chmod 400 newUbuntu.pem
```

now we will try to see if our control node can SSH into server1 and server2 or not.

Go to AWS Console select server1, click on the connect button, and copy the ssh command like below.

```bash
ssh -i "newUbuntu.pem" ubuntu@ec2-3-110-197-196.ap-south-1.compute.amazonaws.com
```

now paste the command into the terminal of the control node and press enter

it might ask for yes/no. press yes. now you are in server1.

now type "exit" to exit from server1 to the control node.

Copy the SSH command for server2 also and try SSH into it.

You will be able to SSH into server2 also.

now you can directly go to the "4. Installing Ansible" step.

### Steps For Windows Users

Download MobaXterm From [here](https://mobaxterm.mobatek.net/download.html) and Install it.

When MobaXterm is first started click on Settings -&gt; Configuration

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696164368994/1857f1a3-50fb-48cd-9691-10215e1891d3.png align="center")

Change Home and Root Directory to the location where the key is stored. in my case it is C:\\temp\\aws. Click on OK. It will ask for a restart Click on Yes. MobaXStream will restart.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696164480475/73984c7f-9a26-454c-8302-72440c66c34a.png align="center")

Now Click On Start Local Terminal

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696164632761/355d0a3b-aa5c-442e-ac89-2c72091f144c.png align="center")

When we run "ls" it should show your key in the current directory

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696164679407/8ef31c9a-b63d-45b3-b53d-94d59b2266a0.png align="center")

Now On the AWS console, Select "Client" Server and Click on the Connect button at the top. then Go to SSH Client and copy the example SSH command

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696164123614/ae662d32-d2de-4361-99c8-2ffe7a4b1d3e.png align="center")

Paste That command in the terminal and press enter. it will ask for yes/no enter yes.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696164761874/56902403-889f-41e4-a5d0-eb1e0f2b084f.png align="center")

Now you are logged in to the control node. On the left-hand side, you will see SSH Browser.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696170665032/1bf4b751-f393-4a7c-a2a1-09085fb5605a.png align="center")

Upload your private key

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696170715287/3341916d-eb9f-47a1-8eb8-6aa9e080c1fb.png align="center")

Now Type the below command to ensure your key is not publicly viewable

```bash
chmod 400 newUbuntu.pem
```

now we will try to see if our control node can SSH into server1 and server2 or not.

Go to AWS Console select server1, click on the connect button, and copy the ssh command like below.

```bash
ssh -i "newUbuntu.pem" ubuntu@ec2-3-110-197-196.ap-south-1.compute.amazonaws.com
```

now paste the command into the terminal of the control node and press enter

it might ask for yes/no. press yes. now you are in server1.

now type "exit" to exit from server1 to the control node.

Copy the SSH command for server2 also and try SSH into it.

You will be able to SSH into server2 also.

Windows Users can also use SCP to copy private SSH key to control the server. But Using MobaXterm is much easier.

As Learning Experience you can try SCP also.

## 4\. Installing Ansible

Update Package list and upgrade Packages

```bash
sudo apt update
sudo apt upgrade
```

Sometimes you may be asked to reboot then you can reboot using the below command.

```bash
sudo reboot
```

Install Ansible

```bash
sudo apt install ansible
```

Now Let's Create a Folder Called "ansiblePractice" and go into that folder

```bash
mkdir ansiblePractice
cd ansiblePractice
```

Create a File called "inventory" and enter the public IP address of server1 and server2 in there like below

```bash
nano inventory
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696166874073/adad6db7-cdc8-43b4-a090-6423db29bda8.png align="center")

Ctrl + O to Save -&gt; Press Enter -&gt; Ctrl + X to exit.

Now we will execute our first ansible ad-hoc command. Sometimes it can ask for yes/no. press yes.

```bash
ansible all --key-file newUbuntu.pem -i inventory -m ping
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696167180456/059166db-8761-435a-a342-607ab3fa7aa9.png align="center")

---

Now Your Lab Environment Is Ready You can start learning Ansible.

%[https://twitter.com/anujsdtw/status/1702900896705970451] 

Happy automating! 🤖
