Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commenting out a line with Ansible lineinfile module

Tags:

I find it hard to believe there isn't anything that covers this use case but my search has proved fruitless.

I have a line in /etc/fstab to mount a drive that's no longer available:

//archive/Pipeline /pipeline/Archives cifs ro,credentials=/home/username/.config/cifs 0   0

What I want is to change it to

#//archive/Pipeline /pipeline/Archives cifs ro,credentials=/home/username/.config/cifs 0   0

I was using this

---
- hosts: slurm
  remote_user: root

  tasks:
    - name: Comment out pipeline archive in fstab
      lineinfile:
        dest: /etc/fstab
        regexp: '^//archive/pipeline'
        line: '#//archive/pipeline'
        state: present
      tags: update-fstab

expecting it to just insert the comment symbol (#), but instead it replaced the whole line and I ended up with

#//archive/Pipeline

is there a way to glob-capture the rest of the line or just insert the single comment char?

 regexp: '^//archive/pipeline *'
 line: '#//archive/pipeline *'

or

 regexp: '^//archive/pipeline *'
 line: '#//archive/pipeline $1'

I am trying to wrap my head around lineinfile and from what I"ve read it looks like insertafter is what I'm looking for, but "insert after" isn't what I want?

like image 445
datakid Avatar asked Aug 31 '16 01:08

datakid


People also ask

How do you comment out in Ansible?

Comment Out & Uncomment Lines using Ansible In Ansible this can be achieved by using the \1 in replace to match ( … every character inside brackets… ) in regexp .

How do you modify a line in a file using Ansible?

You can use the lineinfile Ansible module to achieve that. The regexp option tells the module what will be the content to replace. The line option replaces the previously found content with the new content of your choice. The backrefs option guarantees that if the regexp does not match, the file will be left unchanged.

How do you check if a line exists in a file Ansible?

Using the check_mode parameter ensures that the task does not actually change the file, but instead it reports what it would do. You can then check for the return key "changed" in order to see if it would have been changed. If it would, the line is not present in the file.

What is Lineinfile module in Ansible?

Introduction to Ansible lineinfile. Ansible Lineinfile is a module of Ansible that is used to modify the particular line in a file. It is useful to add a new line, modify a line, replace a line, and remove an existing line in a file if it finds a specific text.

What does the Lineinfile module do?

This module ensures a particular line is in a file, or replace an existing line using a back-referenced regular expression. This is primarily useful when you want to change a single line in a file only.

Which of the following module is used to modify a single line in a file?

Ansible lineinfile module is helpful when you want to add, remove, modify a single line in a file. You can also use conditions to match the line before modifying or removing using the regular expressions.

Will Lineinfile create a file?

We can use the lineinfile module to create a file and add a new line to the created file. In the example playbook above, we use the dest parameter to specify the path of the file.


2 Answers

You can use the replace module for your case:

---
- hosts: slurm
  remote_user: root

  tasks:
    - name: Comment out pipeline archive in fstab
      replace:
        dest: /etc/fstab
        regexp: '^//archive/pipeline'
        replace: '#//archive/pipeline'
      tags: update-fstab

It will replace all occurrences of the string that matches regexp.

lineinfile on the other hand, works only on one line (even if multiple matching are find in a file). It ensures a particular line is absent or present with a defined content.

like image 72
techraf Avatar answered Sep 21 '22 14:09

techraf


Use backrefs=yes:

Used with state=present. If set, line can contain backreferences (both positional and named) that will get populated if the regexp matches.

Like this:

- name: Comment out pipeline archive in fstab
  lineinfile:
    dest: /etc/fstab
    regexp: '(?i)^(//archive/pipeline.*)'
    line: '# \1'
    backrefs: yes
    state: present

Also note that I use (?i) option for regexp, because your search expression will never match Pipeline with capital P in the example fstab.

like image 28
Konstantin Suvorov Avatar answered Sep 21 '22 14:09

Konstantin Suvorov