# Help with lineinfile and backrefs (v1.1)

**URL:** <https://forum.ansible.com/t/help-with-lineinfile-and-backrefs-v1-1/12815>\
**Category:** Ansible Project\
**Created:** [May 17, 2013, 1:31am UTC](https://forum.ansible.com/t/help-with-lineinfile-and-backrefs-v1-1/12815 "2013-05-17T01:31:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ian\_Dexter\_Marquez](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@Ian\_Dexter\_Marquez](https://forum.ansible.com/u/Ian_Dexter_Marquez)\
**Post date:** [May 17, 2013, 1:31am UTC](https://forum.ansible.com/t/help-with-lineinfile-and-backrefs-v1-1/12815/1 "2013-05-17T01:31:55Z")

</div>

Hi,

I can’t seem to make this work from the command line:

> ansible -m lineinfile -a “dest=/etc/ssh/sshd\_config backup=yes backrefs=yes regexp=‘^(AllowGroups .\*)$’ line=‘\1 groupname’” -vvv -s -K local

which replaces the whole “AllowGroups .\*” line with " wheel".

The playbook entry works fine, though:

> lineinfile dest=/etc/ssh/sshd\_config backup=yes backrefs=yes regexp=“^(AllowGroups .\*)$” line=“\1 $groupname”

I’m most likely escaping it wrong? Any pointers will be greatly appreciated. Thanks.

- Ian

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 17, 2013, 6:01pm UTC](https://forum.ansible.com/t/help-with-lineinfile-and-backrefs-v1-1/12815/2 "2013-05-17T18:01:35Z")

</div>

I’m only skimming this, but

.\*

is a greedy expression so you will want to terminate it with something?

I’m also not sure what you think “$groupname” is but it’s not something Ansible gives you.

---

<div class="post-metadata">

**Author:** ![Andreas\_Piesk](https://avatars.discourse-cdn.com/v4/letter/a/d78d45/32.png) [@Andreas\_Piesk](https://forum.ansible.com/u/Andreas_Piesk)\
**Post date:** [May 17, 2013, 6:29pm UTC](https://forum.ansible.com/t/help-with-lineinfile-and-backrefs-v1-1/12815/3 "2013-05-17T18:29:13Z")

</div>

switch the quotes. quote the argument string with single quotes to keep the shell from messing with it:

ansible -m lineinfile -a 'dest=/etc/ssh/sshd\_config backup=yes backrefs=yes regexp="^(AllowGroups .\*)$" line="\\1 $groupname"' -vvv -s -K local

regards,  
-ap

---

<div class="post-metadata">

**Author:** ![PhilEv](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@PhilEv](https://forum.ansible.com/u/PhilEv)\
**Post date:** [May 18, 2013, 4:04pm UTC](https://forum.ansible.com/t/help-with-lineinfile-and-backrefs-v1-1/12815/4 "2013-05-18T16:04:37Z")

</div>

And this is not idempotent as it append the contents of the $groupname to the AllowGroups declaration  
Not necessarely what you want

Phil

---

<div class="post-metadata">

**Author:** ![Ian\_Dexter\_Marquez](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@Ian\_Dexter\_Marquez](https://forum.ansible.com/u/Ian_Dexter_Marquez)\
**Post date:** [May 23, 2013, 1:47am UTC](https://forum.ansible.com/t/help-with-lineinfile-and-backrefs-v1-1/12815/5 "2013-05-23T01:47:11Z")

</div>

It worked! Thanks.

Admittedly, this is an off-band change to the standard sshd\_config  
file -- necessary for those one-off tasks to provide temporary SSH  
access to certain groups. ("$groupname" is just a placeholder I used;  
it will be replaced by the actual group name when invoked. Sorry for  
the misunderstanding.)

Thanks again, all!
