The Ansible Hosts File or Inventory file tells Ansible about the hosts that it can connect to. For Ansible to automate a Linux Server, Network device or Cloud server it has to exist within the inventory (also known as the Ansible hosts file) and saved in either YAML or INI format. The file can also be static or created dynamically by a script. All options will be covered in this tutorial below.
- Ansible Hosts File Default Location
- Ansible Inventory File as YAML
- Ansible Inventory File Groups
- Ansible Inventory File Best Practices
- Dynamic Ansible Inventory
- Ansible Hosts File Comments
Ansible Hosts File Default Location
When you install Ansible the default location for inventory is /etc/ansible/hosts
The file is named hosts by default and is one of the default files installed with Ansible – the other main one being ansible.cfg. If you cat the file you can see it does not contain any entries (they are all commented out) but what it does do is give you a template of how to start formatting your inventory file.
What I suggest is to delete the default hosts file and create your own. I personally create a custom ansible inventory file within each project folder I create, I also then create a custom ansbile.cfg file within that same folder that references your custom host file.
#Custom ansible.cfg file
The Ansible inventory file name can be anything you want as long as you reference it in the ansible.cfg file. I always use hosts for simplicity.
Looking at the hosts file above you can see we have two groups of devices configured, CSR-Routers and SWITCHES. Each group contains some devices and in this instance the devices are defined by name and then the IP address is defined manually using the ansible_host= command.
The name CSR-1 or 2 or 3 is then what shows up when you run the playbook.
You can have multiple inventories and specify to use each inventory at the playbook runtime.
Simply create another inventory file i.e hosts-2 and at playbook runtime use the -i flag to tell Ansible to use that inventory e.g
ansible-playbook backup-router.yml -i hosts-2
You can also locate these inventory files anywhere on your device, in this example the inventory file is located in the same folder as the playbook.
In the example above CSR-1 is defined without the ansible_host command. This name must be resolvable in DNS or via a local hosts file on the Ansible control node.
The other entry is just an IP address, this will work, but when the playbook runs you will just see the IP address as the device being targeted.
All connections are made using SSH to the IP address defined in the file. Other variables such as passwords or specific ports or specific connection parameters can also be defined in this file.
Ansible Inventory File as YAML
The hosts file above is written in the default INI format, an Ansible Inventory can also be written in YAML
In the above example we have the same inventory file but in a YAML format.
Ansible Inventory File Groups
To make managing your hosts easier you can put them into groups within your inventory file. e.g
In the above example you can see I have grouped the devies into two groups denoted with square brackets. I can now address our Ansible Playbooks to the group CSR-Routers and it will will only affect the devices in that group.
If I want to address all the devices in the inventory file, we make use of the children function, as you can see above I have created a group called LAB and it has children which are the two other groups.
Now if I address a Playbook to run against LAB it will affect all devices in this inventory.
Check out my other posts on using Ansible for Network Automation
Ansible Inventory File Best Practices
- Keep your hosts file names simple – I always use hosts
- Comment were required to ensure someone else can understand your inventory
- Use children to address multiple groups at once
- Do no put passwords in your inventory file – reference them in group_vars or host_vars or even better use Ansible vault within those files.
Dynamic Ansible Inventory
For small environments, a static inventory file is sufficient as there are typically not rapid changes to the devices in use.
If you are working within a cloud environment or one where multiple hosts are being spun up and tore down regularly your Ansible Inventory file will become very difficult to manage. Most cloud providers will provide you a script you can use within Ansible to regularly poll your devices and import the inventory, this is referred to as a dynamic inventory.
Ansible Hosts File Comments
If you want to put a comment next to any item in your Ansible Hosts file just start a new line with a #
For more information you can reference the Ansible Best Practice guide on the Ansible Documentation site here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html
What is the Host file in Ansible?
The Ansible hosts file is a file which lists all the hosts that you want to connect to. Hosts can grouped into groups and also put into a super-set with the the :children operator.
Where is Ansible Hosts file?
By default the Ansible Hosts file is located in /etc/ansible/hosts but you can create a custom hosts file and put it wherever you want and then specify it’s location using a custom ansible.cfg
What are the types of inventories in Ansible?
There are two types of inventory you can use within Ansible. Also there are two file types.
The Ansible Hosts file can be static or dynamic and can be in an ini format or YAML.
How do you create an inventory file in Ansible?
You can create an inventory file in Ansible anywhere you like. It is just a text file which by default is called hosts and located in /etc/ansible/ but you can create the file wherever you like and just tell Ansible where it is when you run the playbook either with the -i switch or by defining it in the ansible.cfg file.
Other Ansible Resources
I hope you found this short tutorial informative and if you want to keep learning check out these other Ansible posts
- Ansible Backup Cisco Tutorial – this posts covers a step by step guide to installing Ansible and creating your first playbook to backup a Cisco router config.
- What is Ansible used for? – this post covers the very basics of Ansible, what it is and what it can do?
- Open Source Network Automation Tools – My Top 7 List – This post discusses some of the most popular network automation tools in use today.
- Ansible Vault Tutorial – this post covers how to encrypt your passwords and sensitive information using Ansible Vault
- Ansible Network Automation Course – this is my flagship course covering all aspects of using Ansible for Network Automation