Can lineinfile insert text into a line?

292 views
Skip to first unread message

John Oliver

unread,
Jul 29, 2014, 1:38:55 PM7/29/14
to ansible...@googlegroups.com
Example:

password    sufficient    pam_unix.so shadow nullok try_first_pass use_authtok

And I want to wind up with:

password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok

And let's assume I don't know what might come after pam_unix.so, and want to preserve whatever's there, and so can't just use line="string of text"

Right now, I'd use 'command' or 'shell' to call sed or awk to accomplish this.  But is there a way to massage ansible into doing this?

Berend de Boer

unread,
Jul 29, 2014, 7:14:58 PM7/29/14
to ansible...@googlegroups.com
lineinfile replaces the entire line. But I think you can use regexp back references to copy text from the match into your line.

Paul Tötterman

unread,
Jul 30, 2014, 10:39:13 AM7/30/14
to ansible...@googlegroups.com
Right now, I'd use 'command' or 'shell' to call sed or awk to accomplish this.  But is there a way to massage ansible into doing this?

Augeas could probably be used (<http://augeas.net/docs/references/lenses/files/pam-aug.html>), but I guess there's no ansible module for augeas yet.

Cheers,
Paul

Marc Patermann

unread,
Jul 30, 2014, 11:26:53 AM7/30/14
to ansible...@googlegroups.com
John,
The second last example for lineinfile does use regex with a backreference:
http://docs.ansible.com/lineinfile_module.html
"- lineinfile: dest=/opt/jboss-as/bin/standalone.conf
regexp='^(.*)Xms(\d+)m(.*)$' line='\1Xms${xms}m\3' backrefs=yes"

For a start something like:
regex='^password sufficient pam_unix.so (.*)' line='password
sufficient pam_unix.so sha512 \1' backrefs=yes

You may have to take care \1 does not contain "sha512", so .* may not be
safe enough.


Marc

Michael DeHaan

unread,
Jul 31, 2014, 4:45:22 PM7/31/14
to ansible...@googlegroups.com
No, you don't want this.

The error handling in aug is atrocious, as is writing lenses for it.   Never going to happen in ansible :)

lineinfile was designed to make it easier, and it's also why we have things like the INI module.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/76ac16ae-5c6d-44de-9711-519e975560d0%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages