How Ansible Replace Line works in a file

How Ansible Replace Line works in a file
Techiio-author
Written by Nilima PaulFebruary 3, 2022
11 min read
Ansible
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Nilima Paul

Technology Security Analyst

In this article, We will discuss Ansible Replace Line in File.

Introduction to Ansible Replace Line in File

Ansible Replace Line in File is a module given in Ansible bundle to supplant a string design in a record. For Linux climate, where we have everything as a record. It turns out to be vital to deal with the immensely significant arrangement, library, and contents documents. This is finished by the overseer by refreshing all such documents consistently with all refreshes in the foundation. Ansible gives modules like supplant, blocking file, duplicate, format, and lineinfile to work with such circumstances. Choosing a module relies upon the extent of errands and the number of cycles associated with the same. So one ought to assess first then, at that point, select a module to work with.

Working principle of Ansible Replace Line in File

In Ansible, similar to whatever other module, where we have a bunch of accessible boundaries and conceivable OK qualities for same, in Ansible Replace module we have the same too.

Underneath list incorporates numerous significant boundaries on the off chance that not everything, can be utilized in a blend to create wanted outcomes:

  • 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 LSAT later. Check the supported flags on the man page of chatter 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 with after in a combination.
  • after: This is used to speed city that only content after this match will be removed/replaced. This can be used with before in a combination.t7’ 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.
  • level, role, type, user: These are used to update the SELinux file context.
  • validate: This is used to validate the command before running 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 file also work here.

replace The string pattern which will replace the mated patterns. If this is not set, then matched regex/patterns are removed entirely.

Examples of Ansible Replace Line in File

We will see a few models, yet prior to going there, we initially comprehend our lab, we utilized for testing reason. Here we have an Ansible control server named ansible-regulator and two controllers has named have one and host-two. We will make playbooks and run ansible orders on ansible-regulator hub and see the outcomes on remote hosts.

Example #1

In this model, we have an example record, in which we are attempting to supplant a string utilizing Ansible. For this, we have a playbook like underneath, where we are utilizing Ansible supplant and investigate modules to supplant and show the results.

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 below output where we can see the Hello World is replaced with Namaste To All.

Output:

blogpost

Example #2

In this model, we have a playbook like underneath where we are attempting to eliminate the matched example from out example record. For this we have a playbook like underneath, where we are not utilizing supplant boundary in supplant module, which will bring about eliminating the matched example 'To All' from target record.

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 below output where we can see the ‘To All’ is removed from the file.

Output:

blogpost

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 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

Presently running this playbook will give yield like beneath where we can plainly see that string host1.example.com has been supplanted by server1.example.com.

Additionally check different boundaries used to set consent and responsibility for record after changes.

ansible-playbook ansible_replace_IP.yaml --syntax-check

Output:

blogpost

Conclusion

As we found in this article, this module can supplant a line in a record on track remote machines. Likewise it can set consents, proprietorship and so forth to a document. This module is not difficult to utilize and works with essentials of Linux frameworks.

Ansible
Python
yaml
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Nilima Paul
Technology Security Analyst
Techiio-followerTechiio-followerTechiio-follower
201 Blog Posts
0 Discussion Threads
Trending Technologies
15
Software91
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration28
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance