Bootstrap FreeKB - Ansible - regular expression search using regex_search
Ansible - regular expression search using regex_search

Updated:   |  Ansible articles

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 Buy Me A Coffee



Comments


July 07 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 07 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 07 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

June 22 2022 by Jeremy (moderator)
I finally got around to updating this article based on some of the comments. Hopefully the article is a bit easier to follow now.

April 09 2023 by Suraj
Thank you. The article was helpful, I have bookmarked this article.

November 08 2023 by Anshul Soni
Hi, Thanks for the wonderful article on ansible regex. could you please me to generate regex for string. SYSADM\\t\\t\\t bswocpsappq01 \\t\\t\\t\\t\\t\\t\\t 8 in this last digit should be in between [0-4] and bswocpsappq01 this can be anything SYSADM is constant

Add a Comment


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