Updated April 3, 2023
Introduction to Ansible Inventory File
Ansible Inventory File is the list of devices and these devices can be servers, network devices, or anything which can be managed by ansible like cloud services, docker, etc. Besides the list of devices, servers, and clients, and inventory file can also contain the relevant variables which may be required whenever a connection is triggered for a device or group of devices. There are many variables that can be defined to control how Ansible interacts with remote hosts or devices. One thing to note here is that you cannot configure or make changes on remote clients with ansible if you do not have an inventory file or your inventory file does not have that client’s IP or hostname in it.
What is Ansible Inventory File?
Having a list of all of your target machines is very important, especially as ansible does not have a very good log creation mechanism. Managing an inventory file is as important as having playbooks or roles as without a well-managed inventory file, you will always end up with confusion with many questions to yourself. Adding to the above, defining the appropriate variables in the inventory file reduces the task’s code in playbooks and helps in reducing interpretation time. There is a set of variables that are used for connection and defining the behavior of connection to the target machine, these variables can be defined in ansible inventory file as well as your playbooks.
But having these variables in an ansible inventory file has a more organized manner. By default /etc/ansible/hosts is the Ansible inventory. This is defined in Ansible configuration file /etc/ansible/ansible.cfg. This is a modifiable value, so you can also update it to direct ansible to look for hosts in a custom inventory file.
Put the custom inventory file’s absolute path against the keyword inventory in /etc/ansible/ansible.cfg file like below:
Similarly, if there are different inventory files, you can just give the directory name against the keyword inventory in /etc/ansible/ansible.cfgf ile like below:
How does Ansible Inventory File work?
There are two types of inventories viz Static and Agile/Dynamic. Static files are manually maintained by ansible users, but we know that our work environments change very frequently. For example, in AWS, an organization might be adding and removing EC2 instances very frequently, maybe thousands of such tasks per day. Maintaining a static inventory file in such kinds of situations is too difficult. So, to overcome this situation we must configure our ansible environment with AWS or any other such kind of environment where your clients reside, or the list of clients is maintained. In this way, we leverage dynamic inventory.
Adding to that, if the environment is like AWS, VMWare, AZURE, or clients are managed by LDAP, CMDB systems, etc. and you do not have to manage that environment. It is always suggested to configure your ansible to fetch dynamic data.
There are two types of formats supported for ansible inventory files and used worldwide in production environments.
1. INI
In this kind of files, we simply put key-value pairs. These can also be grouped based on your environment or some common property like below:
2. YAML
Below are facts to note in this case:
- It should start with all groups which contain vars, hosts and children parts.
- Defined sub-entries of any host entries will be treated as variables.
- Entries for the single host should end without a “:”, but in cases that have multiple entries then it should end with a“:”, like in the below example.
- Indentation is very important to maintain, otherwise, you will have syntax errors.
Below is a sample of the Inventory file in JSON format.
Both of the above have different advantages over each other, but both work fine in all cases and are easy to reuse, read and modify. Also, it is easy to maintain.
Also, there are two types of inventory files:
1. Static Inventory
This is a static file managed by the ansible operators. This file has a list of devices that can be in the form of IP addresses or hostnames. But to work with hostnames, an ansible server environment must have a working DNS (Domain Name Server) to resolve hostnames to IP addresses.
2. Dynamic or Agile Inventory
There are environments where devices are continuously getting added and removed. In those environments, it is very difficult to maintain a static file for having all the currently available devices. In such cases and for those environments, the ansible community provides plugins and scripts which can be used to generate a real-time inventory file. Such environments are Cobbler, AWS EC2, OpenStack, and many more. We can use these plugins or scripts and follow the relevant instructions to get the most realistic inventory file at some moment.
Examples of Ansible Inventory File
Now by using examples, we will try to know more about the ansible inventory files, which you might have to use in your day-to-day operations. We will take some examples, but before going there, we should first know our lab, we used for testing purposes. In the lab, there is an ansible-controller server where we are running our commands and where the inventory file resides.
Example #1
In this example, we created a sample inventory file with the below contents. Which have one-to-one entries for target hosts.
Now we can check the list of available hosts in inventory like below:
Code:
ansible all --list-hosts
Output:
Example #2
In this example, we created a sample inventory file with the below contents that have groups of hosts. These groups can contain IPs and hostnames.
Now we can check the list of available hosts in inventory like below:
Code:
ansible all --list-hosts
Output:
Similarly, we can check hosts in a group like below:
Code:
ansible all Public_IP --list-hosts
ansible all Web_Servers --list-hosts
Output:
Conclusion
In today’s changing IT environment, you must have a change management tool in your toolbox and ansible (no doubt) is the best of all available currently considering its vast community, supported platforms, and clientless behavior. So, having good control over your ansible inventory is a must skill. This will not only reduce your work but also showcase your capacity to manage your work.
Recommended Articles
This is a guide to Ansible Inventory File. Here we discuss the introduction, how does ansible inventory file work? and examples respectively. You may also have a look at the following articles to learn more –