cancel
Showing results for 
Search instead for 
Did you mean: 
Trevor
Commander Commander
Commander
  • 248 Views

Ansible Inventory Filename

Ansible executes automation tasks on managed nodes or “hosts”.
We define these hosts or groups of hosts in a file called inventory.

Those 2 lines above (in red) didn't come from Wikepedia - they came
directly from the "Red Hat Interactive Learning Portal".   That one word
that I have underlined is causing a distrubance in the force.  The comment
suggest (to me) that an inventory filename has to be named "inventory".
A few lines later, in that same lesson, an example is provided, creating an
an inventory file, and the name given to that filename is "hosts.yml".

Ouch!  Does the inventory filename have to be "inventory", or can it be
"something.yml"???   Ouch!

 

 

 

Trevor "Red Hat Evangelist" Chandler
Labels (3)
5 Replies
Chetan_Tiwary_
Community Manager
Community Manager
  • 233 Views

@Trevor you can name it anything you want. But make sure to let the ansible command know where it is by using -i option in the running command :

ansible-playbook -i <path to your inventory >  <your.yaml>

or let it know via your ansible.cfg file .

If you dont tell it anything , poor chap will try to find an inventory file at this location  /etc/ansible/hosts as the last resort. 

Chetan_Tiwary__0-1757963495555.png

https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html 

Travis
Moderator
Moderator
  • 158 Views

@Trevor -

The Ansible inventory file can be anything actually, and doesn't even need to be a file. It could be a directory full of inventory files. The important thing is to know what your system is using for your ansible.cfg that specifies the inventory file. As @Chetan_Tiwary_ mentioned, you can also specify an inventory file with the -i on the command line. Keep in mind though, calling it an inventory file can be confusing and it would be better to refer to it more generically as Ansible inventory since it can be a file or a directory. 

Inventory files are often lists of hosts and variables, but inventory files can also be scripts or modules that interact with inventory sources ... at one point in time we had a python script that could leverage IdM as an inventory source for Ansible.

Inventory files also don't need to have an extension, but the extensions can help to know what format it is in such as .yml/.yaml or .toml or whatever, but again, the important thing to keep in mind is that the file or directory is specified and set in the ansible.cfg.

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
Travis
Moderator
Moderator
  • 157 Views

One other thing I should point out as it has gotten me in the past ... when dealing with inventory directories, it reads the inventory files in alphabetical order. So if you are using things like inventory variables or doing any type of dynamic inventory, you need to keep that in mind. 

I had inventory created dynamically, and it wasn't finding some of my inventory variables and systems because order can matter, so it might be necessarry to prepend things with ZZ_ so it comes at the end. This way you can control the order that inventory is processed.

Travis Michette, RHCA XIII
https://rhtapps.redhat.com/verify?certId=111-134-086
SENIOR TECHNICAL INSTRUCTOR / CERTIFIED INSTRUCTOR AND EXAMINER
Red Hat Certification + Training
Chetan_Tiwary_
Community Manager
Community Manager
  • 91 Views

very good addition @Travis . Apart from this there is this option where you can pass multiple inventory sources explicitly with specific order like this :

ansible-playbook -i base_inventory/ -i override_inventory/ playbook.yml 

https://docs.ansible.com/ansible/latest/plugins/inventory.html 

I heard of something called ansible_group_priority as well ( not sure though how it works or apply here ).

Trevor
Commander Commander
Commander
  • 80 Views

A very nice addtion to my ad-hoc commands repertoire!

Thanks Chetan!!!

Trevor "Red Hat Evangelist" Chandler
Join the discussion
You must log in to join this conversation.