Replace a String in a File Using Ansible

Ansible has a 'replace', 'blockinfile' and 'lineinfile' modules for handling lines in a file. Each of them is used for a specific purpose. The choice of choosing one among them depends on the situation.

Here at LinuxAPT, we shall look into how to replace any string in a file using Ansible. Ansible has a 'ansible.builtin.replace' module for the same purpose. 


What is the syntax of Ansible Replace module ?

The general syntax for the Replace module is given below:

- name: Name of the task
ansible.builtin.replace:
path: path of the file that needed to be changed
regexp: regex expression for old string to be replaced
replace: new string that needs to be added to file


A few examples illustrating String replacement in playbooks

The below playbook will check for the hosts file at path provided and replace the old host name(managed1.anslab.com) with new host name(linuxapt.com):

---
- hosts: managed1(Name of your target node)
gather_facts: no
become: true
tasks:
- name: Replace module demo
ansible.builtin.replace:
path: /etc/hosts
regexp: '(\s+)managed1\.anslab\.com(\s+.*)?$'
replace: '\1linuxapt.com\2'

In the output, It replaced every occurrence of "managed1.anslab.com" in the file with "linuxapt.com".

Here, we used regex expression. regex helps in matching multiple strings or a single string. Learn more about python regex here https://docs.python.org/3/library/re.html.


Other Parameters of Ansible Replace module

1. 'after'

When we have the 'after' parameter along with 'replace', it is the 'after' parameter from where the replacement of the content will actually start and it continues till the end of the file.

To be clearer, let us see the below playbook. Here, we have the 'after' parameter value as "PubkeyAuthentication". After encountering the "PubkeyAuthentication" string, it will replace the word "yes" with "no".

The syntax is given below:

- name: Syntax for Replace module with the 'after' expression.
ansible.builtin.replace:
path: path of the file
after: word after which replacement starts
regexp: regular expression to match the word that needs to be changed
replace: string that will replace old word

In the playbook below, observe the usage of the 'after' expression:

---
- hosts: managed1 (Name of your target node)
gather_facts: no
become: true
tasks:
- name: Replace module demo
ansible.builtin.replace:
path: /etc/ssh/sshd_config
regexp: 'yes'
after: 'PubkeyAuthentication'
replace: 'no'

If you check the output, you may notice after the string "PubkeyAuthentication". The string "yes" is now replaced with "no". We are using sed to get just one line from the file for easy visibility.


2. 'before'

'before' is opposite to 'after'. It will replace every matching string specified for the 'before' parameter.

It's Syntax is:

- name: Syntax for Replace module with the 'before' expression.
ansible.builtin.replace:
path: path of the file that needs to be updated
before: before this word, it will match the regex and replace it with new word
regexp: regular expression to match the word to be changed
replace: string that will replace the old word

Let us see an example playbook for the same. The below playbook uses the 'before' keyword:

---
- hosts: managed1(Name of your target node)
gather_facts: no
become: true
tasks:
- name: Replace module demo
ansible.builtin.replace:
path: /etc/hosts
before: 'managed1'
regexp: 'ibmimedia.com'
replace: 'linuxapt.com'

In the above playbook, before the word "managed1", if there is any string like "ibmimedia.com", it will be replaced with "linuxapt.com" for simplicity purposes. Here, Here we are not using any complex regex expressions.


3. 'before and after'

You can also use before and after parameters simultaneously to replace words in between.

It's Syntax is given below:

- name: Syntax for Replace module with the 'before' and 'after' expressions.
ansible.builtin.replace:
path: path of the file
after: after this word the word will be replaced
before: before this word the word will be replaced
regexp: regular expression to match the word to be replaced
replace: string that will replace old word

Now, let us look into a simple example playbook below that demonstrates before and after:

---
- hosts: managed1
gather_facts: no
become: true
tasks:
- name: Replace module demo
ansible.builtin.replace:
path: /etc/hosts
after: '127.0.2.1'
before: 'managed1'
regexp: 'linuxapt.com'
replace: 'ibmisoft.com'

Here, you can clearly observe that the string before 'managed1' and after "127.0.2.1" string is replaced with the string "ibmisoft.com".


Going further, there are few more interesting attributes for this module, for example backup.


4.  'backup'

The 'backup' property has two options that can be set as 'yes' or 'no'. If you set backup as yes, it will create a backup file of the original file along with its timestamp. So, in case you messed up the original file, you can refer to backup file (suggested for production systems).


[Need help in fixing Ansible issues ? We can help you. ]

This article covers the usage of the 'replace' module in Ansible for replacing a string in a file. In fact, Ansible provide multiple ways that you can use to replace a string, an entire line or words that match a certain pattern. There are two modules that you can use to achieve this: the replace module and the inline module. 

Related Posts