How can I get a list of hosts from an Ansible inventory file?

MrDuk picture MrDuk · Jun 3, 2016 · Viewed 31.2k times · Source

Is there a way to use the Ansible Python API to get a list of hosts from a given inventory file / group combination?

For example, our inventory files are split up by service type:

[dev:children]
dev_a
dev_b

[dev_a]
my.host.int.abc.com

[dev_b]
my.host.int.xyz.com


[prod:children]
prod_a
prod_b

[prod_a]
my.host.abc.com

[prod_b]
my.host.xyz.com

Can I use ansible.inventory in some way to pass in a specific inventory file, and the group I want to act on, and have it return a list of hosts that match?

Answer

nitzmahone picture nitzmahone · Jun 4, 2016

Do the same trick from before, but instead of all, pass the group name you want to list:

ansible (group name here) -i (inventory file here) --list-hosts