Ansible Means Automation
If you belong to the IT industry then you most probably heard the buzzword Ansible, If you don’t then I will explain it to you. For better understanding, I want you to think of Automation when you hear the word Ansible, but why because it is just about automation, But Automation of what? Automation of cloud, automation of servers, or network blah blah blah anything comes in the category of IT infrastructure.
Hard to digest? Don’t worry, here is the simplest Definition of Ansible which is self-explanatory
Ansible is an open-source automation tool used for IT configuration Management, Task automation and Application deployment.
Now what is ansible is clear but the next question that comes into mind is that why Ansible? As there are a lot of other alternatives available for this purpose available in the market like chef, SaltStack, Rudder etc.
The answer is quite simple because Ansible is the most famous tool used for automation because it is super easy to use, no need to know programming to use it, and most importantly it is free to use, it is open-source and backed by Redhat. If you mention ansible in your skillset, you are going to be more prominent.
How actually Ansible Automates(A Practical Use Case)?
Consider a scenario, You are a Linux administrator and you have to update a file on a single server then it totally fine you can do it in seconds but what if you have to update that same file manually in 10 or maybe 100 servers?
It’s a huge mess, Right? This is when Ansible comes into the picture. Ansible is used to configure any type of server in just a few minutes. In Ansible we build a “Control station/Management Node” where we set up ansible and its stuff like python etc which is required, and we will connect with other devices via ssh(secure shell), and most importantly we are not required to set up anything over devices that we are supposed to manage/automate because it is agentless. For better understandings, Let’s have a look at its architecture, a bigger picture of how Ansible works.
Now as you can see on the left side we have “Ansible Management Node” which is our Master Node used to control our Hosts. On the right side, we have Hosts(Managed Nodes) i-e the servers we have to manage.
Master Node establishes a connection with Managed Nodes using a Network protocol SSH, and it can reach thousands of servers at the same time securely. Now we are going to the installation phase then it will be easy to understand the concepts of “playbook” and “inventory”.
Time to get your hands dirty with Ansible ;)
- Local-machine, it can be your laptop or desktop. I have Debian Linux installed as the base OS.
- Two virtual machines on your system using VirtualBox or Vmware or servers over the cloud. I have set up servers on the Google cloud platform, you can set up wherever you want locally or remotely choice is yours.
- Basic understanding of Linux commands is mandatory for Ansible.
Installation of Ansible:
Ansible is fairly easy to install. We can install on Debian based Linux with just single command:
sudo apt-get install ansible
Or if you are using other Linux distro like CentOS then you can use
yum install ansible
Alternatively, you can install ansible using pip3 if you have python installed on your system, and the command will be as follow
pip3 install ansible
Note: Offcourse you need to update the packages repository before installation.
Tweaking the Hosts/Inventory:
So now as we have installed ansible on our local machine and boom we are ready to manage our Hosts with our Master Node, to do this so we are going into the ansible directory.
As now we are in ansible directory we have to list all the files here by using command ls. You will have ansible and hosts, as you can see below
Now let discuss the purpose of these two files one by one.
ansible.cfg: This file is used to adjust certain settings in ansible like inventory, library, ask_pass, etc.
hosts: This file is the key component in the ansible also referred as “Inventory” file, it contains all the list of Linux machines, windows machines, or router anything in the IT infrastructure domain which you want to control.
Now the fun part is going to start, first of we have to list of Hosts which we have to manage, so for doing that so we need to add those in the hosts’ file. Follow along with me, run the following command
Go to the end of the file and define the name of the group you are going to manage, in my case I will write [linux], and after we have to mention the IP(s) below the group name and finally we have to write the names(of user on the server) and passwords for my servers to whom I am going to connect, in my case name of user on both servers is same so I am writing it once ansible_name=usman59228 and password=write_your_password. Before doing this we will write [linux:vars] for giving access to the group. Let me clarify by following a screenshot of my host file,
Congratulations! You have completed your first inventory file set up and we are pretty much ready to go.
Validate what we just have done
We need to run some basic commands to check whether we going in the right direction, so our first command is
Syntax: ansible host -m(flag used for module) name_of_module
Actual command: ansible linux -m ping
Explanation of command
Word ansible as a command is self-explanatory and then we have Linux which is the name of our host defined in the “hosts” file, and then -m flag used before module_name and ping is the name of the module which verifies that either “Master Node’s” connection is established with “hosts” or not.
Let’s have a look at what actually happens when we run this command.
Ignore the warnings, Boom we successfully ran Ansible, as you can see SUCCESS message from both servers indicating that we are going in the right direction.
Let’s try to run another command
ansible host -a “cat /etc/os-release”
In this case, we will get the OS release of both servers, lets give it a shot
Now it will be more clear to you how things are working in ansible. I hope this little intro to Ansible will help you.
PS: If you liked the article, please support it with claps. Cheers