March 8, 2021
Ansible playbook to replace multiple lines in a file

Ansible playbook to replace multiple lines in a file

Sponsored

Ansible playbook to replace multiple lines in a file

Introduction

Commands like sed and awk allow us to harness the power of regular expressions to search for text-based patterns in files and manipulate the matched text as per our requirement. The thing about regular expressions is that they are confusing. To the new Linux user, regular expressions may as well be another language. Although sed and awk are very powerful tools, mastering them is a tedious task for a system administrator.
While working on the command line, we frequently interact with various system configuration files and sometimes we need to modify these files to suit our needs. This requirement may span across a fleet of systems as well. Using awk or sed to modify multiline strings in files over ssh could prove to be a handful. Luckily, we can use the replace module in ansible to search for and replace multiple lines between two patterns.
In this post, we will share a sample Ansible playbook that replaces multiple lines of text between two patterns. We can view documentation for the replace module using ansible-doc. Given below is a snippet from the output.

root@linuxnix ~]# ansible-doc replace
> REPLACE (/usr/lib/python3.6/site-packages/ansible/modules/files/replace.py)

This module will replace all instances of a pattern within a file. It is up to the user to maintain idempotence by ensuring
that the same pattern would never match any replacements made.

* This module is maintained by The Ansible Community
OPTIONS (= is mandatory):

- after
If specified, only content after this match will be replaced/removed.
Can be used in combination with `before'.
Uses Python regular expressions; see http://docs.python.org/2/library/re.html.
Uses DOTALL, which means the `.' special character `can match newlines'.
[Default: (null)]
type: str
version_added: 2.4

- attributes
The attributes the resulting file or directory should have.
To get supported flags look at the man page for `chattr' on the target system.
This string should contain the attributes in the same order as the one displayed by `lsattr'.
The `=' operator is assumed as default, otherwise `+' or `-' operators need to be included in the string.
(Aliases: attr)[Default: (null)]
type: str
version_added: 2.3

- backup
Create a backup file including the timestamp information so you can get the original file back if you somehow clobbered it
incorrectly.
[Default: False]
type: bool

- before
If specified, only content before this match will be replaced/removed.
Can be used in combination with `after'.
Uses Python regular expressions; see http://docs.python.org/2/library/re.html.
Uses DOTALL, which means the `.' special character `can match newlines'.
[Default: (null)]
type: str
version_added: 2.4

- encoding
The character encoding for reading and writing the file.
[Default: utf-8]
type: str
version_added: 2.4
...skipping...
= regexp
The regular expression to look for in the contents of the file.
Uses Python regular expressions; see http://docs.python.org/2/library/re.html.
Uses MULTILINE mode, which means `^' and `

For our playbook, we will be using the regexp and replace options to edit the /var/www/html/index.html on a couple of Ubuntu systems. Our goal is to replace the entire content between the body tags with the lines “This is a sample index.html file. The file has been updated using Ansible.” Given below is the current content residing within the body tags.