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

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’ }

`

`

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

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.

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`/

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.