So in this blog we will how to use Ansible as a Configuration management tool and for that we will connect 3 worker server to 1 master server using same key pair and then write Ansible Playbook to give them certain task and to install dependencies on master node which will automatically install dependencies on worker nodes we don't have to give commands one to one to a particular server
Creating Instances:-
Login to AWS console
Search for EC2 Instance
We will create 1 master server and 3 worker servers
Click on Launch Instances
Name the instance as Ansible-master , set 1 as number of instances and choose Ubuntu as Amazon Machine image
Create a new key pair name it as ansible-key as we will use the same key for the worker nodes which will make it easy to navigate to worker nodes via master node
Keep rest of the things as default and click Launch instance
Creating worker servers
Click on Launch instance
Choose the name as server , set 3 as number of instances ( as we are using 3 worker nodes)
Again, choose ubuntu as Amazon Machine image and choose the same key pair which you used in Ansible-master instance because the key will help us to connect with worker servers
Rest set everything as default and click Launch instance
Now you will there are three instances created , rename all the server as server-1, server-2, server-3
Now we will install Ansible on master node
- Enter the command on your command line
ssh -i {key pair path .pem file} ubuntu@{IP address of master node}
2. Enter the command:
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
Now will connect master node to worker nodes
- Firstly, we will update our hosts file
sudo vim /etc/ansible/hosts
Now we will add the IP addresses of all 3 worker servers in the hosts file
[servers]
server_1 ansible_host={IP address of server-1}
server_2 ansible_host={IP address of server-2}
server_3 ansible_host={IP address of server-3}
To finally connect worker servers we will add the key in the master server, for this we will create a new folder
mkdir keys
cd keys
Now we need to copy our .pem file (that we have used while creating instance ) from our local machine to the master server.
Now open your power shell and write the command
scp -i {key pair path} {key pair path} ubuntu@{IP address of master server}:{path of keys folder that we have created on master server}
For example:
scp -i "C:\Downloads\ansible-key.pem" C:\Downloads\ansible-key.pem ubuntu@34.453.56.67:/home/ubuntu/key
Now this command copy the pem file to the key folder of the server
Now again open hosts file
sudo vim /etc/ansible/hosts
And add the lines
[servers:vars]
ansible_ssh_private_key_file= {path of key pair in key folder}
Save the file and check if the server is connected or not
sudo ansible servers -m ping
If you see SUCCESS as output congratulations you have connected 3 worker server with master server
That's a wrap........
Now in the next part we will see how to write Ansible Playbook and give task to the working servers.