What is the Ansible Hosts File ?
The Ansible Hosts File or Ansible-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.
When you install Ansible the default location for inventory is /etc/ansible/hosts
Ansible/Hosts File Location
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.
Ansible/Hosts File Example
The Ansible hosts file is written in either YAML or INE format. The most common format is INE chekc the ansible/hosts file example below.
host1.domain.com 192.168.1.193 [routers] router1.domain.com router2.domain.com CSR-1 ansible_host=192.168.1.199 [switches] switch1.domain.com switch2.domain.com
Hosts can be defined either by IP address or DNS name or if DNS is not resolvable using the ansible_host command
Hosts can also grouped together by putting under groups named by a name in square brackets e.g. [Routers]
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 hosts 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 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.
Ansible multiple inventory files
Whilst the default hosts file resides in /etc/hosts a best practice is to create a custom inventory/hosts file within your project folder, so you basically have a hosts file per project. This depends on your use case but for myself as a consultant and working in different environments this is what I do.
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
Ansible is the most popular tool for Network Automation right now, mainly because of it simplicity to get started. You can be up and running in a few hours.
If you want a hand to get started I have a course which takes you from installation to running your first production ready playbooks.
What is the Ansible hosts file?
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 the 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 I create an Ansible hosts file?
You can create an inventory hosts 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 Best Practices – this post covers how to encrypt your passwords and sensitive information using Ansible Vault
- Ansible Course – this is my flagship course covering all aspects of using Ansible for Network Automation
- Ansible vs Python – Which one should you learn?
- Cisco Switch IOS Upgrade using Ansible
- How to install Ansible Tower
- What are the advantages of using Ansible Roles
- Ansible IOS Command Example
- Ansible Tutorial for Network Engineers
- What is the Ansible Hosts File ?
- Ansible/Hosts File Location
- Ansible/Hosts File Example
- Ansible Inventory YAML
- Ansible Inventory File Groups
- Ansible Inventory File Best Practices
- Ansible multiple inventory files
- Dynamic Ansible Inventory
- Ansible Hosts File Comments