replace module: Invalid group reference

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
}

`

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.

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