# Convert with\_X to loop: but when: condition being ignored?

**URL:** <https://forum.ansible.com/t/convert-with-x-to-loop-but-when-condition-being-ignored/36499>\
**Category:** Ansible Project\
**Created:** [March 31, 2022, 5:43pm UTC](https://forum.ansible.com/t/convert-with-x-to-loop-but-when-condition-being-ignored/36499 "2022-03-31T17:43:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bob](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bob/32/9260_2.png) [@bob](https://forum.ansible.com/u/bob)\
**Post date:** [March 31, 2022, 5:43pm UTC](https://forum.ansible.com/t/convert-with-x-to-loop-but-when-condition-being-ignored/36499/1 "2022-03-31T17:43:04Z")

</div>

I do not know if this is a bug or I do not know how to use when: with the loop:

The tasks

- name: debug backuppc\_client\_mysql\_dump  
ansible.builtin.debug:  
msg: “{{ backuppc\_client\_mysql\_dump }}”  
when: backuppc\_client\_mysql\_dump is defined  
tags: backuppc

- name: set up backuppc user MySQL dump authorized\_keys  
ansible.posix.authorized\_key:  
user: “{{ backuppc\_client\_item.0.username }}”  
state: “present”  
key\_options: “{{ backuppc\_client\_item.0.key\_options }}”  
key: “{{ backuppc\_client\_item.1 }}”  
manage\_dir: true  
loop: “{{{backuppc\_client\_mysql\_dump|subelements(‘authorized\_keys’) }}”  
loop\_control:  
loop\_var: backuppc\_client\_item  
when: backuppc\_client\_mysql\_dump is defined  
tags: backuppc

Ansible run  
TASK [backuppc-client : debug backuppc\_client\_mysql\_dump] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [docker.\*.com]

TASK [backuppc-client : set up backuppc user MySQL dump authorized\_keys] \*\*\*\*\*\*\*  
fatal: [docker.\*.com]: FAILED! =\> {“msg”: “obj must be a list of dicts or a nested dict”}

The debug task is skipped because backuppc\_client\_mysql\_dump is not defined. I expect the MySQL dump authorized\_keys task to be skipped too but it’s not.

Any help?

---

<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:** [March 31, 2022, 5:45pm UTC](https://forum.ansible.com/t/convert-with-x-to-loop-but-when-condition-being-ignored/36499/2 "2022-03-31T17:45:57Z")

</div>

`when` statements with a loop are evaluated for every iteration, and not before the loop happens.

This is documented at: [https://docs.ansible.com/ansible-core/devel/user\_guide/playbooks\_conditionals.html#using-conditionals-in-loops](https://docs.ansible.com/ansible-core/devel/user_guide/playbooks_conditionals.html#using-conditionals-in-loops)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [March 31, 2022, 8:32pm UTC](https://forum.ansible.com/t/convert-with-x-to-loop-but-when-condition-being-ignored/36499/3 "2022-03-31T20:32:27Z")

</div>

So one solution would be to put both tasks in a block, and put the `when: backuppc_client_mysql_dump is defined` condition on the block itself.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 1, 2022, 2:02pm UTC](https://forum.ansible.com/t/convert-with-x-to-loop-but-when-condition-being-ignored/36499/4 "2022-04-01T14:02:10Z")

</div>

blocks are not a way to skip tasks, a 'when' on a block is just for  
tasks inside the block to inherit.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [April 1, 2022, 2:13pm UTC](https://forum.ansible.com/t/convert-with-x-to-loop-but-when-condition-being-ignored/36499/5 "2022-04-01T14:13:21Z")

</div>

Ouch! Then you’d have to put the tasks into their own tasks file, and use `include_tasks` …

Or give `backuppc_client_mysql_dump` a reasonable `|default(...)` so that the looped task(s) would do no harm.

> ```auto
> blocks are not a way to skip tasks, a 'when' on a block is just fortasks inside the block to inherit.
> 
> ```
> 
> > ```auto
> > So one solution would be to put both tasks in a block, and put the `when: backuppc_client_mysql_dump is defined` condition on the block itself.
> > 
> > ```

​
