# Remove '^#' comments from lookup(file)

**URL:** <https://forum.ansible.com/t/remove-comments-from-lookup-file/25136>\
**Category:** Ansible Project\
**Created:** [July 31, 2017, 3:10pm UTC](https://forum.ansible.com/t/remove-comments-from-lookup-file/25136 "2017-07-31T15:10:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thorsten\_Scheurer](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thorsten\_Scheurer](https://forum.ansible.com/u/Thorsten_Scheurer)\
**Post date:** [July 31, 2017, 3:10pm UTC](https://forum.ansible.com/t/remove-comments-from-lookup-file/25136/1 "2017-07-31T15:10:52Z")

</div>

Hello together,

i try to remove all beginnig ‘#’ comments from lookup a file but with no success.  
Actually i only get the first appearance removed but not all comments.

ansible-version: 2.3.2

`  
contents: “{{ lookup(‘file’, ‘/tmp/services’) | regex\_replace(‘^#.\*’, ‘’) }}”

`

Can anybody help?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 31, 2017, 7:18pm UTC](https://forum.ansible.com/t/remove-comments-from-lookup-file/25136/2 "2017-07-31T19:18:12Z")

</div>

I have a hunch that the ^ means the start of the stream(file) in this context. Try remove it and see what happens, since it will remove from # to the first newline everywhere in the stream.

---

<div class="post-metadata">

**Author:** ![Thorsten\_Scheurer](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thorsten\_Scheurer](https://forum.ansible.com/u/Thorsten_Scheurer)\
**Post date:** [August 1, 2017, 8:59am UTC](https://forum.ansible.com/t/remove-comments-from-lookup-file/25136/3 "2017-08-01T08:59:21Z")

</div>

Hello, thanks for your answer. When i remove the ‘^’ it will be removed everywhere in the stream. My goal is just to remove all lines beginning with ‘#’. Do you know how can i do that?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [August 1, 2017, 10:09am UTC](https://forum.ansible.com/t/remove-comments-from-lookup-file/25136/4 "2017-08-01T10:09:31Z")

</div>

For this you will need a multiline matching regular expression.  
This flag isn't something that you can set with the ansible filter.  
Instead, you have to use a somewhat obscure feature of python re  
module, namely inline modifiers.  
See [https://docs.python.org/2/library/re.html](https://docs.python.org/2/library/re.html), the "(?iLmsux)" part.  
In your case this means the 'm' flag:

content: "{{ lookup('file', '/tmp/services') | regex\_replace('(?m)^#.\*', '') }}"

Note that this will lead to empty lines in your file, changing it  
slightly will remove those:

content: "{{ lookup('file', '/tmp/services') |  
regex\_replace('(?m)^#+.\*\\n', '') }}"

Dick

---

<div class="post-metadata">

**Author:** ![Thorsten\_Scheurer](https://avatars.discourse-cdn.com/v4/letter/t/ee7513/32.png) [@Thorsten\_Scheurer](https://forum.ansible.com/u/Thorsten_Scheurer)\
**Post date:** [August 1, 2017, 12:02pm UTC](https://forum.ansible.com/t/remove-comments-from-lookup-file/25136/5 "2017-08-01T12:02:23Z")

</div>

This works perfect. Thanks for the fast help!!!
