Ansible Regex_Search to Filter Data

Today, we are going to discover another filter of the Ansible tool which is the regex search filter. We shall discover how Ansible’s regex search functions. We will also explore how regex search might be useful when using Ansible to find a matching string or character in the Ansible playbook.

In Ansible, reg-ex means regular expression and the search stands for to find something. So, the regex_search filter in Ansible is a kind of complex scan that seeks particular sequences rather than particular words and phrases. Instead of creating many literal search queries, regex makes it possible to find a specific character string using regular expressions. To retrieve a particular sequence, regex works with a search query and metacharacters. Regular expression’s fundamental building elements are metacharacters. Finding some specific strings like security numbers, license numbers, webpages addresses, mailing addresses, registration numbers, etc that fit into specific sequences can be done using regular expressions.

Parameters Of regex_search

The parameter is a unique type of variable that is employed by any filter, plugin, etc to relate to a specific bit of data given to the filter as input value in Ansible. These chunks of information indicate the values of the parameters that will be used to execute or initiate the filter. Below are the required and optional parameters which we are going to use while using the regex_serachh filter in the Ansible playbook.

Required Parameters

The following are the parameters that are required so that we can find the pattern in a provided string.

Input string: We will take input as a string so that we can find the pattern against it.

Optional Parameters

These are the parameters that we will use if there is a need while searching for a specific pattern from the input string.

Regex string: regex is a regular expression that contains a string and specifies the matching pattern while filtering.

Ignorecase: It is a Boolean parameter. If the regular expression string is true, it will make the search case-sensitive and if it is not true then it will be forced to be case-insensitive.

Multiline: It is also a Boolean parameter of regex search in Ansible. If the scan is True, the search over line endings and if it is False, it will not search over the line ending.

Prerequisites of Ansible Filter regex_Search

If you would like to keep going in the regex search, then we will assume that you meet the following prerequisites that are given below:

  • While we want to work in the Ansible tool and want to implement the regex search filter then we will require that you must have Ansible-related software on your system. For this tutorial, we are using Ansible 2.12 or a later version.
  • To execute the filter on remote servers then we need a controller and here Ansible server is the main controller that configures the regex filter in the local host server.

Let us just delve deeper into the regex search filter and check how Ansible puts it into effect by using a few situations.

Example 01: Matching the “Two” string by using Regex Search Filter

Here is the very first example where we will find the “two strings from the sample document that was already created and contains some strings in it. When we want to execute filters in Ansible, we need a playbook where we write the tasks. To do so, the following is the statement we are going to use to create the playbook in Ansible.

[root@master ansible]# nano regex_search.yml

Now, the playbook is initiated in Ansible’s new terminal. Let us start writing the required statements in the playbook so that we can run the filter. First, we will write the host device name where we want to configure the regex search filter. We are using the local host to implement the filter. We will only get the required data of the local host so we have false the gathering fact option. In the first task, we got some data from the sample.txt document and then stored it in the “out” variable that was declared by using the register option of the playbook. Then, we debug the task so that we can search the “two” string from the content of the out variable and display it on the Ansible console terminal.

- hosts: localhost

  gather_facts: false
    - name: store the contents of sample.txt in the 'out' variable
      shell: cat sample.txt
      register: out

    - name: display the content of the 'out' variable, matching 'Two'
        msg: "{{ out.stdout | regex_search('.*two.*', ignorecase=True) }}"

The next step is to perform the following line to obtain the phrase that has the string “two” in it, after writing the tasks for the playbook.

[root@master ansible]# ansible-playbook regex_search.yml

The phrase from the out variable that contains the word “two” is displayed in the output by using the regex search filter, which was successfully executed and can be seen with the ok signal.

Example 2: Show the string “file” from the Declared Variable in Ansible

Now, we are going to implement the 2nd scenario where we want to find the data from the declared variable in Ansible. First, we will again open the playbook:

[root@master ansible]# nano regex_searcg.yml

In this example, we have declared a variable name “my_var” and then we have stored content in the variable. Now, we want to get the specific string from the variable so we have to define the task in the playbook. As shown, we have also used optional parameters of the regex_search filter in the debug option at the end of the playbook. These parameters are multiline and ignorecase parameter that contains the “true” value.

- hosts: localhost

  gather_facts: false
    my_var: "Test first file\nTest second file\nTest third file"

    - name: display the content of the 'my_var' variable, matching 'file'
        msg: "{{ my_var | regex_findall('.*file.*', multiline=True, ignorecase=True) }}"

Now, we will execute the playbook and showcase the results of the playbook in the terminal:

[root@master ansible]# ansible-playbook regex_search.yml

Here are the desired results displayed below:


We went into great detail on what a regex search filter in Ansible is and how to use the filter in the Ansible playbook and what we will receive in return. To understand the Ansible regex search filter properly, we utilized a few cases.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content