Ansible is an open-source, command-line IT automation software application written in Python. It can configure systems, deploy software, and orchestrate advanced workflows to support application deployment, system updates, and more. As of Feb 2023, Ansible has a Market share of 25.37%. Its main competitors are Terraform, Puppet, and Chef.
The architecture of Ansible — Ansible architecture is pretty simple. There is only one management node and others will be hosted where we want our configuration management through an SSH connection.
How to install Ansible:-
Step 1 — Launch an EC2 Instance with T2 Micro, Ubuntu Image, with HTTP, and HTTPS enabled. Create a new key-pair and call it ansible-all-access-key. Label it as ansible-master
Step 2 — Install the packages on the ansible-master EC2 Instance
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
We need to install Ansible only on the Master node. The major advantage of Ansible is that it pushes based on Syntax. We only need to configure the IP address of other servers in the Ansible Master. There is no need to set up an agent or anything and that’s what makes it unique in the offering portfolio. To check if ansible is installed or not, there is an inventory file that gets created in the system. You can view it using cat etc/ansible/hosts. If you are able to see this file, it means that Ansible is installed on your system. Everything will be saved in this host file.
The output of the Inventory File would look somewhat like this
Step 3 — Create three additional EC2 Instances. You can label them as ansible-servers. Here, you can need to use the ansible-all-access key while creating the instance.
Note down the IP Address of the three ansible-servers to be input in the Inventory File.
Step 4 — Using the VIM Editor, we will go to our Inventory File #sudo vim /etc/ansible/hosts and input the Public IP address of the three Ansible-Servers that we want to be configured remotely.
Step 5 — We will need to copy the ansible-master-key from our downloads using this command
Now, you will be able to see the access key in your ssh folder
Step 6— We will need to store the variable name to store the Private Keys.
Step 7 — Next, we will need to give read permission to this access key for the current user using #chmod 600 ansible-all-access-key
Step 8— Now, when you will use the command, #ansible servers -m ping, you will see the below output. You can see the output as “Ping”: “Pong”. Using a single command line, we are able to ping our servers without needing to SSH. This is used for the purpose of automation when we have a lot of servers and do not want the hassle of manual configuration.
Practical Use Cases
Using a single command, we can get the disk space of all our servers
# ansible servers -a “df -h”
The output would look like this
We can update all the servers using a single command
# ansible servers -a “sudo apt update”
It will update packages automatically on all three servers.
We can see the uptime of all the servers using
# ansible servers -a “uptime”
The output would look like this
To summarize, using Ansible, we can get the output without having to SSH into our Remote Servers. This is how automation can be done.
Hope you liked this brief introduction to Ansible. In the next blog, we will learn how to write Ansible Playbooks. If you liked my blog, please give claps and follow me. Thank you! 🙏
Github Repository — https://github.com/writetoritika/ansible-project