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

**URL:** <https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288>\
**Category:** Ansible Project\
**Created:** [February 20, 2020, 5:25pm UTC](https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288 "2020-02-20T17:25:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_Santos](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Joao\_Santos](https://forum.ansible.com/u/Joao_Santos)\
**Post date:** [February 20, 2020, 5:25pm UTC](https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288/1 "2020-02-20T17:25:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [February 20, 2020, 6:12pm UTC](https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288/2 "2020-02-20T18:12:52Z")

</div>

> 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?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Stupid question: every single line in the file or a specific set  
of lines in the file identified in some way or fashion?

---

<div class="post-metadata">

**Author:** ![Joao\_Santos](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Joao\_Santos](https://forum.ansible.com/u/Joao_Santos)\
**Post date:** [February 20, 2020, 6:46pm UTC](https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288/3 "2020-02-20T18:46:38Z")

</div>

To be clear, add # to the beginning of every single line in the file… 10, 1000, 100000 lines… doesnt matter.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 20, 2020, 6:53pm UTC](https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288/4 "2020-02-20T18:53:41Z")

</div>

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](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)

&nbsp;&nbsp;&nbsp;&nbsp;- replace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: "{{ item }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^(.\*)$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: '# \1'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ list\_of\_files }}"

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Joao\_Santos](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Joao\_Santos](https://forum.ansible.com/u/Joao_Santos)\
**Post date:** [February 21, 2020, 8:48am UTC](https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288/5 "2020-02-21T08:48:33Z")

</div>

Hi, thanks for the reply.

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

Thanks for sharing.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [February 21, 2020, 10:16am UTC](https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288/6 "2020-02-21T10:16:38Z")

</div>

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

HTH,  
&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Pawan\_Kumar1](https://avatars.discourse-cdn.com/v4/letter/p/b782af/32.png) [@Pawan\_Kumar1](https://forum.ansible.com/u/Pawan_Kumar1)\
**Post date:** [February 21, 2020, 4:59pm UTC](https://forum.ansible.com/t/ansible-how-to-comment-add-all-lines-in-file/32288/7 "2020-02-21T16:59:03Z")

</div>

Vlado you are the champ 🤺
