# Multiple when statements in loop?

**URL:** <https://forum.ansible.com/t/multiple-when-statements-in-loop/26831>\
**Category:** Ansible Project\
**Created:** [February 22, 2018, 10:28pm UTC](https://forum.ansible.com/t/multiple-when-statements-in-loop/26831 "2018-02-22T22:28:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [February 22, 2018, 10:28pm UTC](https://forum.ansible.com/t/multiple-when-statements-in-loop/26831/1 "2018-02-22T22:28:27Z")

</div>

For reference, this is a continuation of: [https://groups.google.com/forum/#!topic/ansible-project/XhU9iPXjjcw](https://groups.google.com/forum/#!topic/ansible-project/XhU9iPXjjcw)

Can you use multiple when statements in a loop? or is there a different way to approach this without having to write out a task for each item? Take the following for example. I know the syntax is wrong, but it gives you a better idea of what I mean.

`

- name: Insert audit rules  
lineinfile:  
path: “/etc/audit/audit.rules”  
line: “{{ item.line }}”  
regexp: “{{ item.regexp }}”  
state: present  
insertafter: EOF  
notify: Restart auditd  
with\_items:
- { line: ‘-w /home/GOMER/Scripts -p w -k copperfield’, regexp: ‘.\*GOMER._Scripts._’ }  
when: directories.results.0.stat.exists
- { line: ‘-w /home/PYLE/Scripts -p w -k copperfield’, regexp: ‘._PYLE_Scripts.\*’ }  
when: directories.results.1.stat.exists

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 22, 2018, 10:39pm UTC](https://forum.ansible.com/t/multiple-when-statements-in-loop/26831/2 "2018-02-22T22:39:37Z")

</div>

I think instead of doing that, you should use `with_together` and a single `when` statement that meets your needs

…  
line: “{{ item.1.line }}”  
regexp: “{{ item1.regexp }}”  
…  
when: item.0.stat.exists  
with\_together:

- “{{ directories.results }}”

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [February 22, 2018, 11:24pm UTC](https://forum.ansible.com/t/multiple-when-statements-in-loop/26831/3 "2018-02-22T23:24:30Z")

</div>

Thanks. with\_together is new to me. I am having a hard time wrapping my mind around the example you gave. Can you explain it to me?

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [February 22, 2018, 11:51pm UTC](https://forum.ansible.com/t/multiple-when-statements-in-loop/26831/4 "2018-02-22T23:51:02Z")

</div>

I created the following playbook, and this works as expected, but I don’t understand it in context to what you recommended:

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 22, 2018, 11:55pm UTC](https://forum.ansible.com/t/multiple-when-statements-in-loop/26831/5 "2018-02-22T23:55:04Z")

</div>

It allows you to loop 2 parallel sets of data.

See [http://docs.ansible.com/ansible/latest/playbooks\_loops.html#looping-over-parallel-sets-of-data](http://docs.ansible.com/ansible/latest/playbooks_loops.html#looping-over-parallel-sets-of-data)

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [February 23, 2018, 12:09am UTC](https://forum.ansible.com/t/multiple-when-statements-in-loop/26831/6 "2018-02-23T00:09:59Z")

</div>

Right, I saw that… I don’t understand why in the example you gave you are referencing item.1.\* and then using a when statement of item.0
