Can I loop over multipe actions?

56 views
Skip to first unread message

Peter Mooshammer

unread,
Jan 9, 2015, 3:21:14 AM1/9/15
to ansible...@googlegroups.com
Hello,

So include and with_items is depricated and I am wondering how to implement the following:

I have to create a couple of similar services:
(I know this is not working)
- name: loopy
  shell
: download something
 
template: src: dest:
  service
: name={{ service }} state=started  
  with_items
: listing


Is the only way to do this?

- name: download everything
  shell
: download something
  with_items
: listing
- name: copy templates
 
template: src: dest:
  with_items
: listing
- name: download everything
  service
: name={{ service }} state=started
  with_items
: listing

What would be a better approach?

thanks

Peter

Tomasz Kontusz

unread,
Jan 9, 2015, 3:32:11 AM1/9/15
to ansible...@googlegroups.com
The solution you've shown is the only one at the moment.

Peter Mooshammer <pmo...@gmail.com> napisał:

--
Wysłane za pomocą K-9 Mail.

Peter Mooshammer

unread,
Jan 9, 2015, 7:34:23 PM1/9/15
to ansible...@googlegroups.com
thanks for getting back to me.

Here is what i intended:

I have a list of docker services:
services:
  - servicename: zapp
    containername: zapp
    port: 5555
    protocol: tcp
    exposed: false
    image: zapp
  - servicename: xox
    containername: xox
    port: 4444
    protocol: udp
    exposed: true
    linkto: zapp
    image: xox
  

And I wanted to use a loop to create those services, but now I am  doing this:
    - { role: some-services, service: "{{ services[0] }}" }
    - { role: some-services, service: "{{ services[1] }}" }

And I wanted to do this as a loop. But maybe I am completely off. Any ideas?

Note one of the reasons I am doing this is because ansible does not support docker restarts at the moment (it will with 1.9) 

thanks

Peter

Brian Coca

unread,
Jan 9, 2015, 7:38:06 PM1/9/15
to ansible...@googlegroups.com
there is not currently a way to loop through roles, you can make the
role accept a list and have it's tasks loop over it though.

also i would advice to not use a variable named 'service' as it can
conflict with the module of the same name.


--
Brian Coca

Peter Mooshammer

unread,
Jan 9, 2015, 7:58:32 PM1/9/15
to ansible...@googlegroups.com
thanks - good point!!!

Tomasz Kontusz

unread,
Jan 10, 2015, 6:03:16 AM1/10/15
to ansible...@googlegroups.com
I'm pretty sure modules and actions live in a different name space than
variables. Or do you mean visually?

Peter Mooshammer

unread,
Jan 10, 2015, 2:29:33 PM1/10/15
to ansible...@googlegroups.com
I think Tomasz it correct, it was working. Changed it anyways so people don't get confused.


One more question. I tried:
And I wanted to use a loop to create those services, but now I am  doing this:
    - { role: some-services, service: "{{ services[0] }}", when: services[0] is defined }
    - { role: some-services, service: "{{ services[1] }}", when: services[1] is defined }

or 

  - { role: some-services, service: "{{ services[0] }}", when: "{{ services[0] }}" is defined }
 
- { role: some-services, service: "{{ services[1] }}", when: "{{ services[0] }}" is defined }

But none of that is working, this however works:
- { role: some-services, service: "{{ services[1] }}", when: simple_var is defined }

thanks 

Peter

Peter Mooshammer

unread,
Jan 10, 2015, 2:45:07 PM1/10/15
to ansible...@googlegroups.com
just an update:


  - name: debug {{ services[0]['hostname'] }}
    debug: msg={{ services[0]['hostname'] }}
    when: services[0] is defined
  - name: debug {{ services[3]['hostname'] }}
    debug: msg={{ services[3]['hostname'] }}
    when: services[3] is defined

This, it is just not working as a conditional for roles....

Peter Mooshammer

unread,
Jan 10, 2015, 3:17:20 PM1/10/15
to ansible...@googlegroups.com
Got it:

- { role: some-services, service: "{{ services[0] }}", when: services.0 is defined }
- { role: some-services, service: "{{ services[1] }}", when: services.1 is defined }
Reply all
Reply to author
Forward
0 new messages