why does this loop/when fail

44 views
Skip to first unread message

rjwagn...@gmail.com

unread,
Nov 18, 2022, 2:00:09 PM11/18/22
to Ansible Project
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

Andrew Latham

unread,
Nov 18, 2022, 4:05:44 PM11/18/22
to ansible...@googlegroups.com
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.





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/88a55b8c-718d-4a61-88c3-bf989525e65cn%40googlegroups.com.


--
- Andrew "lathama" Latham -

Andrew Latham

unread,
Nov 18, 2022, 4:59:47 PM11/18/22
to ansible...@googlegroups.com


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

rjwagn...@gmail.com

unread,
Nov 21, 2022, 8:01:15 AM11/21/22
to Ansible Project
Wow.  Never would have thought to try that.  Escaping in YAML/Jinja is such an unintuitive mess.  Thanks so much, Andrew.

Stefan Hornburg (Racke)

unread,
Nov 21, 2022, 9:52:39 AM11/21/22
to ansible...@googlegroups.com
On 21/11/2022 14:01, rjwagn...@gmail.com wrote:
> 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
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/88a55b8c-718d-4a61-88c3-bf989525e65cn%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/88a55b8c-718d-4a61-88c3-bf989525e65cn%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
>
>
> --
> - Andrew "lathama" Latham -
>
>
>
> --
> - Andrew "lathama" Latham -
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/a54f7244-663e-42d5-bc37-ef23d3960e5en%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/a54f7244-663e-42d5-bc37-ef23d3960e5en%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Automation expert - Ansible and friends
Linux administrator & Debian maintainer
Perl Dancer & conference hopper

Rob Wagner

unread,
Nov 28, 2022, 9:44:52 AM11/28/22
to ansible...@googlegroups.com
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.

You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/Sh14YJbuf04/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/b155382a-2483-45ff-acd8-491051a503a2%40linuxia.de.
Reply all
Reply to author
Forward
0 new messages