Connect with us

Hi, what are you looking for?

Latest

Insert a line after a string using the lineinfile module

Insert a line after a string using the lineinfile module

 

Insert a line after a string using the lineinfile module

Introduction

I not too long ago got here throughout a scenario whereby I wanted to seek for a string/phrase in a file after which append some piece of textual content after the matched string. I’m positive that there are various methods to perform this utilizing Ansible however on this publish, I’ll exhibit what I used to get this finished. I’ll additionally present you what didn’t work so that you would possibly get an concept about what to not do if you’re attempting to change textual content in information utilizing lineinfile module which I discovered to be superior by the best way. You could discuss with the official documentation for lineinfile proper right here. This module ensures a specific line is in a file, or exchange an current line utilizing a back-referenced common expression. That is primarily helpful whenever you need to change a single line in a file solely.

For the aim of this demonstration, I’ll be utilizing a Centos 7 system with Ansible 2.Eight put in on it. Let’s simply confirm that earlier than we get began.

[[email protected] ~]$ cat /and many others/redhat-release
CentOS Linux launch 7.6.1810 (Core)
[[email protected] ~]$ ansible –version
ansible 2.8.2
config file = /and many others/ansible/ansible.cfg
configured module search path = [u’/home/ssuri/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python model = 2.7.5 (default, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
[[email protected] ~]$

State of affairs

Given under is the file that I’d like to change.

[[email protected] ansible_playbooks]$ cat /and many others/systemd/system/node_exporter.service
WorkingDirectory=/choose/prometheus/
ExecStart=/choose/prometheus/node_exporter –collector.systemd $exporterArg
[Install]

[[email protected] ansible_playbooks]$

My requirement is so as to add this piece of textual content –collector.textfile.listing /root/node_exporter after the string $exporterArg.

Right here’s the playbook I wrote initially to perform this activity.

[[email protected] ansible_playbooks]$ cat didnt_work.yml

– identify: a check play
hosts: all
gather_facts: no

duties:
– identify: node-service exchange in file
lineinfile:
dest: “/and many others/systemd/system/node_exporter.service”
regexp: ‘$exporterArg’
line: “-collector.textfile.listing /root/node_exporter”

The playbook sounds easy proper. Search for $exporterArg string within the file /and many others/systemd/system/node_exporter and add the textual content -collector.textfile.listing /root/node_exporter after it.

However that is what occurred after I ran the playbook.

[[email protected] ansible_playbooks]$ ansible-playbook -i localhost, didnt_work.yml -b

PLAY [a test play] *****************************************************************************************************

TASK [node-service replace in file] ************************************************************************************
modified: [localhost]

PLAY RECAP *************************************************************************************************************
localhost : okay=1 modified=1 unreachable=Zero failed=Zero skipped=Zero rescued=Zero ignored=0

[[email protected] ansible_playbooks]$

Now after I have a look at the file I discover this.

Insert a line after a string using the lineinfile module

[[email protected] ansible_playbooks]$ cat /and many others/systemd/system/node_exporter.service
WorkingDirectory=/choose/prometheus/
-collector.textfile.listing /root/node_exporter
[Install]

[[email protected] ansible_playbooks]$

The playbook changed all the line containing the string $exporterArg with -collector.textfile.listing /root/node_exporter. That isn’t what we needed to occur. I used to be additionally tempted to make use of the insertafter attribute as a substitute of regexp considering it’d work however didn’t. The rationale for that’s the insertafter attribute inserts the road of textual content one line under the matched string.

So, right here’s a fast tip about utilizing regexp within the lineinfile module. The best way this works is sort of a easy search and exchange such as you would use in sed whereas working in bash. To get round this I modified the road part of my playbook to incorporate all the up to date line of textual content that I wanted and that labored.

[[email protected] ansible_playbooks]$ cat testplay.yml

– identify: a check play
hosts: all
gather_facts: no

duties:
– identify: node-service exchange in file
lineinfile:
dest: “/and many others/systemd/system/node_exporter.service”
regexp: ‘$exporterArg’
line: “ExecStart=/choose/prometheus/node_exporter –collector.systemd $exporterArg –collector.textfile.listing /root/node_exporter”

Now after I executed this playbook I used to be in a position to efficiently carry out the required modification within the file.

[[email protected] ansible_playbooks]$ cat /and many others/systemd/system/node_exporter.service
WorkingDirectory=/choose/prometheus/
ExecStart=/choose/prometheus/node_exporter –collector.systemd $exporterArg
[Install]

[[email protected] ansible_playbooks]$ ansible-playbook -i localhost, testplay.yml -b

PLAY [a test play] *****************************************************************************************************

TASK [node-service replace in file] ************************************************************************************
modified: [localhost]

PLAY RECAP *************************************************************************************************************
localhost : okay=1 modified=1 unreachable=Zero failed=Zero skipped=Zero rescued=Zero ignored=0

[[email protected] ansible_playbooks]$
[[email protected] ansible_playbooks]$ cat /and many others/systemd/system/node_exporter.service
WorkingDirectory=/choose/prometheus/
ExecStart=/choose/prometheus/node_exporter –collector.systemd $exporterArg –collector.textfile.listing /root/node_exporter
[Install]

Conclusion

We hope that you simply discovered this real-world instance of utilizing the lineinfile module to be helpful and we encourage you to attempt some fascinating makes use of of this module particularly whenever you resolve to transition a few of your information manipulation scripts to ansible.

Put up Views:
10,697

The next two tabs change content material under.

Insert a line after a string using the lineinfile module

He began his profession in IT in 2011 as a system administrator. He has since labored with HP-UX, Solaris and Linux working programs together with publicity to excessive availability and virtualization options.
He has a eager curiosity in shell, Python and Perl scripting and is studying the ropes on AWS cloud, DevOps instruments, and methodologies. He enjoys sharing the data he is gained through the years with the remainder of the neighborhood.

Insert a line after a string using the lineinfile module

ansible remove multiple lines from file,ansible lineinfile multiple lines,ansible lineinfile append to end of line,ansible replace multiple lines,ansible lineinfile newline,ansible find string in file,blockinfile,ansible lineinfile variable

You May Also Like

Hosting

The latest round of MITRE ATT&CK evaluations proved yet again that Microsoft customers can trust they are fully protected even in the face of...

Hosting

On Thursday, April 23rd, 2020, Canonical Ltd, the makers of Ubuntu Linux distribution officially released the long-awaited Ubuntu 20.04 version code-named “Focal Fossa”, it...

Hosting

As the robot process automation (RPA) market becomes more and more dynamic, more and more companies are trying to integrate RPA into their business...

Latest

One of the most discussed new features in Ubuntu 20.04 is the dark mode. You can install a dark theme in any version of...