Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list groups that host is member of?

Tags:

I have a very complex Ansible setup with thousands of servers and hundreds of groups various servers are member of (dynamic inventory file).

Is there any way to easily display all groups that a specific host is member of?

I know how to list all groups and their members:

ansible localhost -m debug -a 'var=groups' 

But I want to do this not for ALL hosts, but only for a single one.

like image 786
Petr Avatar asked Sep 22 '17 10:09

Petr


People also ask

How do I list groups in Ansible inventory?

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).

What is hosts in Ansible playbook?

Ansible works against multiple managed nodes or “hosts” in your infrastructure at the same time, using a list or group of lists known as inventory. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.

What is Ansible_host?

ansible_host is the hostname or IP address. ansible_port is the port the machine uses for SSH. ansible_user is the remote user to connect as. ansible_ssh_pass if using a password to SSH. ansible_ssh_private_key_file if you need to use multiple keys that are specific to hosts.


1 Answers

Create a playbook called 'showgroups' (executable file) containing:

#!/usr/bin/env ansible-playbook  - hosts: all   gather_facts: no   tasks:   - name: show the groups the host(s) are in     debug:       msg: "{{group_names}}" 

You can run it like this to show the groups of one particular host (-l) in your inventory (-i):

 ./showgroups -i develop -l jessie.fritz.box  
like image 181
René Pijl Avatar answered Oct 20 '22 17:10

René Pijl