# replace module:  Invalid group reference

**URL:** <https://forum.ansible.com/t/replace-module-invalid-group-reference/25564>\
**Category:** Ansible Project\
**Created:** [September 21, 2017, 6:13pm UTC](https://forum.ansible.com/t/replace-module-invalid-group-reference/25564 "2017-09-21T18:13:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 21, 2017, 6:13pm UTC](https://forum.ansible.com/t/replace-module-invalid-group-reference/25564/1 "2017-09-21T18:13:57Z")

</div>

I am trying to append to some lines within the nsswitch file to setup sssd. I am trying this with the loop below, but I have never used backrefs before. I am unsure if I am doing it correctly. I get an error about an invalid group reference. I wonder if my regexp is wrong?

Code

`

- name: Update nsswitch.conf  
replace:  
path: /etc/nsswitch.conf  
regexp: “{{item.regexp}}”  
replace: “{{item.replace}}”  
with\_items:
- {regexp: ‘^passwd.\*’,  
replace: ‘\1 sss’}
- {regexp: ‘^group.\*’,  
replace: ‘\1 sss’}  
notify: restart sssd

`

Traceback results

`

The full traceback is:  
Traceback (most recent call last):  
File “/tmp/ansible\_q0xtoH/ansible\_module\_replace.py”, line 200, in   
main()  
File “/tmp/ansible\_q0xtoH/ansible\_module\_replace.py”, line 173, in main  
result = re.subn(mre, params[‘replace’], contents, 0)  
File “/usr/lib64/python2.6/re.py”, line 162, in subn  
return \_compile(pattern, 0).subn(repl, string, count)  
File “/usr/lib64/python2.6/re.py”, line 278, in filter  
return sre\_parse.expand\_template(template, match)  
File “/usr/lib64/python2.6/sre\_parse.py”, line 795, in expand\_template  
raise error, “invalid group reference”  
sre\_constants.error: invalid group reference

failed: [ansibletest-oel6] (item={u’regexp’: u’^group._‘, u’replace’: u’\1 sss’}) =\> {  
“failed”: true,  
“item”: {  
“regexp”: "^group._",  
“replace”: “\1 sss”  
},  
“module\_stderr”: “Traceback (most recent call last):\n File "/tmp/ansible\_q0xtoH/ansible\_module\_replace.py", line 200, in \n main()\n File "/tmp/ansible\_q0xtoH/ansible\_module\_replace.py", line 173, in main\n result = re.subn(mre, params[‘replace’], contents, 0)\n File "/usr/lib64/python2.6/re.py", line 162, in subn\n return \_compile(pattern, 0).subn(repl, string, count)\n File "/usr/lib64/python2.6/re.py", line 278, in filter\n return sre\_parse.expand\_template(template, match)\n File "/usr/lib64/python2.6/sre\_parse.py", line 795, in expand\_template\n raise error, "invalid group reference"\nsre\_constants.error: invalid group reference\n”,  
“module\_stdout”: “”,  
“msg”: “MODULE FAILURE”,  
“rc”: 1  
}

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [September 21, 2017, 6:17pm UTC](https://forum.ansible.com/t/replace-module-invalid-group-reference/25564/2 "2017-09-21T18:17:30Z")

</div>

You haven’t actually created a match group in your regex. I assume you want something like ^(group.\*) or something. The () create the match group.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 21, 2017, 6:20pm UTC](https://forum.ansible.com/t/replace-module-invalid-group-reference/25564/3 "2017-09-21T18:20:05Z")

</div>

Perfect. Thanks Matt! I am really horrible at regexp. I totally missed grouping it. I tested it and it works. Thanks again.
