# help with conditional inside of a loop

**URL:** <https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921>\
**Category:** Ansible Project\
**Created:** [June 6, 2013, 11:43pm UTC](https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921 "2013-06-06T23:43:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Birdsong](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@David\_Birdsong](https://forum.ansible.com/u/David_Birdsong)\
**Post date:** [June 6, 2013, 11:43pm UTC](https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921/1 "2013-06-06T23:43:32Z")

</div>

Hi, I think this might be an easy one, but I’m unsure of how to use a loop variable inside of the ‘when’ condition.

Here’s what I thought would work, but ansible complains of a syntax error:  
[https://gist.github.com/davidbirdsong/5725778](https://gist.github.com/davidbirdsong/5725778)

Perhaps there’s another way to create a filtered list using set\_facts, which I toyed with here, but no amount of debug showed me a list of mount points:  
[https://gist.github.com/davidbirdsong/5725936](https://gist.github.com/davidbirdsong/5725936)

Here’s the relavent output from setup for one of the nodes:  
[https://gist.github.com/davidbirdsong/5725944](https://gist.github.com/davidbirdsong/5725944)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 6, 2013, 11:53pm UTC](https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921/2 "2013-06-06T23:53:17Z")

</div>

When you start a YAML line with a “{” it thinks it is a dictionary, so you have to quote the line.

So like:

when: “{{ blarg }}”

---

<div class="post-metadata">

**Author:** ![David\_Birdsong](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@David\_Birdsong](https://forum.ansible.com/u/David_Birdsong)\
**Post date:** [June 7, 2013, 12:01am UTC](https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921/3 "2013-06-07T00:01:43Z")

</div>

> When you start a YAML line with a “{” it thinks it is a dictionary, so you have to quote the line.
> 
> So like:
> 
> when: “{{ blarg }}”

Do you see anything wrong with:  
when: “{{ item[‘mount’].endswith(‘da3’) }}”

I get:  
fatal: [10.100.1.43] =\> Conditional expression must evaluate to True or False: {% if {{item[‘mount’].endswith(‘da3’)}} %} True {% else %} False {% endif %}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 7, 2013, 2:08pm UTC](https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921/4 "2013-06-07T14:08:40Z")

</div>

Right, you can’t do conditionals on with\_items, the conditional is there to decide whether to evaluate the task at all or not.

---

<div class="post-metadata">

**Author:** ![David\_Birdsong](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@David\_Birdsong](https://forum.ansible.com/u/David_Birdsong)\
**Post date:** [June 8, 2013, 12:45am UTC](https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921/5 "2013-06-08T00:45:38Z")

</div>

> Right, you can’t do conditionals on with\_items, the conditional is there to decide whether to evaluate the task at all or not.

Not to be contrary, but I hacked this into working last night. I think the output shows that the debug module was not executed for loop iterations where the when clause returned false, which is what I was hoping to achieve:

[https://gist.github.com/davidbirdsong/5733337](https://gist.github.com/davidbirdsong/5733337)

I’d love to know if there’s a better way since this is meant to drive a few steps ie.

for each mount point ending in ‘da3’:  
create and verify directory under the mount point where the dirname is constructed by ip address + some number (unique id)  
verify a symlink originating from the previous directory to another directory

kick off a command to register the directory

i just realized in typing that the loop filter actually needs to be a regex ‘^/export/hd{a-z}3$’ not a simple .endswith.

do i need to write a module or is there another way to do this natively that i’m not seeing?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [June 8, 2013, 2:42am UTC](https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921/6 "2013-06-08T02:42:20Z")

</div>

Hacked this into working means there’s a patch I can look at?

I’m open to it, but be sure to deal with the special casing for the package modules where it uses with\_items to produce a single transaction command.

---

<div class="post-metadata">

**Author:** ![David\_Birdsong](https://avatars.discourse-cdn.com/v4/letter/d/bb73d2/32.png) [@David\_Birdsong](https://forum.ansible.com/u/David_Birdsong)\
**Post date:** [June 10, 2013, 8:40pm UTC](https://forum.ansible.com/t/help-with-conditional-inside-of-a-loop/12921/7 "2013-06-10T20:40:35Z")

</div>

> Hacked this into working means there’s a patch I can look at?

No, sorry wrong use of the word hacked; I ‘fiddled’ with the with\_items + where to satisfy syntax errors and noticed that a simple debug test showed that items that returned False in the ‘where’ clause were skipped.
