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.[video_lightbox_youtube video_id=”0MT9WvX_j4Y&rel=0″ width=”854″ height=”480″ alt=”Ansible Hosts File Video Tutorial ” anchor=”https://www.rogerperkin.co.uk/wp-content/uploads/2019/02/ansible-hosts-file-video-tutorial-button.png”]
All options will be covered in this tutorial below.
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.
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
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.
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? – this post covers the very basics of Anisble, 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