Ansible - regex_search (grep)

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 + '.*)') }}"


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter fdf16 in the box below so that we can be sure you are a human.




Comments


July 7th, 2021 by Gany
Thankyou so much... that's really well explained with eg. and help full for person like new to regex python, ansible. can you suggest more use case for regex in ansible. and there need a correction in this article under your eg for debug - name: display the content of the 'foo' variable debug: var: foo


July 7th, 2021 by Gany
Please help me .... Unsing regex in ansible... how can search for string from list and output should give only that particular line but not the entire list in the output


July 7th, 2021 by Jeremy (moderator)
Gany, thank you for spotting the error with the debug module. I got that corrected. Also, I think you are looking for the split parameter to split the output at new lines. Here is my article on the split parameter -> http://www.freekb.net/Article?id=2497

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |