On Mon, 20 Jun 2022 02:36:37 -0700 (PDT) "[email protected]" <[email protected]> wrote:
> q:
> - 'abc<tab character here>'
> - 123
>
> - debug:
> msg: >-
> {{ q is search('abc\t') }}
There are more aspects to this question:
* It's necessary to understand the difference between 'Double-Quoted
Style' and 'Single-Quoted Style'. See
https://yaml.org/spec/1.2.2/#731-double-quoted-style
https://yaml.org/spec/1.2.2/#732-single-quoted-style
In 'Single-Quoted Style' only "'" has to be escaped. As a result, the
string 'abc\t' doesn't include TAB. It's a sequence of characters: a,
b, c, \, t. Test it. The task below
- copy:
dest: /tmp/test.txt
content: |
{{ ql.0 }}
{{ ql.1 }}
vars:
ql:
- 'abc\tx'
- 123
gives
shell> cat /tmp/test.txt
abc\tx
123
The string will include TAB if you use 'Double-Quoted Style'
- copy:
dest: /tmp/test.txt
content: |
{{ ql.0 }}
{{ ql.1 }}
vars:
ql:
- "abc\tx"
- 123
gives
shell> cat /tmp/test.txt
abc x
123
* The *search* test doesn't work with lists. It works with strings.
See
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings
Use it to test a single item of a list. For example,
- debug:
msg: "{{ ql.0 is search(regex) }}"
vars:
regex: 'abc\tx'
ql:
- "abc\tx"
- 123
gives
msg: true
Also the task below, with single-quoted *item* and escaped backslash
in *regex*, gives the same result
- debug:
msg: "{{ ql.0 is search(regex) }}"
vars:
regex: 'abc\\tx'
ql:
- 'abc\tx'
- 123
* Instead, you can "Test if a list contains a value". See
https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-if-a-list-contains-a-value
For example, both tasks below returns *True*
- debug:
msg: "{{ item in ql }}"
vars:
item: 'abc\tx'
ql:
- 'abc\tx'
- 123
- debug:
msg: "{{ item in ql }}"
vars:
item: "abc\tx"
ql:
- "abc\tx"
- 123
* If you want to use *regex* to test an item in a list use *select*.
For example,
- debug:
msg: "{{ ql|select('search', regex) }}"
vars:
regex: 'abc\\tx'
ql:
- 'abc\tx'
- 123
gives
msg:
- abc\tx
, or use it in a condition. For example,
- debug:
msg: "Regex {{ regex }} is present in the list."
when: ql|select('search', regex)|length > 0
vars:
regex: 'abc\\tx'
ql:
- 'abc\tx'
- 123
gives
msg: Regex abc\\tx is present in the list.
--
Vladimir Botka
--
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/20220620212215.4caa70a4%40gmail.com.
pgpG1w1jIddL7.pgp
Description: OpenPGP digital signature
