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

Updated:   |  Ansible articles

regex_findall 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_findall, 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_findall('.*two.*', ignorecase=True) }}"

 

Which should now return the following. Notice that an array is returned, as indicated by the [ ] characters. regex_findall will return the results in an array, whereas regex_search will return the result as a string.

ok: [localhost] => {
    "msg": [
        "Line Two"
    ]
}

 

It is important to recognize that regex_search will only return the first match, whereas regex_findall will return every match. In this example, regex_search would only return the first line that contains "Line".

- name: "display the content of the 'out' variable, matching 'Line'"
  debug: 
    msg: "{{ out | regex_search('.*Line.*') }}"

 

Notice in this example the only "Line One" is returned, meaning "Line Two" and "Line Three" are not returned.

TASK [display the content of the 'out' variable, matching 'Line'']
ok: [server1.example.com] => {
    "msg": "Line One"
}

 

On the other hand, regex_findall can be used to return every pattern match, not just the first match. In this example, regex_findall would only return every match of "Line".

- name: display the content of the 'out' variable, matching 'Line'
  debug: 
    msg: "{{ out | regex_findall('.*Line.*') }}"

 

Notice in this example that "Line One" and "Line Two" and "Line Three" are returned.

TASK [display the content of the 'out' variable, matching 'Line'']
ok: [server1.example.com] => {
    "msg": "Line One"
}

 

And here is how to search for two (or more) different strings.

- name: display lines containing One or Two
  debug: 
    msg: "{{ out | regex_findall('.*One.*|.*Two.*') }}"

 

Let's say the foo variable contains a value of "Hello World". Here is how you would match all lines 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_findall('(.*' + foo + '.*)') }}"

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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