Cyber Deals - Get up to 65% off on CKA, CKAD, CKS, KCNA, KCSA exams and courses!

Managing Ansible Inventory

Managing Ansible Inventory

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.

[myself]
localhost
[intranetweb]
servera.techbeatly.com
serverb.techbeatly.com
[database]
db101.techbeatly.com
[everyone:children]
myself
intranetweb
database

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 – servera.techbeatly.com and serverb.techbeatly.com . db101.techbeatly.com 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):
    db101.techbeatly.com
    localhost
    servera.techbeatly.com
    serverb.techbeatly.com
    
[root@ansible-box ansible]# ansible intranetweb --list-hosts
  hosts (2):
    servera.techbeatly.com
    serverb.techbeatly.com

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

ansible db1.example.com -i inventory --list-hosts
ansible -i inventory --list-hosts 172.25.252.44
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 '.example.com'
ansible -i inventory --list-hosts '.example.com,!.lab.example.com'
ansible -i inventory --list-hosts lb1.lab.example.com,s1.lab.example.com,db1.example.com
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:

[webservers]
www[01:50].example.com
[databases]
db-[a:f].example.com
192.168.0.[10:20]
[a:c].dns.example.com

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/myinventory.py --list
{"webservers": {"hosts": ["servera.teachbeatly.com"], "vars": {}}}

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

See all parts of Automation with Ansible Guides here

Gineesh Madapparambath

Gineesh Madapparambath

Gineesh Madapparambath is the founder of techbeatly. He is the co-author of The Kubernetes Bible, Second Edition and the author of Ansible for Real Life Automation. 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). (Read more: iamgini.com)


Disclaimer: Views expressed in articles are those of the respective authors and do not necessarily reflect the views of their employers or this site. While we aim for accuracy, please verify information before relying on it, and let us know if you spot an error.
Share:

Related Posts

Installing Ansible

Installing Ansible

You may refer installation doc from official document as well. Requirement Controlnodes Controlnode is just the machine where we install the Ansible …

Infrastructre & hardware

Subnet Mask Information – Quick Reference Sheet

Address Bits Pref NetMask 1 0 /32 255.255.255.255 2 1 /31 255.255.255.254 4 2 /30 255.255.255.252 8 3 /29 255.255.255.248 16 4 /28 255.255.255.240 32 …

AWS Certified Solution Architect – Associate – Learning Path and Certification

AWS Certified Solution Architect – Associate – Learning Path and Certification

I used to get a lot of queries on AWS learning path and exam preparation procedures via emails or chats. Honestly I am not an...

Boot from image using vFlash in Dell PowerEdge Servers

Boot from image using vFlash in Dell PowerEdge Servers

Dell iDRAC is a feature-full server remote administration tool but iDRAC is missing the very basic functionality to boot from a ftp or http based...

How to Install and Configure AWS Command Line Interface (CLI)

How to Install and Configure AWS Command Line Interface (CLI)

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control …

Infrastructre & hardware

How to check CPU count information (LUX)

Quick methods to check. Linux [root~]# dmidecode -t processor |grep CPU Version: Intel(R) Xeon(R) CPU E7- 4830 @ 2.13GHz Version: Intel(R) Xeon(R) CPU …