[ Ansible ] - How to comment (add #) all lines in file

2,216 views
Skip to first unread message

João Santos

unread,
Feb 20, 2020, 12:25:27 PM2/20/20
to Ansible Project
Hey Ansible gurus, need your help here...

Just googled around and was not able to find a satisfactory answer to the following:
  • I have a file and want to insert in the beginning of each line the # character
  • No matter if it has 10 lines or 1000 lines
  • The final goal would be to no matter which file I said so, will add # to the beginning of line
Maybe Ansible is not the proper tool to do it...  I searched throughout Ansible docs and community webpages and was not able to find an example...

Am I doing something wrong? Again, is not the tool to it?
What would be your experience with such situation? Did you managed to simple use shell module and do it?

Please share your findings with me

Mauricio Tavares

unread,
Feb 20, 2020, 1:13:22 PM2/20/20
to ansible...@googlegroups.com
Stupid question: every single line in the file or a specific set
of lines in the file identified in some way or fashion?

> Please share your findings with me
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cafd3761-45c6-474d-b5bf-fa97014f5187%40googlegroups.com.

João Santos

unread,
Feb 20, 2020, 1:47:30 PM2/20/20
to ansible...@googlegroups.com
To be clear, add # to the beginning of every single line in the file... 10, 1000, 100000 lines... doesnt matter.

Vladimir Botka

unread,
Feb 20, 2020, 1:54:02 PM2/20/20
to João Santos, ansible...@googlegroups.com
On Thu, 20 Feb 2020 09:25:26 -0800 (PST)
João Santos <morgado...@gmail.com> wrote:

> - I have a file and want to insert in the beginning of each line the #
> character
> - No matter if it has 10 lines or 1000 lines
> - The final goal would be to no matter which file I said so, will add #
> to the beginning of line

Use "replace" module. For example
https://docs.ansible.com/ansible/latest/modules/replace_module.html#replace-replace-all-instances-of-a-particular-string-in-a-file-using-a-back-referenced-regular-expression

- replace:
path: "{{ item }}"
regexp: '^(.*)$'
replace: '# \1'
loop: "{{ list_of_files }}"

HTH,

-vlado

João Santos

unread,
Feb 21, 2020, 3:49:36 AM2/21/20
to Vladimir Botka, ansible...@googlegroups.com
Hi, thanks for the reply.

Indeed that suggestion worked out in replacement of using `shell` module (or similar).

Thanks for sharing.

Vladimir Botka

unread,
Feb 21, 2020, 5:17:06 AM2/21/20
to João Santos, ansible...@googlegroups.com
On Fri, 21 Feb 2020 08:48:33 +0000
João Santos <morgado...@gmail.com> wrote:

> Indeed that suggestion worked out in replacement of using `shell` module
> (or similar).
>
> On Thu, 20 Feb 2020 at 18:53, Vladimir Botka <vbo...@gmail.com> wrote:
> >
> > Use "replace" module. For example
> >
> > https://docs.ansible.com/ansible/latest/modules/replace_module.html#replace-replace-all-instances-of-a-particular-string-in-a-file-using-a-back-referenced-regular-expression
> >
> > - replace:
> > path: "{{ item }}"
> > regexp: '^(.*)$'
> > replace: '# \1'
> > loop: "{{ list_of_files }}"

You're welcome. Be careful. It's not idempotent. Each run of the task will
prepend '# '.

HTH,
-vlado

Pawan Kumar

unread,
Feb 21, 2020, 11:59:38 AM2/21/20
to ansible...@googlegroups.com
Vlado you are the champ 🤺

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages