# Refactor Playbook to loop over hosts individually

**URL:** <https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396>\
**Category:** Ansible Project\
**Created:** [August 22, 2018, 2:40pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396 "2018-08-22T14:40:06Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caleb\_Guerrero](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@Caleb\_Guerrero](https://forum.ansible.com/u/Caleb_Guerrero)\
**Post date:** [August 22, 2018, 2:40pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/1 "2018-08-22T14:40:06Z")

</div>

All,  
I am trying to refactor my playbook to run an ios command across a group of hosts… My challenge is that the command is specific to individual hosts in the group. As it stands, my playbook initial task is ran against all hosts in my group and looping over every item in my list. My intent is to run the ios command that pertains to the host and not have to have several plays or tasks in 1 playbook. Is that possible? Below is my inventory list and playbook. Feedback would be greatly appreciated!

[ios]  
Til\_INET\_EDC-2-WDC\_7kb\_Core.xxx.org  
Til-Site-RTR-1  
Til-Site-RTR-2  
Til-Site-RTR-3  
Til-Site-SW1  
Til-Site-SW2  
Til-Stratix-5700  
Til-Stratix-8000  
Til-Stratix-8300  
NIS-Til-4506  
Til-WDC-Core  
Til\_INET\_WDC-2-WDC\_7ka\_Core.xxx.org

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 22, 2018, 2:49pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/2 "2018-08-22T14:49:47Z")

</div>

Plays are what map hosts to tasks, there are a couple of other ways  
you can single out a host, but a Play is the optimal path to do this.  
Having multiple plays in a playbook was always the original intention  
(why its named playbook), but if you insist on avoiding an additional  
play, here are a couple of options:

- when: inventory\_hostname == 'specific host'  
or  
- run\_once: True, and make sure that host is always the first host in  
the selection for the play.

---

<div class="post-metadata">

**Author:** ![Arden\_Shackelford](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Arden\_Shackelford](https://forum.ansible.com/u/Arden_Shackelford)\
**Post date:** [August 22, 2018, 2:52pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/3 "2018-08-22T14:52:50Z")

</div>

Couldn’t you set what you needed as a host specific variable in your inventory file and simply call that variable in the appropriate place?

---

<div class="post-metadata">

**Author:** ![Caleb\_Guerrero](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@Caleb\_Guerrero](https://forum.ansible.com/u/Caleb_Guerrero)\
**Post date:** [August 22, 2018, 3:02pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/4 "2018-08-22T15:02:33Z")

</div>

Yeah in my scenario I would have 12+ plays for this development environment. I was mainly looking for other options that would shorten the size of the playbook if I had to run a similar command across hundreds of hosts. With the when conditional, I would still need individual plays per host correct? Also, will the run\_once action run the first item in the for loop for the first host, then my 2nd item in the for loop for the second host in the group, etc?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 22, 2018, 3:08pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/5 "2018-08-22T15:08:03Z")

</div>

No, i misread and didn't realize the 'per item/host' relation ship,  
using a variable on a task as Arden suggests would work better in that  
case.

---

<div class="post-metadata">

**Author:** ![Caleb\_Guerrero](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@Caleb\_Guerrero](https://forum.ansible.com/u/Caleb_Guerrero)\
**Post date:** [August 22, 2018, 3:18pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/6 "2018-08-22T15:18:59Z")

</div>

Actually after re-reading your post Brian. I can have 1 play with multiple tasks set to run\_once or use a when conditional; I could also just use multiple plays to accomplish this. I was hopeful there was a way to use a for loop to loop over specific hosts running the task individually per host; however I am thinking that would make this too complex.I plan to use a role for this and break up my tasks in the tasks/main.yml file.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 22, 2018, 3:41pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/7 "2018-08-22T15:41:16Z")

</div>

If I understand correctly, you would like to run the fist command on fist host, the second command on the second host and so on?

If so you could do something like this

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Restore base configs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ios\_command:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;commands:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- command: 'configure replace {{ item.command }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;prompt: '[no]'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;answer: y  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: intentory\_hostanme == item.host  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- command: nvram:Til-EDC-INET\_base  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: Til\_INET\_EDC-2-WDC\_7kb\_Core.xxx.org  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- command: nvram:Til-Site-RTR-1\_base  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;host: Til-Site-RTR-1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: ios

---

<div class="post-metadata">

**Author:** ![Caleb\_Guerrero](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@Caleb\_Guerrero](https://forum.ansible.com/u/Caleb_Guerrero)\
**Post date:** [August 22, 2018, 3:46pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/8 "2018-08-22T15:46:42Z")

</div>

Yes that is exactly what I was looking for Kai. Would I still specify the entire group ‘ios’ in the hosts section of the playbook?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 22, 2018, 3:53pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/9 "2018-08-22T15:53:07Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![Arden\_Shackelford](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@Arden\_Shackelford](https://forum.ansible.com/u/Arden_Shackelford)\
**Post date:** [August 22, 2018, 6:47pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/10 "2018-08-22T18:47:54Z")

</div>

Just for the sake of actually getting the code I had in mind out, incase anyone else might find this solution helpful.

I’m showing two

Inventory  
`  
[ios]

[Til\_INET\_EDC-2-WDC\_7kb\_Core.xxx.org](http://til_inet_edc-2-wdc_7kb_core.xxx.org/) host\_command=nvram:Til-EDC-INET\_base  
Til-Site-RTR-1 host\_command=nvram:Til-Site-RTR-1\_base  
…  
`

Playbook

`

---

<div class="post-metadata">

**Author:** ![Caleb\_Guerrero](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@Caleb\_Guerrero](https://forum.ansible.com/u/Caleb_Guerrero)\
**Post date:** [August 22, 2018, 7:15pm UTC](https://forum.ansible.com/t/refactor-playbook-to-loop-over-hosts-individually/28396/11 "2018-08-22T19:15:50Z")

</div>

Thanks for the feedback everyone! Arden, that looks like a solid solution also. Considering this code is in a dev environment, I will play around with the various methods specified.
