lineinfile module (insertbefore - insertafter - problems)

769 views
Skip to first unread message

lars svenson

unread,
Jul 11, 2013, 1:13:07 PM7/11/13
to ansible...@googlegroups.com
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='^[\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.

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


Thanks in advance :)

C. Morgan Hamill

unread,
Jul 11, 2013, 2:32:21 PM7/11/13
to ansible-project
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.
--
Morgan

Brian Coca

unread,
Jul 11, 2013, 9:41:04 PM7/11/13
to ansible...@googlegroups.com
the assembly and template modules are normally a much better solution than lineinfile.
--
Brian Coca
Stultorum infinitus est numerus
0110000101110010011001010110111000100111011101000010000001111001011011110111010100100000011100110110110101100001011100100111010000100001
Pedo mellon a minno

lars svenson

unread,
Jul 12, 2013, 1:01:40 PM7/12/13
to ansible...@googlegroups.com
Thank you for your fast and helpful answers
Reply all
Reply to author
Forward
0 new messages