John_Harmon
(John Harmon)
September 21, 2017, 11:08pm
1
This works under a regexp tester, but fails under ansible. I don’t know how to correct it.
Basically, it finds the lines starting with passwd or group, looks in the lines for sss, and appends if it isn’t found.
`
name: Update nsswitch.conf
replace:
path: /etc/nsswitch.conf
regexp: “{{item.regexp}}”
replace: “{{item.replace}}”
with_items:
{regexp: ‘^(?:passwd):\s+(?:(?!sss).)+$’, replace: ‘\0 sss’}
{regexp: ‘^(?:group):\s+(?:(?!sss).)+$’, replace: ‘\0 sss’}
notify: restart sssd
`
Actual results (it wipes out the match and adds " sss")
`
Example:
#passwd: db files nisplus nis
#shadow: db files nisplus nis
#group: db files nisplus nis
sss
shadow: files
sss
#hosts: db files nisplus nis dns
hosts: files dns
`
This works under a regexp tester, but fails under ansible. I don't know
how to correct it.
You need to use a Python based one, since Ansible is using Python re.
Basically, it finds the lines starting with passwd or group, looks in the
lines for sss, and appends if it isn't found.
- name: Update nsswitch.conf
replace:
path: /etc/nsswitch.conf
regexp: "{{item.regexp}}"
replace: "{{item.replace}}"
with_items:
- {regexp: '^(?:passwd):\s+(?:(?!sss).)+$', replace: '\0 sss'}
- {regexp: '^(?:group):\s+(?:(?!sss).)+$', replace: '\0 sss'}
\0 is interpreted as octal in Python re.
Try this instead
- {regexp: '(^(?:passwd):\s+(?:(?!sss).)+$)', replace: '\1 sss'}
- {regexp: '(^(?:group):\s+(?:(?!sss).)+$)', replace: '\1 sss'}
I added a parenthesis around the whole expression and references that as \1
Kai Stian Olstad, thank you! You are right, that worked.
I tried my previous one at https://regex101.com/ and selected Python, but I guess it isn’t 100% the same. I will have to keep that in mind.