Among the most common and valuable modules is the “replace” module in the Ansible core. The replace module allows us to replace the text in a given file by incorporating the features such as pattern matching and regular expressions. This module is constructive when modifying the configuration files across multiple systems without manual intervention.
In this tutorial, we will deeply dive into the workings of the Ansible “replace” module, its basic syntax, and the accepted parameters using practical examples to demonstrate their functionalities.
Ansible Replace Module – Basic Syntax
Before discussing the examples and functionality of the “replace” module, let us illustrate the its basic syntax:
ansible.builtin.replace:
path: /path/to/file
regexp: 'original_string_or_regex'
replace: 'replacement_string'
Key Parameters:
You need to know the following essential parameters when working with the replace module:
Path – The target file in which the replacements will occur.
Regexp – The regular expression to find the matches that need replacement.
Replace – The text with which to replace the matched patterns.
Backups – If set to true, the module creates a file backup, including the timestamp information, which allows you to roll back any changes.
Encoding – It specifies the character encoding for reading and writing the file.
These are just some standard parameters. You can reference the official documentation for more options for the “replace” module.
Example 1: Basic Usage
The following example demonstrates a basic use of the “replace” command to edit a file called “server.ini” with an entry as follows:
Suppose we wish to change the log level from debug to info. We can run the playbook as shown in the following:
ansible.builtin.replace:
path: /etc/custom/server.ini
regexp: 'log_level = debug'
replace: 'log_level = info'
Example 2: Using the Regular Expressions
The “replace” module also allows us to use the regular expression patterns to perform the replacements in a given file.
Suppose we want to replace all the occurrences of a given IP address in a file. We can use the regular expressions to match and replace the value as demonstrated in the following playbook:
ansible.builtin.replace:
path: /path/to/config.txt
regexp: '(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'
replace: '192.168.1.10'
Example 3: Replace a Given Context
Sometimes, you may encounter a scenario where the target file has multiple sections but similar values. Luckily, the “replace” module allows us to specify which context we wish to replace without affecting the other.
Suppose we have a file called “services.ini” with two sections and values as follows:
port = 80
[db]
port = 3306
Suppose we want to replace the port value in the [db] section without changing the port of the web section. In that case, we can use the “lookbehind” and “lookahead” assertions in regular expression as demonstrated in the playbook:
ansible.builtin.replace:
path: /path/to/services.conf
regexp: '(?<=\[db\]\nport = )3306'
replace: '3300'
Example 4: Backing Up Before Making Replacements
To be on the safe side, Ansible allows us to create a backup of the file before making replacements. This is a must-use feature if your configurations are critical for your infrastructure.
The following example shows how to back up a file before making replacements:
ansible.builtin.replace:
path: /path/to/app.cfg
regexp: 'log_level = debug'
replace: 'log_level = info'
backup: true
If a replacement occurs, Ansible creates a backup file in the same directory as the original, appending a timestamp to the filename.
Conclusion
As you discovered, Ansible is a potent tool with the aid of modules like the “replace” module. With the power of regular expressions and granular targeting, you can harness the capabilities of the “replace” module to streamline the configuration file modifications across any number of hosts.