# regexp help.....

**URL:** <https://forum.ansible.com/t/regexp-help/25572>\
**Category:** Ansible Project\
**Created:** [September 21, 2017, 11:08pm UTC](https://forum.ansible.com/t/regexp-help/25572 "2017-09-21T23:08:32Z")\
**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, 11:08pm UTC](https://forum.ansible.com/t/regexp-help/25572/1 "2017-09-21T23:08:32Z")

</div>

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

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 22, 2017, 5:53am UTC](https://forum.ansible.com/t/regexp-help/25572/2 "2017-09-22T05:53:19Z")

</div>

> 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  
> &nbsp;&nbsp;&nbsp;replace:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /etc/nsswitch.conf  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: "{{item.regexp}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: "{{item.replace}}"  
> &nbsp;&nbsp;&nbsp;with\_items:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {regexp: '^(?:passwd):\s+(?:(?!sss).)+$', replace: '\0 sss'}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- {regexp: '^(?:group):\s+(?:(?!sss).)+$', replace: '\0 sss'}

\0 is interpreted as octal in Python re.

Try this instead

&nbsp;&nbsp;&nbsp;- {regexp: '(^(?:passwd):\s+(?:(?!sss).)+$)', replace: '\1 sss'}  
&nbsp;&nbsp;&nbsp;- {regexp: '(^(?:group):\s+(?:(?!sss).)+$)', replace: '\1 sss'}

I added a parenthesis around the whole expression and references that as \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 22, 2017, 4:13pm UTC](https://forum.ansible.com/t/regexp-help/25572/3 "2017-09-22T16:13:53Z")

</div>

Kai Stian Olstad, thank you! You are right, that worked.  
I tried my previous one at [https://regex101.com/](https://regex101.com/) and selected Python, but I guess it isn’t 100% the same. I will have to keep that in mind.
