Updated April 1, 2023
Introduction to Ansible replace
Ansible replaces module is to replace all the instances of the matching string from the path of the file specified. It could be the multiple lines, or single words based on the regular expressions to match the pattern of the string in addition, it provides several options like backup before modifying the file, support replacing before and after line of the matching string, etc.
How does the replace module work in Ansible?
The Ansible replace module is part of the ansible-base and included in all the ansible installations. It replaces all the instances of a pattern within a file.
If you need to change the single instance of the string, not the multiples, then consider using the LineInFile module instead of the replace module.
The Ansible replace module works with several parameters. We will see some basic but important parameters to work with the replace module.
- Path: Path of the destination file. Before Ansible version 2.3, it was named as dest, destfile, and name.
- Regexp: Regular expressions to look for the content of the file, and based on it, the file content is getting replaced.
- Replace: Keyword or string to replace the content inside the file. If this parameter is not set and if the regular expression matches the content of the file, then the entire keyword will be removed in all the instances of the file.
- Backup: Its default value is ‘no’. If the value is set ‘yes’, then it will take a backup of the file before modifying so that the existing file can be retained in case of wrong changes. This parameter backs up the file with the name of the file and the timestamp.
- Owner: Name of the owner to own the file after replacement. It uses the chown command for the owner’s replacement.
- Group: Name of the group to own the file after replacement.
- Mode: It specifies the permission of the mentioned file should have.
- Before: If mentioned, it replaces the contents before the match and can be combined with the after
- After: If mentioned, it replaces the contents after the match and can be combined with the before
There are a few additional parameters that you can check on the ansible website.
To validate the regex expressions online, there are many websites available, and you can pick one from the google search.
Examples of Ansible replace
Given below are the examples of Ansible replace:
Example #1
Replacing the file content with string.
In this example, we will replace the file content on the remote Linux system with the string pattern and without using the regex pattern.
---
- name: Playbook to test the ansible replace module
hosts: linuxservers
tasks:
- name: Replace module test
ansible.builtin.replace:
path: /tmp/myfile.txt
regexp: 'This'
replace: 'That'
The above example will replace the ‘This’ keyword with the ‘That’ keyword in the file /tmp/myfile.txt.
Note: Make sure that the ansible user configured in the inventory file has the proper permission to replace the content of the file.
Output:
Output from the text file.
If we don’t use the replace parameter, then it will replace the matched regex pattern with null.
tasks:
- name: Replace module test
ansible.builtin.replace:
path: /tmp/myfile.txt
regexp: 'This'
Output from the text file.
If the regex content doesn’t match then, it won’t change the file.
Example #2
Using the regexp to replace the content.
The below example will use the regular expression to replace the file content. Here, we need to replace the host file content on all the Linux servers which have the host file string pattern is String-String (for example, “ansible-host” will be replaced with the ansiblehost string.)
---
- name: Ansible playbook to replace string with regex.
hosts: linuxservers
tasks:
- name: task to replace the host file content.
replace:
path: /etc/hosts
regexp: '[A-Za-z]+-[a-z]+|[a-z]+-[a-z]+'
replace: ansiblehost
backup: yes
Before making any changes to the original file, the backup parameter will take the backup of the file in the same folder.
Example #3
Replace the file content on the windows servers using the LineinFile module.
As discussed earlier, we can also use the LineInFile module for the file content replacement or adding/appending the new content to the file. We will use the windows module win_lineinfile because no replace module available for windows, so this can be a good module to work with the file content.
---
- name: Add the new line to the file and replace the content from the file.
hosts: windowsservers
tasks:
- name: Add new line to the file c:\temp\myfile.txt
win_lineinfile:
path: c:\temp\myfile.txt
line: this is a new line
- name: Replace the line in the file.
win_lineinfile:
path: c:\temp\myfile.txt
regex: 'ansible'
line: 'This is from the ansible-playbook'
This example first inserts the line in the file (if it exists) and then replaces the line with the new line if the keyword is found.
Playbook execution:
ansible-playbook replacewinfile.yml -vvv
Output:
Example #4
Ansible playbook to replace the string and change the file permission.
In this example, we will replace the string from the destination file and also change the user permission on the file with the replace module.
---
- name: Playbook to replace the file content and adds user and group permission to the file.
hosts: linuxservers
tasks:
- name: Replace the content of file /tmp/myfile.txt
replace:
path: /tmp/myfile.txt
regexp: 'ansible'
replace: 'ansible-host'
owner: ansibleadmin
group: mygrp
mode: 0644
This example will replace the file content ansible with the ansible-host keyword and change the file’s owner, group, and mode.
Conclusion
Ansible replaces operation is very powerful when we need to deal with the file contents. This is because it can replace the contents of the files on a very large scale. Examples like replacing the hosts’ file content in all windows or Unix servers, changing the web app configurations on all the web servers, etc.
Recommended Articles
This is a guide to Ansible replace. Here we discuss How does the replace module works in Ansible, along with the examples and outputs. You may also have a look at the following articles to learn more –