# why does this loop/when fail

**URL:** <https://forum.ansible.com/t/why-does-this-loop-when-fail/37365>\
**Category:** Ansible Project\
**Created:** [November 18, 2022, 7:00pm UTC](https://forum.ansible.com/t/why-does-this-loop-when-fail/37365 "2022-11-18T19:00:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [November 18, 2022, 7:00pm UTC](https://forum.ansible.com/t/why-does-this-loop-when-fail/37365/1 "2022-11-18T19:00:09Z")

</div>

Hey all - I think I’m losing my mind. Can anyone explain why every iteration of task 3 is being skipped (compare w/ task 2, especially)?

(ansible2\_12\_8) rowagn@mlb656 client % cat d.yml

- hosts: all  
gather\_facts: no  
vars:  
s: ‘1 2 3’  
t: p1\_xyz  
t\_list:
- p1\_xyz
- p2\_xyz

tasks:

- name: task 1  
debug:  
msg: ‘{{ ( t | regex\_replace(“^p(\d+).\*$”, “\1”) ) in s }}’

- name: task 2  
debug:  
msg: ‘{{ ( item | regex\_replace(“^p(\d+).\*$”, “\1”) ) in s }}’  
loop: “{{ t\_list }}”

- name: task 3  
debug:  
msg: “{{ item }} is in s”  
loop: “{{ t\_list }}”  
when: ( item | regex\_replace(“^p(\d+).\*$”, “\1”) ) in s  
(ansible2\_12\_8) rowagn@mlb656 client % ansible-playbook d.yml -i ~/localhost

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [task 1] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: true  
}

TASK [task 2] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=p1\_xyz) =\> {  
“msg”: true  
}  
ok: [localhost] =\> (item=p2\_xyz) =\> {  
“msg”: true  
}

TASK [task 3] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost] =\> (item=p1\_xyz)  
skipping: [localhost] =\> (item=p2\_xyz)  
skipping: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

I’ve started at this for hours.

Rob

---

<div class="post-metadata">

**Author:** ![Andrew\_Latham](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Andrew\_Latham](https://forum.ansible.com/u/Andrew_Latham)\
**Post date:** [November 18, 2022, 9:05pm UTC](https://forum.ansible.com/t/why-does-this-loop-when-fail/37365/2 "2022-11-18T21:05:17Z")

</div>

Rob

Trying to follow along, So on Task 3 you want to iterate over the list t\_list and match if the digit is in variable s? First thought is that you are testing if an int is a string.

---

<div class="post-metadata">

**Author:** ![Andrew\_Latham](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Andrew\_Latham](https://forum.ansible.com/u/Andrew_Latham)\
**Post date:** [November 18, 2022, 9:59pm UTC](https://forum.ansible.com/t/why-does-this-loop-when-fail/37365/3 "2022-11-18T21:59:19Z")

</div>

ok, here it is… should be \1 vs \1 in your replacement and test(is) on string contents(in)

- name: task 3  
debug:  
msg: “{{ item }} is in the s”  
loop: “{{ t\_list }}”  
when: ( item | regex\_replace(“^p(\d+).\*$”, “\1”) ) is in s

- name: task 3.orig  
debug:  
msg: “{{ item }} is in s”  
loop: “{{ t\_list }}”  
when: ( item | regex\_replace(“^p(\d+).\*$”, “\1”) ) in s

TASK [task 3] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> (item=p1\_xyz) =\> {  
“msg”: “p1\_xyz is in the s”  
}  
ok: [localhost] =\> (item=p2\_xyz) =\> {  
“msg”: “p2\_xyz is in the s”  
}

TASK [task 3.orig] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost] =\> (item=p1\_xyz)  
skipping: [localhost] =\> (item=p2\_xyz)  
skipping: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

I did this in a hurry, so there may be mistakes or more awesome ways of doing this. YMMV

---

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [November 21, 2022, 1:01pm UTC](https://forum.ansible.com/t/why-does-this-loop-when-fail/37365/4 "2022-11-21T13:01:15Z")

</div>

Wow. Never would have thought to try that. Escaping in YAML/Jinja is such an unintuitive mess. Thanks so much, Andrew.

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [November 21, 2022, 2:51pm UTC](https://forum.ansible.com/t/why-does-this-loop-when-fail/37365/5 "2022-11-21T14:51:42Z")

</div>

> Wow. Never would have thought to try that. Escaping in YAML/Jinja is such an unintuitive mess. Thanks so much, Andrew.

It is easier though when you use single quotes for the arguments of the regex\_replace filter.

Regards

```
        Racke

```

---

<div class="post-metadata">

**Author:** ![rjwagner.dba](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@rjwagner.dba](https://forum.ansible.com/u/rjwagner.dba)\
**Post date:** [November 28, 2022, 2:44pm UTC](https://forum.ansible.com/t/why-does-this-loop-when-fail/37365/6 "2022-11-28T14:44:30Z")

</div>

Thanks Racke, but I just tried:

- name: task 3  
debug:  
msg: “{{ item }} is in s”  
loop: “{{ t\_list }}”  
when: ( item | regex\_replace(‘^p(\d+).\*$’, ‘\1’) ) in s

It fails. Only works with \1.
