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.
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
you will see windows like the above. Click on Instances(running)
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
Keep Every other thing Default.
on the right-hand side, you will see the summary. In "Number of instances" enter 3. then click on "Launch instance".
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
Paste That command in the terminal and press enter. it will ask for yes/no enter yes.
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
# 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:~/
now again log into the server and you can see your private key is copied like below
Now Type the below command to ensure your key is not publicly viewable
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.
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 and Install it.
When MobaXterm is first started click on Settings -> Configuration
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.
Now Click On Start Local Terminal
When we run "ls" it should show your key in the current directory
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
Paste That command in the terminal and press enter. it will ask for yes/no enter yes.
Now you are logged in to the control node. On the left-hand side, you will see SSH Browser.
Upload your private key
Now Type the below command to ensure your key is not publicly viewable
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.
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
sudo apt update
sudo apt upgrade
Sometimes you may be asked to reboot then you can reboot using the below command.
sudo reboot
Install Ansible
sudo apt install ansible
Now Let's Create a Folder Called "ansiblePractice" and go into that folder
mkdir ansiblePractice
cd ansiblePractice
Create a File called "inventory" and enter the public IP address of server1 and server2 in there like below
nano inventory
Ctrl + O to Save -> Press Enter -> Ctrl + X to exit.
Now we will execute our first ansible ad-hoc command. Sometimes it can ask for yes/no. press yes.
ansible all --key-file newUbuntu.pem -i inventory -m ping
Now Your Lab Environment Is Ready You can start learning Ansible.
Happy automating! ๐ค