# Can I combine with\_items and when?

**URL:** <https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849>\
**Category:** Ansible Project\
**Created:** [February 26, 2018, 2:39pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849 "2018-02-26T14:39:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nico\_Sabbi](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nico\_Sabbi](https://forum.ansible.com/u/Nico_Sabbi)\
**Post date:** [February 26, 2018, 2:39pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/1 "2018-02-26T14:39:09Z")

</div>

HI,  
can I combine when: and with\_items clauses as in this example?

become: true  
become\_user: root  
tasks:

- block:
- debug:  
msg: “riga {{item}}”
- user:  
name: “{{ item.split(‘:’)[0] }}”  
home: “{{ item.split(‘:’)[1] }}”  
when: item search(“:”)  
with\_items: [“abc:def”, “yyy.zzz”]

The sad output is the following, indicating that “item” is undefined in the loop.

TASK [debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {“msg”: “The task includes an option with an undefined variable. The error was: ‘item’ is undefined\n\nThe error appears to have been in ‘/home/nico2601/block.yml’: line 25, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - block:\n - debug:\n ^ here\n\nexception type: \<class ‘ansible.errors.AnsibleUndefinedVariable’\>\nexception: ‘item’ is undefined”}  
to retry, use: --limit @/home/nico2601/block.retry

What I want to do is ensuring that the user before the : is set only when the string that I’m looping over does contain :  
What’s the right way to do it?  
Thanks.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 26, 2018, 3:38pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/2 "2018-02-26T15:38:24Z")

</div>

> HI,  
> can I combine when: and with\_items clauses as in this example?
> 
> &nbsp;&nbsp;become: true  
> &nbsp;&nbsp;become\_user: root  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- block:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: "riga {{item}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- user:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ item.split(':')[0] }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;home: "{{ item.split(':')[1] }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item search(":")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: ["abc:def", "yyy.zzz"]
> 
> The sad output is the following, indicating that "item" is undefined in  
> the loop.

when is executing before the loop so that's the reason this will not work.

> TASK [debug]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> fatal: [localhost]: FAILED! =\> {"msg": "The task includes an option with an  
> undefined variable. The error was: 'item' is undefined\n\nThe error appears  
> to have been in '/home/nico2601/block.yml': line 25, column 9, but may\nbe  
> elsewhere in the file depending on the exact syntax problem.\n\nThe  
> offending line appears to be:\n\n - block:\n - debug:\n ^  
> here\n\nexception type: \<class  
> 'ansible.errors.AnsibleUndefinedVariable'\>\nexception: 'item' is undefined"}  
> to retry, use: --limit @/home/nico2601/block.retry
> 
> What I want to do is ensuring that the user before the : is set only when  
> the string that I'm looping over does contain :  
> What's the right way to do it?

If you but the user code in a file and use include\_tasks it will work.

user.yml

---

<div class="post-metadata">

**Author:** ![flowerysong1](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@flowerysong1](https://forum.ansible.com/u/flowerysong1)\
**Post date:** [February 26, 2018, 4:14pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/3 "2018-02-26T16:14:19Z")

</div>

You’ll note that this error is from the ‘debug’ task, which has neither a loop nor a ‘when’ clause. `item` is undefined because `item` has not been defined for that task.

If you were attempting to apply the loop to the entire block, that is not supported.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 26, 2018, 4:18pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/4 "2018-02-26T16:18:51Z")

</div>

You are right, I don't know what I was thinking.

So Nico, forget what I wrote.

---

<div class="post-metadata">

**Author:** ![Nico\_Sabbi](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nico\_Sabbi](https://forum.ansible.com/u/Nico_Sabbi)\
**Post date:** [February 26, 2018, 4:22pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/5 "2018-02-26T16:22:28Z")

</div>

OK, it’s working now. Thanks!

become: true  
become\_user: root  
tasks:

- block:  
#- debug:

# msg: “riga {{item}}”

- include\_tasks: user.yml  
when: item | search(“:”)  
with\_items: [“abc:/home/def”, “ghi:/home/jkh”]

But as noted by floweryoursong…

---

<div class="post-metadata">

**Author:** ![Nico\_Sabbi](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nico\_Sabbi](https://forum.ansible.com/u/Nico_Sabbi)\
**Post date:** [February 26, 2018, 4:23pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/6 "2018-02-26T16:23:45Z")

</div>

Yes, I was trying to loop the whole block, the equivalent of  
while(something)  
{  
-1  
-2  
-3  
}

So ansible can’t do it?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 26, 2018, 4:28pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/7 "2018-02-26T16:28:27Z")

</div>

You can't loop block. To loop two or more tasks you would need to put it in a file and use include\_tasks as I described.

Allowed directives for block is listed here  
[https://docs.ansible.com/ansible/latest/playbooks\_keywords.html#block](https://docs.ansible.com/ansible/latest/playbooks_keywords.html#block)

---

<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:** [February 26, 2018, 5:06pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/8 "2018-02-26T17:06:09Z")

</div>

FYI, when executes INSIDE the loop, contrary to what was mentioned above.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [February 26, 2018, 5:16pm UTC](https://forum.ansible.com/t/can-i-combine-with-items-and-when/26849/9 "2018-02-26T17:16:43Z")

</div>

Yeah, it was a brain fart on my side, don't know why since I use when and loop a lot.
