For a backup I need to iterate over all hosts in my inventory file to be sure that the backup destination exists. My structure looks like
/var/backups/ example.com/ sub.example.com/
So I need a (built-in) variable/method to list all hosts from inventory file, not only a single group.
For groups its look like this
- name: ensure backup directories are present action: file path=/var/backups/{{ item }} state=directory owner=backup group=backup mode=0700 with_items: groups['group_name'] tags: - backup
You can use the option --list-hosts. It will show all the host IPs from your inventory file.
Open the default hosts file (/etc/ansible/hosts) using your favorite text editor to see what an Ansible hosts file looks like. By default, Ansible looks for the hosts in the /etc/ansible/hosts file. The default inventory file contains different examples you can use as references while setting up your 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).
The default location for the inventory file is /etc/ansible/hosts. You can also create project-specific inventory files in alternate locations. The inventory file can list individual hosts or user-defined groups of hosts.
Thats the solution:
with_items: groups['all']
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