How to uncomment/comment lines in files using Ansible

Uncommenting or commenting a line is usually a task of just adding character or removing a character at the front; Unless it is a block comment.

Let us see a few ways of dealing with single line comments in Ansible.

Commenting a line in Ansible using replace module

Replace module in Ansible is used to substitute all instances of a particular string/pattern in a file.

In the following task, I am trying to comment a line, by replacing the line with another line having ‘#’ in front of that. You can use any other comment character as you please.

This is the most basic example, and a better way to do this will be given in the next case.

So, I am using the following three parameters of the replace module for the task.

  • Path – This is the name of the file with absolute path.
  • Regexp – The pattern which we need to replace. It can be either a python regex or a string. All the matches in the given file will be substituted.
  • Replace – This is the pattern/string to be replaced with.

The following task replaces all lines which have ‘This line should be commented’.

Input.txt
=========
Hello,How are you?
This line should be commented
This line should be commented

- hosts: all
  tasks:
  - name: Exampel of commenting a line in Ansible.
    replace:
      path: /Users/mdtutorials/Documents/Ansible/Input.txt
      regexp: 'This line should be'
      replace: '#This line should be'

Input.txt after execution
=========================
Hello,How are you?
#This line should be commented
#This line should be commented

Note:

In the above example, if there is some character before the value given by regexp parameter, in the file. Then it won’t be commented. For example, the if the input.txt contained ‘No This line should be commented’, it would be modified to ‘No #This line should be commented’.

Commenting every line having a particular pattern

We already know that the value set against the ‘regexp’ can be any regular expressions in Python. This applies to the replace parameter also. So using grouping and matching, we can comment any line containing a particular pattern.

In the below example, I am commenting out any line containing the word ‘google servers’.

  • The below ‘regexp’ matches every line starting with any number of random characters(.*) and ending with the same(.*), But with the pattern ‘google server’ somewhere in between.
  • In the replace module, everything that was matched in the previous line ( every character inside brackets) can be availed using ‘\1’. I am also adding the ‘#’ in front of that. So primarily the line is replaced with an commented version of itself.
Input.txt
---------
Hello,How are you?
The following are US based google servers
127.0.0.1
The following are UK based google servers
127.0.0.1

 hosts: all
  tasks:
  - name: Commenting a line using the regualr expressions in Ansible.
    replace:
      path: /Users/mdtutorials/Documents/Ansible/Input.txt
      regexp: '(.*google server.*)'
      replace: '#\1'

Input.txt after execution
-------------------------
Hello,How are you?
#The following are US based google servers
127.0.0.1
#The following are UK based google servers
127.0.0.1

Uncommenting a line in Ansible using the replace module

You can also similarly do the uncommenting task. You just have to remove the first character, right. We can do that using the matching and grouping.

For example, in the below case, I am attempting to uncomment the two lines with the text ‘uncomment this line’

The regexp matches every line starting with a ‘#’ character and having ‘Uncomment this line’ pattern. It does not matter if any characters are in between them since I have given the ‘.*’ to take care of that.

input.txt
----------
Hello,How are you?
#Uncomment this line
#Also, Uncomment this line

- hosts: all
  tasks:
  - name: Ansible delete file glob
    replace:
      path: /Users/mdtutorials/Documents/Ansible/Input.txt
      regexp: '^#(.*Uncomment this line.*)'
      replace: '\1'

input.txt after execution
-------------------------
Hello,How are you?
Uncomment this line
Also, Uncomment this line

3 Replies to “How to uncomment/comment lines in files using Ansible”

  1. Thanks for that tutorials. I find them very useful!

    I think that ther’s a mistake on the last example. File after execution is the same as before execution.

    Thanks

    1. Hi Marcin,
      Thanks for that:). I have corrected the output in the example.

    2. Hi, can you ping that corrected lines

Comments are closed.