Hi - Does anyone (who understands how backslashes work in Ansible/YAML) 
know why both of the following tasks work:

(ansible2_15_8) rowagn@localhost:~#> cat d.yml
- hosts: all
  gather_facts: no
  vars:
    s: 'This is a string containing 1 and 2.'
    t:
      - p1_xyz
      - p2_xyz
      - p4_xyz

  tasks:
  - name: single backslash
    debug:
      msg: '{{ item }} is in s'
    loop: '{{ t }}'
    when: ( item | regex_replace('^p(\d+).*$', '\\1') ) in s

  - name: double backslash
    debug:
      msg: '{{ item }} is in s'
    loop: '{{ t }}'
    when: ( item | regex_replace('^p(\\d+).*$', '\\1') ) in s

(ansible2_15_8) rowagn@localhost:~#> ansible-playbook -i l d.yml

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

TASK [single backslash] 
*****************************************************************************************************************************************
ok: [localhost] => (item=p1_xyz) => {
    "msg": "p1_xyz is in s"
}
ok: [localhost] => (item=p2_xyz) => {
    "msg": "p2_xyz is in s"
}
skipping: [localhost] => (item=p4_xyz)

TASK [double backslash] 
*****************************************************************************************************************************************
ok: [localhost] => (item=p1_xyz) => {
    "msg": "p1_xyz is in s"
}
ok: [localhost] => (item=p2_xyz) => {
    "msg": "p2_xyz is in s"
}
skipping: [localhost] => (item=p4_xyz)

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


The tasks are extracting the number from the strings in list t and then 
looking for that number in string s.  What is strange is the second example 
at 
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/regex_replace_filter.html#examples
 
indicates the backslashes in both parameters need to be doubled, but the 
above testing shows double backslashes are not required in the first 
parameter (they are required in the second parameter).

Thanks
Rob

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/bd36f3ef-9b36-4af1-aede-9435535d9fb0n%40googlegroups.com.

Reply via email to