Skip to content

Managing Ansible Inventory

Avatar photo

Inventory File

An inventory file is a simple and plain text file where we store those host information ansible has to deal with. It can be simple in any format (YAML, ini, etc). Default inventory file location is /etc/ansible/hosts but you can specify a different inventory file in your ansible config file (as above) or using the -i option on the command line.

[root@ansible-box ~]# ansible all -m ping -i myinventory

Here see a sample inventory file.





You can see, this is a simple one in ini format and contains few hosts and groups. Group myself contains only one host which is localhost. (Not a mandatory one, I have given as an example). intranetweb group containers 2 servers – and is under database group. And finally we have group everyone with all other groups as children.

Lets check what ansible can understand from this inventory file.

[root@ansible-box ansible]# ansible all --list-hosts
  hosts (4):
[root@ansible-box ansible]# ansible intranetweb --list-hosts
  hosts (2):    

Here see some of the sample ansible --list-host pattern for reference.

ansible -i inventory --list-hosts
ansible -i inventory --list-hosts
ansible -i inventory --list-hosts all
ansible -i inventory --list-hosts london
ansible -i inventory --list-hosts environments
ansible -i inventory --list-hosts ungrouped
ansible -i inventory --list-hosts ''
ansible -i inventory --list-hosts ',!'
ansible -i inventory --list-hosts,,
ansible -i inventory --list-hosts '172.25.'
ansible -i inventory --list-hosts 's'
ansible -i inventory --list-hosts 'prod,172,lab'
ansible -i inventory --list-hosts 'db,&london'

If you are adding a lot of hosts following similar patterns, you can add as below rather than listing each hostname:



Dynamic Inventory

Inventory can be static (like ini or YAML format) or dynamic via inventory scripts. For those working on cloud or container based environment, you will not be able to manage inventory with a static source. In that case you need a dynamic inventory system with scripts.

There are few sample inventory scripts available by community; check this repo for samples. Also read Working with Dynamic Inventories.

[devops@ansible-box dep-dynamic]$ inventory/ --list
{"webservers": {"hosts": [""], "vars": {}}}

Let’s learn about ad-hoc commands in next chapter.

See all parts of  Automation with Ansible Guides here


The views expressed and the content shared in all published articles on this website are solely those of the respective authors, and they do not necessarily reflect the views of the author’s employer or the techbeatly platform. We strive to ensure the accuracy and validity of the content published on our website. However, we cannot guarantee the absolute correctness or completeness of the information provided. It is the responsibility of the readers and users of this website to verify the accuracy and appropriateness of any information or opinions expressed within the articles. If you come across any content that you believe to be incorrect or invalid, please contact us immediately so that we can address the issue promptly.

Avatar photo
Gineesh Madapparambath is the founder of techbeatly and he is the author of the book - 𝗔𝗻𝘀𝗶𝗯𝗹𝗲 𝗳𝗼𝗿 𝗥𝗲𝗮𝗹-𝗟𝗶𝗳𝗲 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻. He has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (aka Gini Gangadharan -


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.