# lineinfile:  replacing line instead of appending the line

**URL:** <https://forum.ansible.com/t/lineinfile-replacing-line-instead-of-appending-the-line/30618>\
**Category:** Ansible Project\
**Created:** [June 20, 2019, 6:37pm UTC](https://forum.ansible.com/t/lineinfile-replacing-line-instead-of-appending-the-line/30618 "2019-06-20T18:37:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dave\_McPherson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dave_mcpherson/32/6973_2.png) [@Dave\_McPherson](https://forum.ansible.com/u/Dave_McPherson)\
**Post date:** [June 20, 2019, 6:37pm UTC](https://forum.ansible.com/t/lineinfile-replacing-line-instead-of-appending-the-line/30618/1 "2019-06-20T18:37:44Z")

</div>

I am attempting to add a group to the sudoers file after the line  
%wheel ALL=(ALL) ALL  
example

## Allows people in group wheel to run all commands

%wheel ALL=(ALL) ALL

doman\vm\_admins ALL-(ALL) ALL

- name: add vm\_admins to sudoers  
lineinfile:  
path: /etc/sudoers  
regexp: “^%wheel”  
insertafter: “^%wheel”  
line: ‘{{realm\_name.realm}}\vm\_admins ALL=(ALL) ALL’  
backup: yes

The result is that ansible is replacing the line with the new line instead of adding it after.

example

## Allows people in group wheel to run all commands

doman\vm\_admins ALL-(ALL) ALL

Any help would be appreciated.

Thanks,

Dave

---

<div class="post-metadata">

**Author:** ![Angel\_Rengifo\_Cancin](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Angel\_Rengifo\_Cancin](https://forum.ansible.com/u/Angel_Rengifo_Cancin)\
**Post date:** [June 20, 2019, 7:08pm UTC](https://forum.ansible.com/t/lineinfile-replacing-line-instead-of-appending-the-line/30618/2 "2019-06-20T19:08:30Z")

</div>

Hi:

> I am attempting to add a group to the sudoers file after the line  
> %wheel ALL=(ALL) ALL  
> example
> 
> ## Allows people in group wheel to run all commands
> 
> %wheel ALL=(ALL) ALL
> 
> doman\vm\_admins ALL-(ALL) ALL
> 
> - name: add vm\_admins to sudoers  
> lineinfile:  
> path: /etc/sudoers  
> regexp: “^%wheel”  
> insertafter: “^%wheel”  
> line: ‘{{realm\_name.realm}}\vm\_admins ALL=(ALL) ALL’  
> backup: yes

I haven’t used “inserafter” option before, but according to documentation:

[https://docs.ansible.com/ansible/latest/modules/lineinfile\_module.html](https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html)

“… If regular expressions are passed to both regexp and insertafter, insertafter is only honored if no match for regexp is found.”

It seems that when regexp and inserafter use regular expressions, inserafter will work only if regexp didn’t match:

What if you try to comment the regexp option and use only inserafter?

---

<div class="post-metadata">

**Author:** ![Dave\_McPherson](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dave_mcpherson/32/6973_2.png) [@Dave\_McPherson](https://forum.ansible.com/u/Dave_McPherson)\
**Post date:** [June 20, 2019, 7:38pm UTC](https://forum.ansible.com/t/lineinfile-replacing-line-instead-of-appending-the-line/30618/3 "2019-06-20T19:38:44Z")

</div>

I commented out the regex line but got the same result. the line was added to the end of the file.

Dave

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [June 20, 2019, 7:40pm UTC](https://forum.ansible.com/t/lineinfile-replacing-line-instead-of-appending-the-line/30618/4 "2019-06-20T19:40:38Z")

</div>

In my opinion, lineinfile should rarely be used, except in special cases where it is the only option.

At the point in time, most linux distros support an /etc/sudoers.d directory, which can contain individual files for all added sudoers rules.

Use the copy or template module to drop a file in that directory.

Barring that, I’d recommend, templating out the whole /etc/sudoers file, instead of trying to manipulate individual lines within it.

---

<div class="post-metadata">

**Author:** ![Angel\_Rengifo\_Cancin](https://avatars.discourse-cdn.com/v4/letter/a/9e8a1a/32.png) [@Angel\_Rengifo\_Cancin](https://forum.ansible.com/u/Angel_Rengifo_Cancin)\
**Post date:** [June 20, 2019, 7:49pm UTC](https://forum.ansible.com/t/lineinfile-replacing-line-instead-of-appending-the-line/30618/5 "2019-06-20T19:49:38Z")

</div>

> In my opinion, lineinfile should rarely be used, except in special cases where it is the only option.

I agree

> At the point in time, most linux distros support an /etc/sudoers.d directory, which can contain individual files for all added sudoers rules.
> 
> Use the copy or template module to drop a file in that directory.
> 
> Barring that, I’d recommend, templating out the whole /etc/sudoers file, instead of trying to manipulate individual lines within it.

Here’s a good post about sudo configuration with ansible and jinja2:

[https://www.reddit.com/r/ansible/comments/5xzj8b/how\_to\_modify\_sudoers\_file\_with\_ansible/](https://www.reddit.com/r/ansible/comments/5xzj8b/how_to_modify_sudoers_file_with_ansible/)

Hope it helps
