Ansible inventory management

In this article, we will learn what is an ansible inventory management file and how to use it with ad-hoc commands, and how to use a custom inventory file.

Ansible is an automation tool that gives the ability to configure multiple servers from one single location. So we have to maintain a list of servers to manage them. The file which maintains this list of servers is known as the inventory file or hosts file.

The Ansible inventory file has a list of all managed host names. One host’s details will be written in one line of a hosts file.

The default location of the inventory file is /etc/ansible/hosts. The hosts file is the default ansible host file where we can add all our infrastructure servers to be managed by Ansible. We can add our hostname in either IP address format or fully qualified domain name or just server name.

>> vi /etc/ansible/hosts

suraj.getinputs.com
mysql.getinputs.com
webserver.getinputs.com

Grouping

Grouping helps when we have to perform an operation on a group of hosts instead of an individual host.

>> vi /etc/ansible/hosts

suraj.getinputs.com

[mysql-servers]
mysql-1.getinputs.com
mysql-2.getinputs.com
mysql-3.getinputs.com 

[web-servers]
webserver-a.getinputs.com
webserver-b.getinputs.com
webserver-c.getinputs.com

In the above list, we are grouping the hosts. A host can be present in more than one group.

mysql-1.getinputs.com, mysql-2.getinputs.com, and mysql-3.getinputs.com are grouped under mysql-servers.

webserver-a.getinputs.com, webserver-b.getinputs.com, and webserver-c.getinputs.com are grouped under web-servers.

Grouping patterns

If our hostnames are similar then we can make use of some patterns instead of writing individual hostnames in multiple lines. We can make use of either numeric or alphabetic format like below:

>> vi /etc/ansible/hosts

suraj.getinputs.com

[mysql-servers]
mysql-[1-3].getinputs.com

[web-servers]
webserver-[a-c].getinputs.com

mysql-[1-3] : It will pick mysql-1, mysql-2 and mysql-3

webserver-[a-c] : It will pick webserver-a, webserver-b and webserver-c

Using a different user instead of root

In some cases, we may need to use a different user than the root. When we run Adhoc commands or playbooks they will run by using the default user root. If we want to override the root user then we can make use of the below option like

>> vi /etc/ansible/hosts

suraj.getinputs.com ansible_user=suraj

[mysql-servers]
mysql-[1-3].getinputs.com

[web-servers]
webserver-[a-c].getinputs.com

ansible_user is an argument that is used to override the user.

When we run the host suraj.getinputs.com it will override the user root to suraj whereas mysql and webserver hosts will use the default root user.

Using an inventory file with Adhoc commands

Adhoc commands are used to run the smaller tasks. We can run only one task at a time. If we want to run multiple tasks at a time then we have to make use of playbooks.

>> ansible all -m ping

all : It will take list of all the servers mentioned in the inventory file. It doesn’t matter if the host belongs to any group.
-m : m means a module, their are different modules available in ansible.
ping : ping is a module name. Ping module is used to check the connection to the host server.

The above command will ping all the servers in the inventory file. If we have to ping only a group of servers. Say we have to ping all the web-servers in the hosts file then we can specify that group name in the ansible command instead of all arguments.

>> ansible web-servers -m ping

If we are specifying the wrong group name then what happens?

>> ansible servers -m ping

Output: No hosts matched, nothing to do

As the servers group does not exist in the inventory file hence no hosts are matched and since there are no hosts so no operation can be performed.

Using a custom inventory file

If we don’t want to use a default host file then we can create a new host file in a different location and make use of it as a custom inventory file.

Firstly we will delete all the host names from the actual inventory file /etc/ansible/hosts. Now we will create a new file named hosts in /opt folder.

>> vi /opt/hosts

suraj.getinputs.com

[mysql-servers]
mysql-[1-3].getinputs.com

[web-servers]
webserver-[a-c].getinputs.com

>> ansible web-servers -m ping

If we run this command then by default ansible tries to pick the hosts from /etc/ansible/hosts but there is no group named web-servers in the file as it has already been removed. Hence we will get an output as No hosts matched.

We can make use of our new inventory file using -i option

>> ansible -i /opt/hosts web-servers -m ping

Output: Success. Ansible will ping all the servers in the web-servers group from the /opt/hosts file.

In this article, we have learned what is an ansible inventory management file and how to use it with ad-hoc commands, and how to use a custom inventory file. I hope you found this article interesting and valuable. Please share this article with your friends and help me grow. If you are having any concerns or questions about this article please comment below. If you want to get in touch with me please visit the Contact Me page and send an email.

Leave a Comment