# What is the proper way to add a loop for a whole playbook ?

**URL:** <https://forum.ansible.com/t/what-is-the-proper-way-to-add-a-loop-for-a-whole-playbook/29020>\
**Category:** Ansible Project\
**Created:** [November 13, 2018, 2:42pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-add-a-loop-for-a-whole-playbook/29020 "2018-11-13T14:42:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan\_prihoda](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@ryan\_prihoda](https://forum.ansible.com/u/ryan_prihoda)\
**Post date:** [November 13, 2018, 2:42pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-add-a-loop-for-a-whole-playbook/29020/1 "2018-11-13T14:42:12Z")

</div>

Normally I’d add my loop at the end of each task but my playbook is growing rather long. How do I set variables in the playbook so that each task will loop though them ? Something like this

`

`

- name: archive logs  
hosts: localhost  
vars:  
date: “{{ lookup(‘pipe’, ‘date -d "-1 month" +"%m-%Y"’) }}”  
loop:
- { customer: ‘customer1’, archive: ‘customer1.tgz’, bucket: ‘customer1-log-archive’ }
- { customer: ‘customer2’, archive: ‘customer2.tgz’, bucket: ‘customer2-log-archive’ }

`

`

---

<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:** [November 13, 2018, 2:55pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-add-a-loop-for-a-whole-playbook/29020/2 "2018-11-13T14:55:50Z")

</div>

hosts: \<= this already loops the whole playbook with each host being  
an item, other than that, there is no other way.

---

<div class="post-metadata">

**Author:** ![ryan\_prihoda](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@ryan\_prihoda](https://forum.ansible.com/u/ryan_prihoda)\
**Post date:** [November 13, 2018, 2:59pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-add-a-loop-for-a-whole-playbook/29020/3 "2018-11-13T14:59:53Z")

</div>

This play is only ran locally. I meant to loop through the customers so that I could have {{ item.customer }} {{ item.archive }} {{ item.bucket }} available as variables in each task in my playbook without having to add the same loop at the end of each task.

---

<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:** [November 13, 2018, 3:18pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-add-a-loop-for-a-whole-playbook/29020/4 "2018-11-13T15:18:26Z")

</div>

Create an inventory in which the 'hosts' are customers and associate  
the variables to each host, then have the play run `hosts: customer`  
and `delegate_to: localhost`/

---

<div class="post-metadata">

**Author:** ![ryan\_prihoda](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@ryan\_prihoda](https://forum.ansible.com/u/ryan_prihoda)\
**Post date:** [November 13, 2018, 3:33pm UTC](https://forum.ansible.com/t/what-is-the-proper-way-to-add-a-loop-for-a-whole-playbook/29020/5 "2018-11-13T15:33:05Z")

</div>

I ended up restructuring my vars a bit:  
`

- name: archive logs  
hosts: localhost  
vars:
- date: “{{ lookup(‘pipe’, ‘date -d "-1 month" +"%m-%Y"’) }}”
- customers:
- { name: ‘customer1’, archive: ‘customer1.tgz’, bucket: ‘customer1-log-archive’ }
- { name: ‘customer2’, archive: ‘customer2.tgz’, bucket: ‘customer2-log-archive’ }

`

Then I just need to add with\_items: “{{ customers }}” to the end of each task to get the loop that I wanted.

Thanks for the help.
