Ansible - Create your own inventory file

Tasks are run against managed nodes, also known as hosts.


After a clean install of Ansible, inventory in ansible.cfg is commented out, like this.

#inventory = /path/to/hosts


In this scenario, the default hosts file is /etc/ansible/hosts. After a clean install of Ansible, the default hosts file is completely commented out. If you were to issue command ansible all -m ping, the following would be displayed.

Likewise, if you were to uncomment inventory in ansible.cfg without defining your inventory, the following would be displayed.

[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


Managed nodes are defined in inventory. There are multiple ways to define your inventory. Refer to Ansible - Defining your inventory. One option is to create your own inventory file on your Ansible control node, which is what this article deals with. The hosts / inventory file can use different file formats. The most commonly used formats are INI and YAML. Here is an example using the INI format.


And here is the YAML format.



Children can be nested, like this in YAML.



Or like this in INI.





Let's say you create a file named inventory.yml. You could then use the ansible-playbook command with -i inventory.yml to run the foo.yml playbook against the hosts in inventory.yml.

Be aware that if you do not use the -i command line option, the default hosts file will be used.

ansible-playbook foo.yml -i inventory.yml


Notice that the hosts / inventory file is static, meaning it contains a predefined list of managed node, which is the antithesis of automation. You can dynamically maintain a list of managed nodes.

Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter 09d84 in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |