Understanding Configuration Management with Ansible: A Step-by-Step Guide
Day 55 : #90DaysOfDevOps Challange
In the world of DevOps, efficient configuration management is vital for managing and scaling infrastructure effectively. Ansible, a powerful automation tool, simplifies configuration management by allowing you to define and manage infrastructure as code. In this blog post, we will walk through the process of installing Ansible on an AWS EC2 instance and explore its basic functionalities.
Task-01: Installing Ansible on AWS EC2 (Master Node)
To begin our journey with Ansible, we need to set up the master node where Ansible will be installed. Follow these steps:
Log in to your AWS EC2 instance as the administrator.
Open the terminal and execute the following commands:
sudo apt-add-repository ppa:ansible/ansible sudo apt update sudo apt install ansible
This will add the Ansible repository, update the package lists, and install Ansible on your EC2 instance.
Task-02: Understanding the Hosts File
The host file in Ansible plays a crucial role in defining the inventory of servers or nodes that Ansible will manage. To learn more about the host file and its structure, follow these steps:
Open the terminal on your EC2 instance.
Execute the following command to open the host file in a text editor:
sudo vim /etc/ansible/hosts
This command will open the hosts file using the vim text editor.
Familiarize yourself with the structure of the hosts file and make any necessary modifications to suit your infrastructure requirements.
Save the changes and exit the text editor.
Task-03: Setting Up Additional EC2 Instances (Nodes) and Testing Ansible Connectivity
In this task, we will set up two more EC2 instances as nodes and establish connectivity between the master node and the nodes using Ansible. Follow these steps:
Create two additional EC2 instances with the same private keys as the previous instance. Ensure that these instances have the necessary network and security configurations.
On the master node, open the terminal and navigate to the directory where the private key is located.
Use the following command to set the appropriate permissions for the private key file:
chmod 400 ansible_key
On the master node, open the terminal and execute the following command to open the hosts file in a text editor:
sudo vim /etc/ansible/hosts
This command will open the hosts file using the vim text editor.
Update the file with the following content:
[servers] server1 ansible_host=<ip> server2 ansible_host=<ip>
Replace
<ip>
with the actual IP address of each server you want to manage with Ansible.Save the changes and exit the text editor.
Let's verify the inventory that we have created.
ansible-inventory --list -y
Now, test the connectivity between the master node and the nodes by executing the following command:
ansible all -m ping --private-key=ansible_key
This command instructs Ansible to ping all the nodes defined in the hosts file using the specified private key.
Oops!!Failed
May be error with setting up the ssh connection between the asnsible master and client servers🧐🧐
To resolve the connectivity issue and ensure successful pinging of the node servers using Ansible, you can follow these steps:
On the master server, generate a public key by executing the following command in the terminal:
ssh-keygen -t rsa
This command will generate a public-private key pair in the
~/.ssh
directory on the master server.Locate the generated public key file (
id_rsa.pub
) in the~/.ssh
directory. You can view the contents of the public key file by using the following command:cat ~/.ssh/id_rsa.pub
Note down the content of the public key.
Connect to each of the node servers using SSH and navigate to the
~/.ssh
directory on each node.Open the
authorized_keys
file on each node using a text editor and paste the content of the master server's public key (id_rsa.pub
) at the end of the file. Save the changes and exit the text editor.Once you have copied the public key to both node servers, you can test the connectivity using Ansible. Execute the following command in the terminal on the master server:
ansible all -m ping
This command will use the inventory file (
/etc/ansible/hosts
by default) to ping all the nodes. Since you have already set up the connectivity by copying the public key, Ansible should be able to connect to the nodes and perform the ping successfully.If the connectivity is successful, you will see a response indicating a successful ping from each node.
By following these steps, you should be able to establish SSH connectivity between the master server and the node servers by copying the public key. This will enable Ansible to connect to the nodes and perform operations such as pinging or executing playbooks.
- If the connectivity is successful, you will see a response indicating a successful ping from each node.
Conclusion
Congratulations! You have successfully completed the essential tasks to understand configuration management with Ansible. In this blog post, we covered the installation of Ansible on an AWS EC2 instance, explored the hosts file, set up additional EC2 instances as nodes, and tested Ansible connectivity between the master node and the nodes. With Ansible, you can now automate and streamline your infrastructure management processes effectively. Stay tuned for our next blog post, where we will delve deeper into Ansible's powerful features and advanced configuration management techniques.