# Can lineinfile insert text into a line?

**URL:** <https://forum.ansible.com/t/can-lineinfile-insert-text-into-a-line/15804>\
**Category:** Ansible Project\
**Created:** [July 29, 2014, 5:38pm UTC](https://forum.ansible.com/t/can-lineinfile-insert-text-into-a-line/15804 "2014-07-29T17:38:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Oliver](https://avatars.discourse-cdn.com/v4/letter/j/97f17d/32.png) [@John\_Oliver](https://forum.ansible.com/u/John_Oliver)\
**Post date:** [July 29, 2014, 5:38pm UTC](https://forum.ansible.com/t/can-lineinfile-insert-text-into-a-line/15804/1 "2014-07-29T17:38:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Berend\_de\_Boer](https://avatars.discourse-cdn.com/v4/letter/b/e495f1/32.png) [@Berend\_de\_Boer](https://forum.ansible.com/u/Berend_de_Boer)\
**Post date:** [July 29, 2014, 11:14pm UTC](https://forum.ansible.com/t/can-lineinfile-insert-text-into-a-line/15804/2 "2014-07-29T23:14:58Z")

</div>

lineinfile replaces the entire line. But I think you can use regexp back references to copy text from the match into your line.

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [July 30, 2014, 2:39pm UTC](https://forum.ansible.com/t/can-lineinfile-insert-text-into-a-line/15804/3 "2014-07-30T14:39:13Z")

</div>

> 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](http://augeas.net/docs/references/lenses/files/pam-aug.html)), but I guess there’s no ansible module for augeas yet.

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![Marc\_Patermann](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@Marc\_Patermann](https://forum.ansible.com/u/Marc_Patermann)\
**Post date:** [July 30, 2014, 3:26pm UTC](https://forum.ansible.com/t/can-lineinfile-insert-text-into-a-line/15804/4 "2014-07-30T15:26:43Z")

</div>

John,

John Oliver schrieb (29.07.2014 19:38 Uhr):

> 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?

The second last example for lineinfile does use regex with a backreference:  
[http://docs.ansible.com/lineinfile\_module.html](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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 31, 2014, 8:45pm UTC](https://forum.ansible.com/t/can-lineinfile-insert-text-into-a-line/15804/5 "2014-07-31T20:45:16Z")

</div>

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.
