Regexp failure: Unmatched group

38 views
Skip to first unread message

John Harmon

unread,
Nov 5, 2018, 3:56:31 PM11/5/18
to Ansible Project
I am getting a failure with the latter half of my task.  I replaced the working stuff with ....
According to a regexp tester, every group does have something, and I don't have empty groups.  https://regex101.com/r/MNkwaD/1
I changed stdout_callback to minimal, but it didn't look much different.
I am so new to regexp I could use another pair of eyes.  I don't know what else to look for here.

Code:
    - name: Sanitize vm.cfg step 2 of 2
      lineinfile
:
        path
: "{{ file }}"
        regexp
: "{{ item.regexp }}"
        line
: "{{ item.line }}"
        backrefs
: "{{ item.backrefs }}"
      with_items
:
       
....
       
- regexp: (^disk\W+\[)?('file:[^']+'(,)?)?('phy:[^']+'(,)?)?(])?
          line
: \1\2\3\6
          backrefs
: yes

Failure:
failed: [ovmftp] (item={u'regexp': u"(^disk\\W+\\[)?('file:[^']+'(,)?)?('phy:[^']+'(,)?)?(])?", u'line': u'\\1\\2\\3\\6\\5\\4', u'backrefs': True}) => {"changed": false, "item": {"backrefs": true, "line": "\\1\\2\\3\\6\\5\\4", "regexp": "(^disk\\W+\\[)?('file:[^']+'(,)?)?('phy:[^']+'(,)?)?(])?"}, "module_stderr": "tput: No value for $TERM and no -T specified\ntput: No value for $TERM and no -T specified\nTraceback (most recent call last):\n  File \"<stdin>\", line 113, in <module>\n  File \"<stdin>\", line 105, in _ansiballz_main\n  File \"<stdin>\", line 48, in invoke_module\n  File \"/tmp/ansible_lineinfile_payload_Wm2J_Z/__main__.py\", line 520, in <module>\n  File \"/tmp/ansible_lineinfile_payload_Wm2J_Z/__main__.py\", line 511, in main\n  File \"/tmp/ansible_lineinfile_payload_Wm2J_Z/__main__.py\", line 300, in present\n  File \"/usr/lib64/python2.7/re.py\", line 266, in _expand\n    return sre_parse.expand_template(template, match)\n  File \"/usr/lib64/python2.7/sre_parse.py\", line 800, in expand_template\n    raise error, \"unmatched group\"\nsre_constants.error: unmatched group\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}


Kai Stian Olstad

unread,
Nov 5, 2018, 4:23:31 PM11/5/18
to ansible...@googlegroups.com
On Monday, 5 November 2018 21:56:31 CET John Harmon wrote:
> I am getting a failure with the latter half of my task. I replaced the
> working stuff with ....
> According to a regexp tester, every group does have something, and I don't
> have empty groups. https://regex101.com/r/MNkwaD/1
> I changed stdout_callback to minimal, but it didn't look much different.
> I am so new to regexp I could use another pair of eyes. I don't know what
> else to look for here.
>
> Code:
> - name: Sanitize vm.cfg step 2 of 2
> lineinfile:
> path: "{{ file }}"
> regexp: "{{ item.regexp }}"
> line: "{{ item.line }}"
> backrefs: "{{ item.backrefs }}"
> with_items:
> ....
> - regexp: (^disk\W+\[)?('file:[^']+'(,)?)?('phy:[^']+'(,)?)?(])?
> line: \1\2\3\6
> backrefs: yes

So you have a regexp that isn't working, but haven't told us what you are trying to do, so it's actually impossible to help you.


--
Kai Stian Olstad


John Harmon

unread,
Nov 5, 2018, 4:28:29 PM11/5/18
to Ansible Project

So you have a regexp that isn't working, but haven't told us what you are trying to do, so it's actually impossible to help you.


--
Kai Stian Olstad


In the link I provided there is a sample string.  I capture 6 different groups.  In short, I am trying to remove all phy devices strings from the line.  From my line, you can see I am trying to only retain groups 1,2,3 and 6.  When complete, I should have a new "disk = ['file.... ]" string that only contains devices being referenced by a file instead of a physical device.

My apologies that I wasn't more clear.

Kai Stian Olstad

unread,
Nov 5, 2018, 5:35:17 PM11/5/18
to ansible...@googlegroups.com
On Monday, 5 November 2018 22:28:29 CET John Harmon wrote:
>
> > So you have a regexp that isn't working, but haven't told us what you are
> > trying to do, so it's actually impossible to help you.
> >
> In the link I provided there is a sample string. I capture 6 different
> groups. In short, I am trying to remove all phy devices strings from the
> line. From my line, you can see I am trying to only retain groups 1,2,3
> and 6. When complete, I should have a new "disk = ['file.... ]" string
> that only contains devices being referenced by a file instead of a physical
> device.

If you that is the only place you have phy: replace module would be a lot easier.

- replace:
path: /path/to/file
regexp: (, )?'phy:[^']+'
replace: ''


--
Kai Stian Olstad


John Harmon

unread,
Nov 5, 2018, 5:42:24 PM11/5/18
to Ansible Project

If you that is the only place you have phy: replace module would be a lot easier.

  - replace:
      path: /path/to/file
      regexp: (, )?'phy:[^']+'
      replace: ''


--
Kai Stian Olstad



Yep, you are right.  That is much easier.  I'll go that route.  Out of curiosity do you think it is a syntax issue with my hodgepodge of groups causing the "unmatched group" error? or a bug?

Kai Stian Olstad

unread,
Nov 6, 2018, 7:19:46 AM11/6/18
to ansible...@googlegroups.com
On 05.11.2018 23:42, John Harmon wrote:
>> If you that is the only place you have phy: replace module would be a
>> lot
>> easier.
>>
>> - replace:
>> path: /path/to/file
>> regexp: (, )?'phy:[^']+'
>> replace: ''
>>
>>
> Yep, you are right. That is much easier. I'll go that route. Out of
> curiosity do you think it is a syntax issue with my hodgepodge of
> groups
> causing the "unmatched group" error? or a bug?

I don't think it's a bug, more in the line it's impossible or at least
very hard to write regexp to cover that string.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages