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

21 views
Skip to first unread message

ryan prihoda

unread,
Nov 13, 2018, 9:42:12 AM11/13/18
to Ansible Project
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' }



Brian Coca

unread,
Nov 13, 2018, 9:56:14 AM11/13/18
to ansible...@googlegroups.com
hosts: <= this already loops the whole playbook with each host being
an item, other than that, there is no other way.



--
----------
Brian Coca

ryan prihoda

unread,
Nov 13, 2018, 10:00:18 AM11/13/18
to ansible...@googlegroups.com
 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. 

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/a2a6PBxUG20/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7f6i50uYXD31J%2BPBfEKkTou-byGnHM1qWjEAm68%2BMG-Qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Nov 13, 2018, 10:18:50 AM11/13/18
to ansible...@googlegroups.com
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`/

--
----------
Brian Coca

ryan prihoda

unread,
Nov 13, 2018, 10:33:06 AM11/13/18
to Ansible Project
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.
Reply all
Reply to author
Forward
0 new messages