40

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?

Kadir
  • 1,664
  • 2
  • 19
  • 22

7 Answers7

58

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'
larsks
  • 277,717
  • 41
  • 399
  • 399
14

Listing groups

Using tools built-in to Ansible + jq, this gives you something reasonably close:

ansible-inventory --list | jq "keys"

An advantage of this approach vs manually parsing inventory files is that it fully utilizes your ansible.cfg files (which can point to one or multiple inventory files, a directory of inventory files, etc...).

Example output on my machine (local_redis_all is a locally defined Ansible group):

[
  "_meta",
  "all",
  "local_redis_all",
]

If you prefer it in plain-text form, use an approach like ansible-inventory --list | jq -r "keys | .[]". It will give you an output like this:

_meta
all
local_redis_all

Listing hosts

This was not part of the original question, but including it here anyway since it might be useful to some of my readers. Use the following command for JSON output (note: the command actually outputs a JSON array for each group, I haven't yet figured out a way to flatten these with jq - suggestions are welcome):

ansible-inventory --list | jq ".[].hosts | map(.)?

This gives you an output similar to this:

[
  "redis-01",
  "redis-02",
  "redis-03"
]

Likewise, in raw text format (one host per line): ansible-inventory --list | jq -r ".[].hosts | .[]?"

This gives you an output like this:

redis-01
redis-02
redis-03
Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
8

Note:- For New Ansible Users

Ansible has some special internal variables which are also known as Magic Variables.

From this link you will get full list of magic variables Magic Variables

There is magic variable called "groups" which hold the inventory group information. we can access the value of any variable ( both user defined and Internal ) using an ansible module called debug .

I am Using Separate Inventory File

$
$ ansible -i inventory.ini all -m debug -a "var=groups" 
$
centos-client.ansible.lab | SUCCESS => {
    "groups": {
        "all": [
            "centos-client.ansible.lab", 
            "ubuntu-client.ansible.lab"
        ], 
        "centos": [
            "centos-client.ansible.lab"
        ], 
        "ubuntu": [
            "ubuntu-client.ansible.lab"
        ], 
        "ungrouped": []
    }
}
ubuntu-client.ansible.lab | SUCCESS => {
    "groups": {
        "all": [
            "centos-client.ansible.lab", 
            "ubuntu-client.ansible.lab"
        ], 
        "centos": [
            "centos-client.ansible.lab"
        ], 
        "ubuntu": [
            "ubuntu-client.ansible.lab"
        ], 
        "ungrouped": []
    }
}
Fuji Komalan
  • 1,979
  • 16
  • 25
  • This should be the best answer. Magic variables are the way to go. I attended DO407 of Redhat training and I am confirming this answer. – Abdennour TOUMI Apr 27 '19 at 17:56
5

Method #1 - Using Ansible

If you just want a list of the groups within a given inventory file you can use the magic variables as mentioned in a couple of the other answers.

In this case you can use the groups magic variable and specifically just show the keys() in this hash (keys + values). The keys are all the names of the groups.

NOTE: By targeting the localhost we force this command to just run against a single host when processing the inventory file.

$ ansible -i inventory/rhvh localhost -m debug -a 'var=groups.keys()'
localhost | SUCCESS => {
    "groups.keys()": "dict_keys(['all', 'ungrouped', 'dc1-rhvh', 'dc2-rhvh', 'dc3-rhvh', 'dc4-rhvh', 'dc5-rhvh', 'rhvh', 'dc1', 'dc2', 'dc3', 'dc4', 'dc5', 'production'])"
}

Method #2 - using grep & sed

You could of course just grep the contents of your inventory file too:

$ grep -E '^\[' inventory/rhvh
[dc1-rhvh]
[dc2-rhvh]
[dc3-rhvh]
[dc4-rhvh]
[dc5-rhvh]
[rhvh:children]
[dc1:children]
[dc2:children]
[dc3:children]
[dc4:children]
[dc5:children]
[production:children]

You're on the hook though for teasing out of the 2nd method's output or you can use sed to do it:

$ grep -E '^\[' inventory/rhvh | sed 's/:children//'
[dc1-rhvh]
[dc2-rhvh]
[dc3-rhvh]
[dc4-rhvh]
[dc5-rhvh]
[rhvh]
[dc1]
[dc2]
[dc3]
[dc4]
[dc5]
slm
  • 15,396
  • 12
  • 109
  • 124
3

If you're not sure if the host will actually be in the inventory you can use:

ansible -i hosts/ localhost -m debug -a 'var=groups'

-i where your inventory files are kept
-m enable module debug.
-a module arguments.

It will output the group / hosts just once and not for every host in your inventory.

Same goes for just getting the list of groups in the inventory:

ansible -i hosts/ localhost -m debug -a 'var=groups.keys()'
wowbagger
  • 590
  • 5
  • 15
0

another way to see your hosts is just to press tab after the ansible keyword.enter image description here

javidasd
  • 1,126
  • 13
  • 18
-4

Something like that? cat ~/inventory/* | grep "\[.*\]"

vai
  • 9
  • 1