Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any option to list groups in Ansible?

Tags:

ansible

As far as i know, ansible has an option named --list-hosts for listing hosts. Is there any option for listing host groups? Or any other way to come through?

like image 427
Kadir Avatar asked Oct 27 '15 08:10

Kadir


People also ask

What is groups in Ansible?

Group variables are a convenient way to apply variables to multiple hosts at once. Before executing, however, Ansible always flattens variables, including inventory variables, to the host level. If a host is a member of multiple groups, Ansible reads variable values from all of those groups.

How do you define group variables in Ansible?

We can create directories under the group named directory, it will read all the files under these directories in lexicographical. If there is some value which is true for your environment and for every server, the variable containing that value should be defined under /etc/ansible/group_vars/all file.

Does Ansible do inventory?

do-ansible-inventory is a tool that generates an Ansible inventory file with your DigitalOcean Droplets. It is an alternative to dynamic inventories as you can run do-ansible-inventory once and receive a static inventory file that you can use anywhere, copy, or modify.


1 Answers

You can simply inspect the groups variable using the debug module:

ansible localhost -m debug -a 'var=groups.keys()' 

The above is using groups.keys() to get just the list of groups. You could drop the .keys() part to see group membership as well:

ansible localhost -m debug -a 'var=groups' 
like image 134
larsks Avatar answered Sep 20 '22 21:09

larsks