CentOS

How to 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.

What Will We Cover?

In this post, we will explore how to replace any string in a file using Ansible. Ansible has a ‘ansible.builtin.replace’ module for the same purpose. Let us explore the replace module below.

The general syntax for the Replace module is as follows:

- 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

Let us see some example playbooks below. 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(linuxhint.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: '\1linuxhint.com\2'

You can see the output of this playbook below. It replaced every occurrence of “managed1.anslab.com” in the file with “linuxhint.com”.

I am using regex expression here. regex helps in matching multiple strings or a single string. Learn more about python regex here https://docs.python.org/3/library/re.html.

Now, let us explore the other parameters of this 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”.

Syntax:

- 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 below, you may notice after the string “PubkeyAuthentication”. The string “yes” is now replaced with “no”. I am 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.

Syntax:

- 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: 'linuxhint.com'

replace: 'linuxways.net'

In the above playbook, before the word “managed1”, if there is any string like “linuxhint.com”, it will be replaced with “linuxways.net” for simplicity purposes. Here, I am not using any complex regex expressions. You can see the file before and after running output in the above screenshot.

3. ‘before and after’

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

Syntax:

- 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: 'linuxways.net'

replace: 'tecofers.com'

In the above playbook, you can clearly observe that the string before ‘managed1’ and after “127.0.2.1” string is replaced with the string “tecofers.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).

Conclusion

In this guide, we have seen the usage of the ‘replace’ module in Ansible for replacing a string in a file. Also, you can refer to the official ansible document for more parameters that go with this module.

Similar Posts