Remove '^#' comments from lookup(file)

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?

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.

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?

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

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