Search code examples
ansibleansible-facts

How to show all the host names of hosts within a specific OS in Ansible


Is there any command in Ansible to collect the hostnames of hosts with OS Debain? The file hosts contains no groups!

So a simple command to see the hostnames of hosts containing Debain.


Solution

  • The setup module, which is implicitly called in any playbook via the gather_facts mechanism, contains some output that you could use.
    Look for example for the ansible_distribution fact, which should contain Debian on the hosts you are looking for.

    If all you want is that list once, you could invoke the module directly, using the ansible command and grep:

    ansible all -m setup -a 'filter=ansible_distribution' | grep Debian

    If you want to use that information dynamically in a playbook, you could use this pattern:

    ---
     - hosts: all
       tasks:
         - name: Do something on Debian
           debug:
             msg: I'm a Debian host
           when: ansible_distribution == 'Debian'