In today's fast-paced IT landscape, managing and maintaining infrastructure can be a daunting task. With the rise of cloud computing and complex distributed systems, the need for efficient and scalable configuration management solutions has become more crucial than ever. This is where tools like Ansible come into play, offering a powerful platform for automating various IT tasks.
What is Ansible?
Ansible is an open-source automation tool that falls under the category of Configuration Management Software. It is designed to simplify complex tasks such as configuration management, application deployment, intra-service orchestration, and provisioning. Ansible operates by using SSH (Secure Shell) to connect to remote machines and execute predefined tasks known as "playbooks."
Task-01
Installation of Ansible on AWS EC2 (Master Node)
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
Create an EC2 instance.
Connect to your EC2 instance using SSH.
Add the Ansible PPA repository using the following command:
sudo apt-add-repository ppa:ansible/ansible
- NOTE : If you are using Ubuntu 24.04 LTS or above then you don't need to run the above command.
Update the package using the following commands
sudo apt update
Install Ansible using the following command:
sudo apt install ansible
Once the installation is complete, you can check the version of Ansible using the following command:
ansible --version
Task-02
Read more about Hosts file sudo nano /etc/ansible/hosts and ansible-inventory --list -y
The Ansible hosts file is a configuration file that contains a list of hosts or servers that Ansible can manage. The hosts 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 hosts file, you can use any text editor of your choice.
sudo nano /etc/ansible/hosts
Once the file is open, you can add the IP addresses or hostnames of the servers you want to manage. The format for adding hosts is as follows:
[group_name]
host1
host2
host3
In this example, group_name is a user-defined name for the group of hosts, and host1, host2, and host3 are the IP addresses or hostnames of the servers. You can define multiple groups of hosts in the hosts file, each with its own list of hosts.
After you have added the hosts to the file, you can verify the inventory of hosts that Ansible can manage using the ansible-inventory command with the --list and -y options:
ansible-inventory --list -y
This command will display a YAML-formatted list of hosts and their attributes, including the hostnames, IP addresses, and any other defined variables or group memberships.
Task-03
Setup 2 more EC2 instances with same Private keys as the previous instance (Node)
Launch 2 new EC2 instances with same private keys as ansible-demo-master instance.
Edit /etc/ansible/hosts file and add public key of ansible server instances
[servers]
server1 ansible_host=public ip of server1
server2 ansible_host=public ip of server2
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/private_key
ansible-inventory --list -y
To establish secure communication, create a public key on the master server and copy it using the ssh-keygen
command.
ssh-keygen
Copy the public key of the master server to the nodes. This can be found in the id_rsa.pub
file.
Now, we'll proceed to copy the above public key on the nodes. We'll use the below command to open the authorized_keys file
. Do the same on both nodes.
sudo vim /home/ubuntu/.ssh/authorized_keys
Once the private keys are copied, run the following command to test the connectivity to the nodes using Ansible:
ansible all -m ping -i /etc/ansible/hosts
Congratulations! You have successfully completed the tasks for today. By installing Ansible, understanding the hosts
file, and testing connectivity, you have taken the first steps towards leveraging the power of Ansible for configuration management.
I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching .
thank you : )