Let's say you are using the vars plugin to define the foo variable.
vars:
foo: "Line One\nLine Two\nLine Three"
The debug module can be used to output the foo variable.
- name: display the content of the 'foo' variable
debug:
var: foo
Which should return the following.
TASK [display the content of the 'foo' variable]
ok: [server1.example.com] => {
"foo": "Line One\nLine Two\nLine Three"
}
regex_search can be used to perform a regular expression search for a string matching a pattern. Here is how to use regex_search, matching the first line containing "Two".
- name: display the content of the 'foo' variable, matching 'Two'
debug:
msg: "{{ foo | regex_search('.*two.*', ignorecase=True) }}"
Which should now return the following.
TASK [display the content of the 'foo' variable, matching 'Two']
ok: [server1.example.com] => {
"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.
- name: display the content of the 'foo' variable, matching 'Line'
debug:
msg: "{{ foo | regex_search('.*Line.*') }}"
One option is to set multiline to true.
- name: display the content of the 'foo' variable, matching 'Line'
debug:
msg: "{{ foo | regex_search('.*Line.*', multiline=True, ignorecase=True) }}"
Or, regex_findall can be used to return every pattern match, not just the first match.
TASK [display the content of the 'foo' variable, matching 'Line']
ok: [server1.example.com] => {
"msg": "Line One"
}
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 + '.*)') }}"