# Looping through hosts in a playbook

**URL:** <https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856>\
**Category:** Ansible Project\
**Created:** [November 25, 2015, 7:24pm UTC](https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856 "2015-11-25T19:24:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Sherman1](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Roger\_Sherman1](https://forum.ansible.com/u/Roger_Sherman1)\
**Post date:** [November 25, 2015, 7:24pm UTC](https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856/1 "2015-11-25T19:24:02Z")

</div>

I’m working on a playbook, that will be deploying a java app to four hosts. I want it to push the app out to each node, making sure the correct directories exist to push the new version to - that’s the easy part, and it can do all four of the nodes at once, no issue there. But then I want it to loop through the list of hosts, taking them out of the load balancer, changing the app version that is linked on each node, and then put them back into the load balancer, one at a time. All of the tasks are easily enough performed, but the looping part I’m not sure about. I’ve googled looping through hosts, but mainly I’m finding articles on how to loop through hosts in a template.

Is it possible to run through hosts in a loop as a task, or alternatively, can the tasks be run from a template? I suppose I could just set the whole playbook to do just one host at a time (serial: 1), or do multiple ansible runs, limiting the run to one host at a time, but that seems a bit inelegant - it seems like the tasks that can safely be run in parallel should be, and the tasks that can’t should be looped. Am I overthinking this?

---

<div class="post-metadata">

**Author:** ![Alexander\_Artemenko](https://avatars.discourse-cdn.com/v4/letter/a/71e660/32.png) [@Alexander\_Artemenko](https://forum.ansible.com/u/Alexander_Artemenko)\
**Post date:** [November 26, 2015, 5:12am UTC](https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856/2 "2015-11-26T05:12:24Z")

</div>

Roger, did you try to use “with\_items” ([http://docs.ansible.com/ansible/playbooks\_loops.html#standard-loops](http://docs.ansible.com/ansible/playbooks_loops.html#standard-loops))?

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [November 26, 2015, 12:40pm UTC](https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856/3 "2015-11-26T12:40:07Z")

</div>

You might benefit from looking at [http://radar.oreilly.com/2013/07/zero-downtime-application-updates-with-ansible.html](http://radar.oreilly.com/2013/07/zero-downtime-application-updates-with-ansible.html)

that seems close to what you’r trying to achiev

---

<div class="post-metadata">

**Author:** ![Paul\_Markham](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Paul\_Markham](https://forum.ansible.com/u/Paul_Markham)\
**Post date:** [November 27, 2015, 3:27am UTC](https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856/4 "2015-11-27T03:27:56Z")

</div>

You can have multiple plays in a playbook. Put the tasks that can be done in parallel in one play and the tasks that need to be done one at a time in another play with “serial: 1”.

---

<div class="post-metadata">

**Author:** ![Roger\_Sherman1](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Roger\_Sherman1](https://forum.ansible.com/u/Roger_Sherman1)\
**Post date:** [November 27, 2015, 8:18pm UTC](https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856/5 "2015-11-27T20:18:42Z")

</div>

Hi Alexander,

I don’t think “with\_items” is the way to go here - if I use “with\_items”, it’s going to try to execute on all of the hosts at once, unless it can be used in a method that I haven’t thought of.

---

<div class="post-metadata">

**Author:** ![Roger\_Sherman1](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Roger\_Sherman1](https://forum.ansible.com/u/Roger_Sherman1)\
**Post date:** [November 27, 2015, 8:19pm UTC](https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856/6 "2015-11-27T20:19:25Z")

</div>

Thanks Philippe, I’ll look at that.

---

<div class="post-metadata">

**Author:** ![Roger\_Sherman1](https://avatars.discourse-cdn.com/v4/letter/r/c68b51/32.png) [@Roger\_Sherman1](https://forum.ansible.com/u/Roger_Sherman1)\
**Post date:** [November 27, 2015, 8:20pm UTC](https://forum.ansible.com/t/looping-through-hosts-in-a-playbook/19856/7 "2015-11-27T20:20:18Z")

</div>

Oh, that’s an interesting idea, thanks Paul - I’ll do some experimenting with that.
