What's this Ansible?
Ansible is an open-source automation tool, or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning
Task-01
Installation of Ansible on AWS EC2 (Master Node)
- Open the AWS console and create an EC2 instance.
- Connect to the instance and execute the Ansible commands
Add the Ansible PPA repository
sudo apt-add-repository ppa:ansible/ansible
Update the packages
sudo apt update
Install Ansible
sudo apt install ansible -y
Check the ansible version
ansible --version
Our Ansible Master is installed.
Task-02
Ansible always requires hosts to run or deploy the applications, so let's check what hosts are and where to update or find them.
Ansible hosts file is a configuration file that contains a list of hosts or servers that Ansible can manage. The host's file is located at /etc/ansible/hosts on the Ansible control node, and it is used to define the inventory of hosts that Ansible can manage.
To edit the host's file, by using the below commands
sudo nano /etc/ansible/hosts
It contains all the host's details that are categorized as Ungrouped, webservers, dbservers
Press "control+X" and press "Y" to save the file
ansible-inventory --list -y
Displays a YAML-formatted list of hosts and their attributes, including the hostnames, IP addresses, and any other defined variables or group memberships.
Task-03
Created 2 more EC2 instances as hosts with the same Private keys (pemfile) as the previous instance (Master)
On Master add all the host PublicIP's of (Nodes) that you want to configure through Ansible
sudo nano /etc/ansible/hosts
Press "control+X" and press "Y" to save the file
I have added host IPs under webservers and saved the file
How to try to ping all the hosts using the below command them
ansible all -m ping
Permission is denied due to public key, now we need to provide the private pem.file in the host file
Now we need to update the pem key from local to EC2 using SCP
scp -i "mykeypair.pem" mykeypair.pem ubuntu@ec2-34-224-80-215.compute-1.amazonaws.com:/home/ubuntu/.ssh
Check the pem file in EC2 terminal and change it permissions
chmod 600 <your pem key>
Now configure the host file by adding the pem file
sudo nano /etc/ansible/hosts
We passed the mykeypair.pem in hosts file as a variable
Let's check the status of the host VMS by executing the below command
ansible all -m ping
That's it you have successfully pinged the 2 hosts that we have defined in our host file.