# playbook to comment out one line and add another line in a file

**URL:** <https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588>\
**Category:** Ansible Project\
**Created:** [May 3, 2022, 5:10pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588 "2022-05-03T17:10:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 3, 2022, 5:10pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/1 "2022-05-03T17:10:06Z")

</div>

how do i use ansible to remotely comment out a line in a docker-compose file and add a line

like this

#image: gralog/graylog:4.2.5  
image: graylog/graylog4.2.8

then do a docker compose down  
and docker-compose up -d

i need to run this on multiple nodes

---

<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:** [May 3, 2022, 6:52pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/2 "2022-05-03T18:52:02Z")

</div>

I assume, the expected result is below. If not confirm your example  
and ignore the rest here

#image: graylog/graylog4.2.5  
image: graylog/graylog4.2.8

For example, create a dictionary of the disabled and enabled versions

&nbsp;&nbsp;&nbsp;&nbsp;versions:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.2.5: false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.2.8: true

Then the \*lineinfile\* task below should comment out disabled versions  
and add enabled versions

&nbsp;&nbsp;&nbsp;&nbsp;- lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: docker-compose  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regex: '^\s\*#\*\s\*image: graylog/graylog{{ item.key }}\s\*$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '{{ hash }}image: graylog/graylog{{ item.key }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ versions|dict2items }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash: "{{ item.value|ternary('', '#') }}"

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 4, 2022, 8:50pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/3 "2022-05-04T20:50:31Z")

</div>

do ytou mean like this

---

<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:** [May 4, 2022, 10:13pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/4 "2022-05-04T22:13:52Z")

</div>

See the complete example below

> cat docker-compose

image: graylog/graylog4.2.5

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 5, 2022, 2:09am UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/5 "2022-05-05T02:09:28Z")

</div>

Got it, thanks!

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 5, 2022, 5:03pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/6 "2022-05-05T17:03:02Z")

</div>

I was abl to get my first node changed but the other 2 had this added

image: gralog/graylog:4.2.5  
#image: graylog/graylog4.2.5  
image: graylog/graylog4.2.8

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 5, 2022, 9:27pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/7 "2022-05-05T21:27:16Z")

</div>

I don’t think you can get `line_in_file` to do what you want. `line_in_file` is designed to operate on a single line. Keeping the commented out line greatly complicates matters. How many patchlevels to you intend to maintain as comments?

You may be able to do it with `replace` and a lot of testing. But I’d strongly encourage you to reconsider using `#` as a poor replacement for revision control.

---

<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:** [May 5, 2022, 10:51pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/8 "2022-05-05T22:51:39Z")

</div>

'y' is missing in 'gralog/graylog:4.2.5' in the first line. This is  
the reason why the first line wasn't replaced. Instead, the second  
line was added.

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 6, 2022, 2:21am UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/9 "2022-05-06T02:21:47Z")

</div>

it only works for first node. When i run the playbook against 3 nodes the 2nd and 3rd node have this

image: graylog/graylog:4.2.5  
#image: graylog/graylog4.2.5  
image: graylog/graylog4.2.8

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 6, 2022, 2:22am UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/10 "2022-05-06T02:22:49Z")

</div>

this is the playbook

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 6, 2022, 2:41am UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/11 "2022-05-06T02:41:32Z")

</div>

I tested with another pb and it works fine on all 3 nodes

---

<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:** [May 6, 2022, 4:44am UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/12 "2022-05-06T04:44:22Z")

</div>

> it only works for first node. When i run the playbook against 3 nodes the  
> 2nd and 3rd node have this
> 
> image: graylog/graylog:4.2.5  
> #image: graylog/graylog4.2.5  
> image: graylog/graylog4.2.8

Quoting from \*lineinfile\* parameter \*regexp\*  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile\_module.html#parameter-regexp](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-regexp)

&nbsp;&nbsp;For state=present, the pattern to replace if found. Only the last  
&nbsp;&nbsp;line found will be replaced.  
&nbsp;&nbsp;For state=absent, the pattern of the line(s) to remove.

Note the difference between the states \*present/absent\*. If \*absent\*  
all matches are removed. If \*present\* only the last one is replaced.  
The default state is \*present\*.

One of the scenarios on how to proceed might be removing all redundant  
lines first. For example, given the file

> cat docker-compose

image: graylog/graylog4.2.5  
#image: graylog/graylog4.2.5  
image: graylog/graylog4.2.8

The playbook

> cat pb.yml

- hosts: localhost  
&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;versions:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.2.5: false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.2.8: true  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Remove versions  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: docker-compose  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^\s\*#\*\s\*image: graylog/graylog{{ item.key }}\s\*$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ versions|dict2items }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: force\_remove\_versions|d(false)|bool  
&nbsp;&nbsp;&nbsp;&nbsp;- name: Add versions  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: docker-compose  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^\s\*#\*\s\*image: graylog/graylog{{ item.key }}\s\*$'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '{{ hash }}image: graylog/graylog{{ item.key }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ versions|dict2items }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash: "{{ item.value|ternary('', '#') }}"

removes all matching lines and creates the specified ones

> ansible-playbook pb.yml -e force\_remove\_versions=true

PLAY [localhost]

---

<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:** [May 6, 2022, 4:50am UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/13 "2022-05-06T04:50:10Z")

</div>

> \> cat pb.yml  
> - hosts: localhost  
> &nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;versions:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.2.5: false  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4.2.8: true  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Remove versions  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: docker-compose  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^\s\*#\*\s\*image: graylog/graylog{{ item.key }}\s\*$'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ versions|dict2items }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: force\_remove\_versions|d(false)|bool  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Add versions  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: docker-compose  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '^\s\*#\*\s\*image: graylog/graylog{{ item.key }}\s\*$'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line: '{{ hash }}image: graylog/graylog{{ item.key }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop: "{{ versions|dict2items }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hash: "{{ item.value|ternary('', '#') }}"

The playbook is idempotent

> cat docker-compose

#image: graylog/graylog4.2.5  
image: graylog/graylog4.2.8

> ansible-playbook pb.yml

PLAY [localhost]

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 6, 2022, 12:48pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/14 "2022-05-06T12:48:11Z")

</div>

i got it to work the simple way with multiple tasks

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 6, 2022, 12:52pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/15 "2022-05-06T12:52:10Z")

</div>

I have another requirement to down the container on node01 first

docker-compose down  
then run the pb,  
then docker-compose up -d

wait 30secs or so

repeat with node02  
repeat with node03

is there docker module or I should just use shell module?

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [May 6, 2022, 1:57pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/16 "2022-05-06T13:57:52Z")

</div>

I believe it’s always better to use designated Modules over or that said … you may want to get familiar (or at least be aware of ) where you can find any modules. that said … here is the (from that list) you can study it online or with the built-in help like one last comment … just like using designated modules for designated tasks in Ansible it’s always best to use designated Threads for different problems. This may not make a difference for you just now, but others who may be following the list have a chance to benefit from the solution somebody is digging out

---

<div class="post-metadata">

**Author:** ![Tony\_Wong](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tony\_Wong](https://forum.ansible.com/u/Tony_Wong)\
**Post date:** [May 6, 2022, 8:36pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/17 "2022-05-06T20:36:02Z")

</div>

does this mean I cannot keep the old commented line?

#image: graylog/graylog4.2.5

requirement is I need to have this line commented

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [May 6, 2022, 8:54pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/18 "2022-05-06T20:54:20Z")

</div>

Assuming you only have one line for each version, it will assure that the line for any version that’s marked false will be commented, and any line for a version marked true will be uncommented.

Where it gets hairy is when you have more than one line for any version. In that case, the second task only manages the first of the lines for that version. That shouldn’t happen, but you’ve already seen that it does sometimes. When it does, you can run the playbook with `force_remove_versions` set to true, and it will remove all the matching lines - both good and bad - before the second task puts the single entries back in the file, properly commented or not as the case may be.

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [May 6, 2022, 9:27pm UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/19 "2022-05-06T21:27:32Z")

</div>

> does this mean I cannot keep the old commented line?
> 
> #image: graylog/graylog4.2.5

It's tricky. You can write scripting to do anything you want to do,  
but it's not necessarily built-in precisely the way you want it to  
behave.

It's why many config files, such as those in /etc/sudoers.d/, deploy  
files rather than editing lines of /etc/sudoers.

---

<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:** [May 7, 2022, 3:34am UTC](https://forum.ansible.com/t/playbook-to-comment-out-one-line-and-add-another-line-in-a-file/36588/20 "2022-05-07T03:34:18Z")

</div>

Quoting from \*lineinfile\* parameter \*regexp\*  
[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile\_module.html#parameter-regexp](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-regexp)

&nbsp;&nbsp;For state=present, the pattern to replace if found. Only the last  
&nbsp;&nbsp;line found will be replaced.

Thank you!

> Assuming you only have one line for each version, it will assure  
> that the line for any version that's marked false will be  
> commented, and any line for a version marked true will be  
> uncommented.  
> Where it gets hairy is when you have more than one line for any  
> version. In that case, the second task only manages the first of

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^^^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last
