# lineinfile module (insertbefore - insertafter - problems)

**URL:** <https://forum.ansible.com/t/lineinfile-module-insertbefore-insertafter-problems/13103>\
**Category:** Ansible Project\
**Created:** [July 11, 2013, 5:13pm UTC](https://forum.ansible.com/t/lineinfile-module-insertbefore-insertafter-problems/13103 "2013-07-11T17:13:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lars\_svenson](https://avatars.discourse-cdn.com/v4/letter/l/b782af/32.png) [@lars\_svenson](https://forum.ansible.com/u/lars_svenson)\
**Post date:** [July 11, 2013, 5:13pm UTC](https://forum.ansible.com/t/lineinfile-module-insertbefore-insertafter-problems/13103/1 "2013-07-11T17:13:07Z")

</div>

Dear community,

I’m using the lineinfile module to insert a line into a vhost file on Debian. But it does not work as I expected. Here’s is the code.

- name: add auth lines to auth via .htpasswd to /etc/apache2/sites-available/$DOMAIN  
action: lineinfile dest=/etc/apache2/sites-available/$DOMAIN  
regexp=‘\[1\]\*allow’  
line=’ AuthType Basic’  
insertafter=‘\[2\]\*allow’  
state=present

Ansible does find the right line, but replaces the " allow"-line with “AuthType Basic”. Nor insertafter, neither insertbefore are working - just replacing.

I’m using ansible 1.2. Any hints or suggestions?

Thanks in advance 🙂

* * *

1. \W 

2. \W

---

<div class="post-metadata">

**Author:** ![C\_Morgan\_Hamill](https://avatars.discourse-cdn.com/v4/letter/c/ebca7d/32.png) [@C\_Morgan\_Hamill](https://forum.ansible.com/u/C_Morgan_Hamill)\
**Post date:** [July 11, 2013, 6:32pm UTC](https://forum.ansible.com/t/lineinfile-module-insertbefore-insertafter-problems/13103/2 "2013-07-11T18:32:21Z")

</div>

Excerpts from lars svenson's message of 2013-07-11 13:13:07 -0400:

> &nbsp;&nbsp;- name: add auth lines to auth via .htpasswd to  
> /etc/apache2/sites-available/$DOMAIN  
> &nbsp;&nbsp;&nbsp;&nbsp;action: lineinfile dest=/etc/apache2/sites-available/$DOMAIN  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp='^[\W]\*allow'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line=' AuthType Basic'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;insertafter='^[\W]\*allow'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state=present
> 
> Ansible does find the right line, but replaces the " allow"-line with  
> "AuthType Basic". Nor insertafter, neither insertbefore are working - just  
> replacing.

The 'regexp' parameter is meant to match the line being inserted, not  
the line you want to insert after. If the regex matches any line in the  
file, then the contents of 'line' will \*not\* be inserted, ensuring  
idempotency. So, in your case, you'd want 'regexp' to be  
' AuthType Basic'.

Note that this will match that line \*anywhere\* in the file, so if it  
occurs more than once, it will not get inserted, even if its occurrence  
is not after the line matching 'lineafter'.

I \*think\* you can get clever here with back-references and whatnot, but  
I'm not positive.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [July 12, 2013, 1:41am UTC](https://forum.ansible.com/t/lineinfile-module-insertbefore-insertafter-problems/13103/3 "2013-07-12T01:41:04Z")

</div>

the assembly and template modules are normally a much better solution than lineinfile.

---

<div class="post-metadata">

**Author:** ![lars\_svenson](https://avatars.discourse-cdn.com/v4/letter/l/b782af/32.png) [@lars\_svenson](https://forum.ansible.com/u/lars_svenson)\
**Post date:** [July 12, 2013, 5:01pm UTC](https://forum.ansible.com/t/lineinfile-module-insertbefore-insertafter-problems/13103/4 "2013-07-12T17:01:40Z")

</div>

Thank you for your fast and helpful answers
