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.
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).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With