
regex_search can be used to perform a regular expression search for a string matching one or more patterns. Let's say foo.txt contains the following.
Line One
Line Two
Line Three
In this example, the shell module and register parameter are used to store the contents of foo.txt in the "out" variable and the debug module is used to print the contents of the out variable.
---
- hosts: localhost
tasks:
- name: store the contents of foo.txt in the 'out' variable
shell: cat /home/john.doe/foo.txt
register: out
- debug:
var: out
...
Which should return the following.
ok: [localhost] => {
"out": {
"changed": true,
"cmd": "cat /home/john.doe/foo.txt",
"delta": "0:00:00.018949",
"end": "2022-07-12 03:41:47.957318",
"failed": false,
"rc": 0,
"start": "2022-07-12 03:41:47.938369",
"stderr": "",
"stderr_lines": [],
"stdout": "Line One\nLine Two\nLine Three",
"stdout_lines": [
"Line One",
"Line Two",
"Line Three"
]
}
}
Here is how to use regex_search, matching each line containing "Two".
AVOID TROUBLE
Notice that out.stdout is used. You wouldn't want to use out.stdout_lines since stdout_lines contains a list/array. You want to use stdout since stdout is a string object.
- name: "display the content of the 'out' variable, matching 'Two'"
debug:
msg: "{{ out.stdout | regex_search('.*two.*', ignorecase=True) }}"
Which should now return the following.
ok: [localhost] => {
"msg": "Line Two"
}
By default, regex_search will only return the first match. In this example, the first line that contains "Line" will be returned, meaning "Line Two" and "Line Three" are not returned.
---
- hosts: localhost
vars:
foo: "Line One\nLine Two\nLine Three"
tasks:
- name: display the content of the 'foo' variable, matching 'Line'
debug:
msg: "{{ foo | regex_search('.*Line.*') }}"
...
One option is to set multiline to true. Or, regex_findall can be used to return every pattern match, not just the first match.
- name: display the content of the 'foo' variable, matching 'Line'
debug:
msg: "{{ foo | regex_search('.*Line.*', multiline=True, ignorecase=True) }}"
Which should return the results as an array/list.
ok: [localhost] => {
"msg": [
"Line One",
"Line Two",
"Line Three"
]
}
Let's say the foo variable contains a value of "Hello World". Here is how you would search for the first line in the "out" variable that contain Hello World, using the foo variable.
- name: display the content of the 'out' variable, matching the 'foo' variable
debug:
msg: "{{ out | regex_search('(.*' + foo + '.*)') }}"
And here is another example based on something I happened upon once.
---
- hosts: localhost
vars:
fruits: "apple1 orange2 banana3 grapes4"
pattern: mypattern
tasks:
- debug:
msg: "{{ out | regex_search(''+pattern+'[0-9]') }}"
...
And here is how you can do something using the when parameter.
---
- hosts: localhost
vars:
foo: "Line One\nLine Two\nLine Three"
tasks:
- debug:
msg: The foo variable does contain 'Line'
when: foo | regex_search('.*Line.*')
- debug:
msg: The foo variable does NOT contain 'bar'
when: not foo | regex_search('.*bar.*')
...
Did you find this article helpful?
If so, consider buying me a coffee over at