Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all ansible hosts from inventory file for a task

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 
like image 596
falsch Avatar asked Dec 29 '13 18:12

falsch


People also ask

How can I get a list of hosts from an Ansible inventory file?

You can use the option --list-hosts. It will show all the host IPs from your inventory file.

How to see Ansible hosts?

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.

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

Where is the Ansible hosts file?

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.


1 Answers

Thats the solution:

with_items: groups['all'] 
like image 57
falsch Avatar answered Sep 23 '22 04:09

falsch