Nicola Musatti
Nicola Musatti

Reputation: 18218

How to check the results of all the iterations of an Ansible looping task in a single `when` clause

Consider this task sequence from a role of mine:

- command: "svn status {{dest}}/{{client}}"
  changed_when: false
  register: svn_status
  when: svn_checkout is not skipped

- command: "svn add {{dest}}/{{client}}/{{svn_dir}}"
  loop:
    - dir_a
    - dir_b
    - dir_b
  loop_control:
    loop_var: svn_dir
  register: svn_add
  when: svn_status is not skipped and svn_status.stdout != ""

- command: "svn commit {{dest}}/{{client}}
  --username user --password password
  --non-interactive
  -m 'Configuration for {{client}}'"
  when: svn_add is changed

In the last when condition I would like to check that all iterations of the preceding loop where skipped. In plain Python this would be something like

all([ r.skipped for r in svn_add.results ])

but Jinja2 doesn't seem to accept it. Is there an alternative way?

Upvotes: 0

Views: 142

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

Use rejectattr filter to remove skipped elements and count result:

when: svn_add.results | rejectattr('skipped','defined') | list | count == 0

in English: when number of elements that have no skipped attribute is zero.

Upvotes: 1

Related Questions