How to Replace Strings and Lines with Ansible

There are several ways to replace a series of characters, an entire line, or words that correspond to a certain pattern. Two modules make this possible: a replacement module and an integrated module. We will dig deeper and look at some examples of how these modules can be used in a reading course to replace lines and rows.

Replacement of a string from a file with a tolerance of

The replacement module replaces all copies of a specific string in a file.

If no string exists, no action is taken and no errors are displayed. Maybe it’s just the return of the fact that nothing’s changed. Three parameters are required in the file to set the :

  • The location of the file as specified in the Path Directive.
  • The regexp directive is a string that needs to be replaced or modified. In addition, you can submit any regular Python expression.
  • The replacement directive is a replacement word or string of characters.

This is the syntax of the replacement module:

– Replace path 1A: /path/path/to/filerexp: replace ‘string of regular for search’ replace ‘word to replace search string’ save: yes

I have a text file called sample.txt, the content of which is shown below.

Unix is a free and open source system used by developers and office workers.  Thanks to the efforts of Linux Torvalds, Unix has become the most popular open source system.

The goal is to replace the Unix chain with Linux. To do this, we create a Playbook file as shown in the figure.

– Host: 127.0.0.1 Task
:
– Name: It is permitted to replace the example
series: Pad
: /etc/ansible/sample.txt
regexp : Replaces Unix
: Linux

Then we start the game.

# ansible-playbook example.txt

You can see in the output that the Unix string has been replaced by Linux.

 

Let’s take another example.

Our second goal is to change the hostname in my /etc/hosts file from server.myubuntu.com to server.linuxtechi.info.

We create a change_hostname.yml file that looks like this:

– Host: 127.0.0.1 Task
:
– Name: It is permitted to replace the example
series: Pad
: /etc/hosts
regexp: ‘(s+)server.myubuntu.com(s+.*)? $’
replace: ‘1server.linuxtechi.info2’.

 

When you start up a Playbook, the domain name changes accordingly, as shown in the figure :

# asible-playbook change_hostname.yml

Laptop replacement registration

Permitted linear module

The Ansible Inline module can be used in different ways. You can use it to insert a new line, delete or edit an existing line from a file. We’ll take a closer look at them all.

Insert a line at the end of the stock (EDF)

First we learn how to make strings when they’re not in the file. Start by specifying the path to the file to which you want to add a line with the path attribute. It replaces the destabilisation option used in Annex 2.3 and previous versions.

Then indicate the line to be added to the EDF. In this case we will add a new item to the /etc/hosts file. If the string already exists, Ansible will not add it and no changes will be made.

The report parameter indicates that the row can be written to the file, and the creation parameter indicates that the file can be created if it does not already exist. This is the player file update_ip.yml.

– Host: 127.0.0.1 Task
:
– Name: It is allowed to update the IP address of a
line file: Path
: /etc/hosts
line: ‘173.82.56.150 wwww.linuxtechi.io’
status: make real
: yes

– network

When starting the Playbook file, please note that a new item or a new line has been added.

# asible-playbook update_ip.yml

 

Insert line before or after

Sometimes you need to insert a new line just before or after a file partition and not always at the end of the line. Use the insert and insert for the instructions.

In the next gamebook we add a line directly after the [default] section in the ansible.cfg file to indicate our preference for an inventory file. We escaped [] because they’re normal sexual characters.

– Host: 127.0.0.1 Task
:
– Name: It is permitted to update the IP address of the
linear file: Pad
: /etc/ansible/ansible.cfg
Line: inventory = /home/linuxtechi/hosts Insert
after : …standard.

Insert, insert, insert, insert, insert, insert, insert, insert, insert, insert

To insert a line directly in front of the parameter, use the parameter insertbefore. In the following example we add the same line just before the #library template in the Ansible configuration file.

– Host: 127.0.0.1 Task
:
– Name: It is allowed to update the IP address of a
line file: Path
: /etc/ansible/ansible.cfg
Line: inventory = /home/linuxtechi/hosts
Insert before: #library.

Deleting a line with line module

This is exactly the opposite of adding a string. The easiest way to do this is to set the status parameter in the absence of . For example, to delete an entry in a file, you must ensure that the status parameter has a missing value

– Host: 127.0.0.1 Task
:
– Name: IP address of the line file
: Path
: /etc/hosts
Line: ‘173.82.56.150 wwwwwlinuxtechi.io’
Status: missing

Reading Library

Another way to delete a string is to use the regexp parameter. For example, the reading guide below deletes all lines in the file that start with a Unix word.

– Host: 127.0.0.1 Task
:
– Name: It is allowed to execute a linear file excessively, an example linear file
:
dest: /etc.ansible/sample.txt
regexp : ^Unix
-Status: none

That’s all that comes out of this article, which we hope will help you understand how to replace lines and rows with anabolics.

Read it: Examples for 9 tee commands under Linux

Related Tags:

ansible' | regex_replace variable,ansible replace ip address in file,ansible find string in file,ansible string replace char,replace with variable ansible,ansible remove multiple lines from file,ansible lineinfile multiple lines,ansible substring,ansible remove characters from string,ansible playbook change config file,ansible blockinfile,ansible lineinfile,win_lineinfile multiple lines,how to append file using ansible,ansible ini_file,ansible remove duplicate lines in file,ansible replace hostname,ansible comment out line,regexp is required with backrefs: true,how to remove a line in a file using ansible,ansible check if line exists in file,"lineinfile" always changed,lineinfile jinja,ansible rc'': 257,user module ansible,ansible rename file,ansible replace use variable,ansible manage config files,ansible replace string in variable,ansible replace multiple lines,ansible replace with_items,ansible lineinfile with_items