Updated June 23, 2023
Introduction to Ansible Replace Line in File
Ansible Replace Line in File is a module provided in the Ansible package to replace a string pattern in a file. For the Linux environment, where we have everything as a file. It becomes very important to manage all important configuration, library, and script files. The administrator does this by updating all such files regularly with all updates in infrastructure. Ansible provides modules like replace, blockinfile, copy, template, and line in the file to work with such situations. Selecting a module depends on the scope of tasks and the number of iterations involved in the same. So one should evaluate first and then select a module to work with.
Working of Ansible Replace Line in File
In Ansible, like any other module, where we have a set of available parameters and possible acceptable values for the same, in Ansible Replace module, we have the same as well.
- backup: This parameter is used for taking a backup of a file with a timestamp before updating it. So that we will be able to restore it when needed later. Default is “no”.
- attributes: This is used to set attributes of the resulting file. This can be checked by lsattrlater. Check the supported flags at man page of chattr on target systems.
- owner: Name of the user owning the resulting file.
- group: Name of the group owning the resulting file.
- after: This is used to specify that only content after this match will be removed/replaced. This can be used with before in a combination.
- before: This is used to specify that only content after this match will be removed/replaced. This can be used in a combination.
- mode: The permission of the resulting file. Better practice is to use 4 octal numbers inside single quotes to represent the permission, like ‘0777’ or ‘0644’.
- path: The file’s path, which is our task’s target.
- regexp: The pattern to look for in every line of the file. It uses Python regular expression.
- selevel, serole, setype, seuser: These are used to update the SELinux file context.
- validate: This is used to validate the command before running it on the target system shell. The file path to be validated is passed in via ‘%s’.
- encoding: The character encoding for a file. Default s utf-8.
- others: All arguments accepted by the file also work here.
- replace: The string pattern, which will replace the mated patterns. If this is not set, matched regex/patterns are removed entirely.
Examples of Ansible Replace Line in File
We will see some examples, but before going there, we first understand our lab, which we used for testing purposes. Here we have an Ansible control server named ansible-controller and two remote hosts named host-one and host-two. We will create playbooks and run Ansible commands on ansible-controller node and see the results on remote hosts.
Example #1
In this example, we have a sample file where we are trying to replace a string using Ansible. For this, we have a playbook like the one below, where we use Ansible replace and debug modules to replace and display the outputs.
Code:
---
- hosts: all
gather_facts: no
tasks:
- name: Here we check the contents of /var/tmp/sample_file file on target machines.
shell: 'cat /var/tmp/sample_file'
register: var_output
- name: Here are the contents of /var/tmp/sample_file file on target machines before replace
debug:
var: var_output.stdout_lines
- name: Here we are replacing the string pattern
replace:
path: /var/tmp/sample_file
regexp: 'Hello World'
replace: "Namaste To All"
- name: Here we are again checking the contents of /var/tmp/sample_file file on target machines.
shell: 'cat /var/tmp/sample_file'
register: var_output_1
- name: Here are the contents of /var/tmp/sample_file file on target machines after replace
debug:
var: var_output_1.stdout_lines
Now executing this playbook like below:
ansible-playbook ansible_replace.yaml
We have the below output where we can see the Hello World is replaced with Namaste To All.
Output:
Example #2
In this example, we have a playbook like the one below where we try to remove the matched pattern from our sample file. For this, we have a playbook like the one below, where we are not using replace parameter in replace the module, which will result in removing the matched pattern ‘To All’ from the target file.
Code:
---
- hosts: all
gather_facts: no
tasks:
- name: Here we check the contents of /var/tmp/sample_file file on target machines.
shell: 'cat /var/tmp/sample_file'
register: var_output
- name: Here are the contents of /var/tmp/sample_file file on target machines before replace
debug:
var: var_output.stdout_lines
- name: Here we are replacing the string pattern
replace:
path: /var/tmp/sample_file
regexp: 'To All'
- name: Here we are again checking the contents of /var/tmp/sample_file file on target machines.
shell: 'cat /var/tmp/sample_file'
register: var_output_1
- name: Here are the contents of /var/tmp/sample_file file on target machines after replace
debug:
var: var_output_1.stdout_lines
Now executing this playbook like below:
ansible-playbook ansible_replace.yaml
We have the below output where we can see the ‘To All’ is removed from the file.
Output:
Example #3
In this example, we are going to replace a string in /etc/hosts and also will set ownership and permissions. For this, we have a playbook like the one below.
Code:
---
- hosts: host-one
gather_facts: no
tasks:
- name: Here we check the contents of /etc/hosts
shell: 'cat /etc/hosts| grep "192.168.0.11"'
register: var_output
- name: Here are the contents of /etc/hosts file on target machines before replace
debug:
var: var_output.stdout_lines
- name: Here we are replacing the string pattern
replace:
backup: yes
path: /etc/hosts
regexp: '(\s+)host1\.example\.com(\s+.*)?$'
replace: '\1server1.example.com\2'
owner: root
group: root
mode: 0644
- name: Here we again check the contents of /etc/hosts
shell: 'cat /etc/hosts| grep "192.168.0.11"'
register: var_output
- name: Here are the contents of /etc/hosts file on target machines after replace
debug:
var: var_output.stdout_lines
Now running this playbook will give output like the one below, where we can clearly see that the string host1.example.com has been replaced by server1.example.com.
Also, check the other parameters used to set permission and ownership of the file after changes.
ansible-playbook ansible_replace_IP.yaml --syntax-check
Output:
Conclusion
As we saw in this article, this module can replace a line in a file on target remote machines. Also, it can set permissions, ownership, etc., to a file. This module is easy to use and works with the basics of Linux systems.
Recommended Articles
This is a guide to Ansible Replace Line in File. Here we discuss the introduction to Ansible Replace Line in File, working, and programming examples. You may also have a look at the following articles to learn more –