# Understanding the "by design" behavior of "when" conditionals with "with\_items"

**URL:** <https://forum.ansible.com/t/understanding-the-by-design-behavior-of-when-conditionals-with-with-items/23617>\
**Category:** Ansible Project\
**Created:** [February 2, 2017, 11:49am UTC](https://forum.ansible.com/t/understanding-the-by-design-behavior-of-when-conditionals-with-with-items/23617 "2017-02-02T11:49:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RaSca](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@RaSca](https://forum.ansible.com/u/RaSca)\
**Post date:** [February 2, 2017, 11:49am UTC](https://forum.ansible.com/t/understanding-the-by-design-behavior-of-when-conditionals-with-with-items/23617/1 "2017-02-02T11:49:01Z")

</div>

Hi everybody,  
following this bug [1] I’ve opened yesterday and the suggestion from Brian I want to understand in depth what is considered by design inside Ansible about “when”.

Now, leaving aside the usage of “when” inside roles, let’s start with this simple example:

`

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 2, 2017, 1:44pm UTC](https://forum.ansible.com/t/understanding-the-by-design-behavior-of-when-conditionals-with-with-items/23617/2 "2017-02-02T13:44:10Z")

</div>

This will avoid any and all problems with my\_key definition.

&nbsp;&nbsp;&nbsp;with\_file: "{{ (my\_key is defined)|ternary(my\_key|default('') +  
'.pub', )}}"

---

<div class="post-metadata">

**Author:** ![RaSca](https://avatars.discourse-cdn.com/v4/letter/r/cc9497/32.png) [@RaSca](https://forum.ansible.com/u/RaSca)\
**Post date:** [February 2, 2017, 3:21pm UTC](https://forum.ansible.com/t/understanding-the-by-design-behavior-of-when-conditionals-with-with-items/23617/3 "2017-02-02T15:21:22Z")

</div>

Hi Brian,  
thanks for your answer. I changed the yaml as you suggested:

`

- name: Configure key on remote host  
authorized\_key:  
user: root  
key: “{{ item }}”  
with\_file: “{{ (my\_key is defined)|ternary(my\_key|default(‘’) + ‘.pub’, )}}”  
become: true  
when: do\_the\_test|bool  
`

But it stills fail to locate the file:

`  
TASK [bug\_20919\_bis : Configure key on remote host] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /home/rasca/Red-Hat/ansible/tests/roles/bug\_20919\_bis/tasks/main.yml:12  
[WARNING]: Unable to find ‘rasca.pub’ in expected paths.

File lookup using None as file  
fatal: [192.168.122.100]: FAILED! =\> {  
“failed”: true,  
“msg”: “could not locate file in lookup: rasca.pub”  
}  
to retry, use: --limit @/home/rasca/Red-Hat/ansible/tests/bug\_20919\_bis.retry  
`

So the problem here does not seem to be related at what is inside the variable passed to “with\_file”, but to the fact that independently from what’s passed and from the “when:” variable, ansible looks for the file.

Thanks,

Raoul

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 6, 2017, 3:44pm UTC](https://forum.ansible.com/t/understanding-the-by-design-behavior-of-when-conditionals-with-with-items/23617/4 "2017-02-06T15:44:01Z")

</div>

The with\_ section gets executed BEFORE the when, so you cannot use  
when to avoid the with\_ from being evaluated, the when runs once per  
item (inside the with\_ loop) to allow the task to be conditional PER  
ITEM.

This is as documented :  
[http://docs.ansible.com/ansible/playbooks\_conditionals.html#loops-and-conditionals](http://docs.ansible.com/ansible/playbooks_conditionals.html#loops-and-conditionals)
