Ansible

Ansible Regex_Replace

Ansible is one of the most influential and popular free and open-source automation and configuration management tool.

One of the central building blocks of Ansible is adaptability and the set of features for various environments. This is true even in operations such as string manipulation which can be challenging when dealing with multiple files or hosts.

This tutorial explores the robust set of filters available in the Ansible ecosystem to manipulate the data within the templates and playbooks.

We will, of course, talkabout the regex_replace filter module. This powerful tool allows us to apply the regular expression operations on string values, providing extensive and very powerful pattern matching and replacement.

Ansible Regexp_Replace: Syntax and Usage

Before we dive into the depths, let’s understand the basic syntax of this feature:

{{ 'original_string' | regex_replace('pattern_to_match', 'replacement_string') }}

We can express the given syntax as follows:

original_string – The string on which you want to apply the regex.

pattern_to_match – The regex pattern to look for within the original string.

replacement_string – The string that replaces the matched pattern.

Example 1: Basic Usage

Let us look at a basic example of using this feature.

Suppose we have a string that says “Port to MySQL is 3306” and we wish to replace the “MySQL” string with “MariaDB”.

{{ 'Port to MySQL is 3306' | regex_replace('MySQL', 'MariaDB') }}

Example 2: Replacing the IP Addresses

Suppose we aim to find the IP addresses and replace them within the logs to ensure the user privacy. We can create a playbook that does this for us as demonstrated in the following example:

---
- hosts: localhost
  vars:
    log_message: "User logged in from 192.168.1.1"
  tasks:
    - name: Mask IP Address
      debug:
        msg: "{{ log_message | regex_replace('(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})', 'XXX.XXX.XXX.XXX') }}"

The previous playbook should locate the IP address of the user and replace it with the defined values as follows:

TASK [Mask IP Address] ******************************
ok: [localhost] => {
    "msg": "User logged in from XXX.XXX.XXX.XXX"
}

Example 3: Masking the Email Addresses

We can also use the regex_replace feature to create a regular expression that replaces the email addresses with the masked values. An example playbook demonstration is as follows:

---
- hosts: localhost
  vars:
    email_string: "Contact me at [email protected]"
  tasks:
    - name: Mask Email Address
      debug:
        msg: "{{ email_string | regex_replace('([a-zA-Z0-9._%+-]+)@', 'XXXX@') }}"

Similarly, the previous playbook should return a masked value as follows:

ok: [localhost] => {
    "msg": "Contact me at [email protected]"
}

Example 4: Removing the HTML Tags

Consider another example where we need to extract a plain text from a string of the HTML data. We can make use of regular expressions as demonstrated in the following:

---
- hosts: localhost
  vars:
    html_content: "<p>Greetings from Linuxhint!</p>"
  tasks:
    - name: Strip HTML tags
      debug:
        msg: "{{ html_content | regex_replace(']+>', '') }}"

This should extract the actual string that is within the HTML tags as demonstrated by the resulting output:

ok: [localhost] => {
    "msg": "Greetings from Linuxhint!"
}

Example 5: Date Formatting

Another common usage of regular expressions is date formatting. For example, we can use the regex_replace module to format a date from YYYY/MM/DD to DD-MM-YYYY as follows:

---
- hosts: localhost
  vars:
    date_string: "Today's date is 2023/07/27"
  tasks:
    - name: Format Date
      debug:
        msg: "{{ date_string | regex_replace('^(.*?)(\\d{4})/(\\d{2})/(\\d{2})', '\\1\\4-\\3-\\2') }}"

Output:

ok: [localhost] => {
    "msg": "Today's date is 27-07-2023"
}

There you have it!

Remember that since a backslash is an escape character in YAML, you need to double it (\\) in your regular expressions.

You can also employ the (?i) at the beginning of your regex pattern for case-sensitive matching.

Conclusion

You learned how to use the regex_replace feature in Ansible to perform the regular expression pattern matching and replacement in string data with ease. You also come across multiple examples that demonstrate how to use this module to perform various tasks.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list