Hey Learners! Welcome back. As we already discussed and understood the concept of IaC and Configuration Management, Today, we will dive into the automation tool used for configuration management (CM) i.e. Ansible. CM in terms of Ansible means that it maintains the configuration of the product performance by keeping a record and updating information that describes an enterprise's hardware and software. Let's begin...
What is Ansible?
Ansible is an open-source automation tool which allows us to automate application deployment, intra-service orchestration, cloud provisioning, and many other IT tools.
Ansible is easy to deploy because it does not use any agents or custom security infrastructure.
Ansible uses a Playbook to describe tasks. Playbooks are written in the YAML language which is human readable and is commonly used for configuration files.
Ansible is designed for multi-tier deployment. Ansible does not manage one system at a time, it models IT infrastructure by describing all of the systems are interrelated. Ansible is completely agentless which means Ansible works by connecting your nodes through ssh(by default).
Ansible simplifies the tasks related to managing and configuring systems and applications, making it an effective tool for automating various aspects of IT operations and infrastructure management.
Advantages:-
Free: Ansible is an open-source tool
Powerful: Ansible lets you model even complex IT workflow.
Flexible: You can orchestrate the entire application environment no matter where it's deployed. You can also customize it based on your needs.
Agentless: You don't need to install any other software or firewall ports on the client systems you want to automate. You don't have to set up a separate management structure.
Efficient: As we don't need to install any extra software, there is more room for application resources on your server.
Task 1- Installation of Ansible on AWS EC2 (Master Node)
Create an EC2 instance on AWS.
Connect EC2 instance using SSH
Add the repository for the installation of Ansible using the following commands.
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
ansible --version
You have successfully installed Ansible.
Task 2- Understanding the Hosts File
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 the hosts that Ansible can manage.
Open the /etc/ansible/hosts file to define the hostnames or the IP addresses of the node you want Ansible to manage. Use the
vim /etc/ansible/hosts
command to edit the hist file.You can list the configured hosts, that Ansible can manage using the
ansible-inventory --list -y
command. 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 3- Setup additional EC2 instances
1- Launch 2 more instances with the same key pair as used for Ansible Master.
2- Copy key-pair from the Local host to Ansible Master via SSH
Use the scp command as follows to copy the key-pair
Check the copied file on Ansible Master using the ls command at the specified path while copying from the Local host.
3- Add host IP addresses in the /etc/hosts
file so that we can use names in the /etc/ansible/hosts
file on Ansible Master.
ansible_node1 and ansible_node2 are the names with respective IP addresses
We created nodes as a group and added both ansible nodes in it.
In the node1 group, we added only ansible_node1. Similarly, for the node2 group, we add ansible_node2.
4- Add the variable to add the copied key so that Ansible will connect with nodes with the help of this key. Also, add the user to connect with. Refer below screenshot and add the details in the /etc/ansible/hosts
file.
5- Now try to connect nodes with the ansible ad-hoc command ansible all -m ping
Successfully connected with nodes.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- Hashnode LinkedIn Github
Happy Learning!