lineinfile module (insertbefore - insertafter - problems)

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 :slight_smile:


  1. \W ↩︎

  2. \W ↩︎

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

  - name: add auth lines to auth via .htpasswd to
/etc/apache2/sites-available/$DOMAIN
    action: lineinfile dest=/etc/apache2/sites-available/$DOMAIN
               regexp='^[\W]*allow'
               line=' AuthType Basic'
               insertafter='^[\W]*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.

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.

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

Thank you for your fast and helpful answers