replace module: Invalid group reference

2,325 views
Skip to first unread message

John Harmon

unread,
Sep 21, 2017, 2:13:57 PM9/21/17
to Ansible Project
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 <module>
    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 <module>\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
}


Matt Martz

unread,
Sep 21, 2017, 2:18:20 PM9/21/17
to ansible...@googlegroups.com
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.

--
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-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4d26cef9-107d-4f41-967e-dcb52328491b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matt Martz
@sivel
sivel.net

John Harmon

unread,
Sep 21, 2017, 2:20:05 PM9/21/17
to Ansible Project
Perfect.  Thanks Matt!  I am really horrible at regexp.  I totally missed grouping it.  I tested it and it works.  Thanks again.
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.
Reply all
Reply to author
Forward
0 new messages