Ansible Lineinfile Module With Regexp

Ansible Lineinfile Module With Regexp Examples

Ansible lineinfile module is used to insert new lines at the end of the file or anywhere in the file. With ansible lineinfile module we can remove existed lines from the file and we can replace the lines. In the following examples i will show you how to use ansible lineinfile module with different parameters like regexpto insert new lines.

Craete new file and add a line:

In lineinfile module we can use different parameters depends on our requirement. Here i used ‘path’ parameter, path parameter tells the lineinfile to which file it has to modify So in the path parameter we should mention path of the file which we are going to modify. In line parameter we have to add the line which is going to add in the file. To add the line to the file, we should mention state parameter as present. Create parameter with yes, we used to create the file if the file is not existed.

what does this playbook do

it will create new file and it will add the line to file. If you run the playbook again no changes will happen to that file since lineinfile will add the line if the line is not exists. If the line is already existed then it won’t add that line again.

if you removed the create parameter and if you run the playbook. You must have the file existed in the defined path otherwise it will through you error like

fatal: [localhost]: FAILED! => {“changed”: false, “msg”: “Destination /project/devops/abc.txt does not exist !”, “rc”: 257}

If The File is Already Existed:

if the file is already existed then it will append to end of file.

Ansible Lineinfile With Regexp Example:

Regexp is used to modify the particular line in the file. Here we mentioned in the regexp parameter as ‘^devops’. that means the line started with docker has to modify with the line ‘hello devops’ in the file. we should mention backrefs as yes. Backrefs yes will replace the line with ‘hello devops’ only if it is find regexp line(^docker) in file. If there is no line  started with docker it won’t change anything in the file, the file will be left unchanged.

By default backrefs value is no, so if you have not mentioned backrefs in the task it will assume like ‘backref: no’

What Will Happen If you have Not Mentioned Backrefs: or

What Will Happen If you Mention ‘backrefs: no’

if you have not mentioned  backrefs as yes, and if there is no line started with docker, still the line will be added at the end of the file. If you run the playbook multiple times, the line will be added taht number of times. To avoid this problem you should mention backref as yes.

Add a line after/before a particular line:

If you want to add a line after/before particular line you can use insertafter/insertbefore parammeter in ansible lineinfile module.

Here it will add the ‘hello devops’ line after the line started with docker.

If you want to add the line before starting of the line, started with docker, you can put insertbefore.

Remove a line using Ansible regexp

Here it will remove the line started with docker. To remove the line by using regexp in ansible we should use state parameter as absent.

In the all above examples we discussed about the line started with, since i mentioned the symbol(^). This symbol represents the ‘line starting with’, if you want to know more different options you should learn python regexp.

https://docs.python.org/2/library/re.html