# Iterate through hostname list with a local\_action

**URL:** <https://forum.ansible.com/t/iterate-through-hostname-list-with-a-local-action/12059>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [November 1, 2012, 10:24am UTC](https://forum.ansible.com/t/iterate-through-hostname-list-with-a-local-action/12059 "2012-11-01T10:24:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [November 1, 2012, 10:24am UTC](https://forum.ansible.com/t/iterate-through-hostname-list-with-a-local-action/12059/1 "2012-11-01T10:24:30Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![Daniel\_Hokka\_Zakriss](https://avatars.discourse-cdn.com/v4/letter/d/f05b48/32.png) [@Daniel\_Hokka\_Zakriss](https://forum.ansible.com/u/Daniel_Hokka_Zakriss)\
**Post date:** [November 1, 2012, 10:29am UTC](https://forum.ansible.com/t/iterate-through-hostname-list-with-a-local-action/12059/2 "2012-11-01T10:29:39Z")

</div>

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  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;- name Create snapshot  
> &nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [November 1, 2012, 10:34am UTC](https://forum.ansible.com/t/iterate-through-hostname-list-with-a-local-action/12059/3 "2012-11-01T10:34:47Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [November 1, 2012, 11:25am UTC](https://forum.ansible.com/t/iterate-through-hostname-list-with-a-local-action/12059/4 "2012-11-01T11:25:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mark\_Maas](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Mark\_Maas](https://forum.ansible.com/u/Mark_Maas)\
**Post date:** [November 1, 2012, 1:33pm UTC](https://forum.ansible.com/t/iterate-through-hostname-list-with-a-local-action/12059/5 "2012-11-01T13:33:22Z")

</div>

Excellent, this is can use!

Thanks!
