Iterate through hostname list with a local_action

Dear List,

I’d like to iterate through the list of hostnames in a playbook so I can run a command on each hostname. That way I can use the “vmm” command from Vmware to make a snapshot of all hosts in the playbook before acting on them.

Something like this:

/etc/ansible/hosts:
[hostnames]
test01
testxx04
testing

playbook.yml:

Mark Maas wrote:

Dear List,

I'd like to iterate through the list of hostnames in a playbook so I can
run a command on each hostname. That way I can use the "vmm" command from
Vmware to make a snapshot of all hosts in the playbook before acting on
them.

Something like this:

/etc/ansible/hosts:
[hostnames]
test01
testxx04
testing

playbook.yml:
---
- hosts: hostnames
  tasks:
  - name Create snapshot
    local_action: command vmm -t Upgrade ${HOSTNAME_FROM_HOSTNAMES_LIST}

With local_action, you have access to all the same variables that you
typically do, and it runs once per host. So all you really need is to use
$inventory_hostname

Daniel

With local_action, you have access to all the same variables that you
typically do, and it runs once per host. So all you really need is to use
$inventory_hostname

Ah, is there a list of those variables somewhere?

There are really only two like this:

$inventory_hostname and $inventory_hostname_short (which removes the
part after the ".")

Should be covered where we discuss local_action and delegate_to in the
Advanced Playbooks docs chapter.

The other (sort of) is the $item variable used in "with_items" and
"with_fileglob", etc.

Excellent, this is can use!

Thanks!