"filter" functionality for 'when:' conditional

32 views
Skip to first unread message

Patrick Glomski

unread,
Jun 1, 2015, 5:14:33 PM6/1/15
to ansible...@googlegroups.com
When running a task with multiple items using 'with_items', the results are registered in a variable under 'results' as a list. Is there any way to apply a filter such as 'any' or 'all' to that list in a conditional on the next task's execution? The only way I can do something currently is to perform the following task several times (once for every item in the list that evaluates as true for my condition). I'd rather not notify a handler, because the following tasks depend on this task being evaluated.

simple example I just made up to illustrate what I want:

###############################################
- name: run a script to configure network interfaces
  lineinfile:
    - dest: /etc/sysconfig/network-scripts/ifcfg-{{item}}"
    - ...
    - some arbitrary keywords...
    - ...
  with_items:
    - eth0
    - eth1
  register: configured_network

- name: restart the network service. what I currently do (potentially restarts network twice)
  service: name=network state=restarted
  when: item.changed == true
  with_items: configured_network.results

- name: mount a share that is on the newly configured network and wasn't available before
  mount: ...
###############################################

I would like to restart the service ONE time on check. Something like:

- name: restart the network service
  service: name=network state=restarted
  when: any(item.changed == true for item in configured_network.results)

Apologies for the long-winded examples, but I didn't know how else to convey what I'm interested in. Is this kind of functionality possible???

Thanks,
Patrick


Brian Coca

unread,
Jun 1, 2015, 5:46:20 PM6/1/15
to ansible...@googlegroups.com
i think this is the droid you are looking for:
http://jinja.pocoo.org/docs/dev/templates/#map
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, 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/602ca00d-f879-4380-8d8c-60e15663aa29%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

Patrick Glomski

unread,
Jun 2, 2015, 7:58:34 AM6/2/15
to ansible...@googlegroups.com
Thanks so much for your help, Brian! I'll try to RTFM next time.

Patrick
Reply all
Reply to author
Forward
0 new messages