Question:
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:
1 2 |
//archive/Pipeline /pipeline/Archives cifs ro,credentials=/home/username/.config/cifs 0 0 |
What I want is to change it to
1 2 |
#//archive/Pipeline /pipeline/Archives cifs ro,credentials=/home/username/.config/cifs 0 0 |
I was using this
1 2 3 4 5 6 7 8 9 10 11 12 13 |
--- - 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
1 2 |
#//archive/Pipeline |
is there a way to glob-capture the rest of the line or just insert the single comment char?
1 2 3 |
regexp: '^//archive/pipeline *' line: '#//archive/pipeline *' |
or
1 2 3 |
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?
Answer:
You can use the replace
module for your case:
1 2 3 4 5 6 7 8 9 10 11 12 |
--- - 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.