Commenting out a line with Ansible lineinfile module

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:

What I want is to change it to

I was using this

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

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

or

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:

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.

Leave a Reply