32

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
techraf
  • 64,883
  • 27
  • 193
  • 198
falsch
  • 1,425
  • 2
  • 14
  • 21

2 Answers2

47

Thats the solution:

with_items: groups['all']
falsch
  • 1,425
  • 2
  • 14
  • 21
  • 1
    Unfortunately "item" appears to be a string rather than the inventory item. How would you access item.inventory_hostname, for example? – Rick O'Shea Apr 18 '16 at 01:33
  • 3
    Perhaps you want to access hostvars? hostvars[item]["ansible_os_family"] for example. – Jeremy May 27 '16 at 21:37
1

if the purpose is to just print all the hostnames then use below command.

ansible all --list-hosts

vaibhav singhal
  • 883
  • 8
  • 9