# using regex in replace module

**URL:** <https://forum.ansible.com/t/using-regex-in-replace-module/29256>\
**Category:** Ansible Project\
**Created:** [December 13, 2018, 10:23am UTC](https://forum.ansible.com/t/using-regex-in-replace-module/29256 "2018-12-13T10:23:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danish09](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@danish09](https://forum.ansible.com/u/danish09)\
**Post date:** [December 13, 2018, 10:23am UTC](https://forum.ansible.com/t/using-regex-in-replace-module/29256/1 "2018-12-13T10:23:25Z")

</div>

Hey there,

I am using ansible 2.6.4. In on the projects I was working on, I had to search for 2 different words in the same file and replace them as necessary. Used **replace** module 2 times for this. Below is the playbook that I have used.

- name: backup conf file and search and replace regex  
replace:  
path: /etc/nginx/sites-available/cruk-eb  
regexp: ‘cert.cruk.org.crt’  
replace: ‘cert3.pem’  
backup: yes

- name: backup conf file and search and replace regex again  
replace:  
path: /etc/nginx/sites-available/cruk-eb  
regexp: ‘privkey.cruk.org.key.pem’  
replace: ‘privkey3.pem’  
backup: yes

Is there a way that I can search and replace 2 words in one task/play

Pleas let me know if there is any other information that you need. And apologies if I have missed out anything.

Thanks

Danish.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 13, 2018, 11:43am UTC](https://forum.ansible.com/t/using-regex-in-replace-module/29256/2 "2018-12-13T11:43:16Z")

</div>

This should do it:

&nbsp;&nbsp;&nbsp;&nbsp;- name: backup conf file and search and replace regex  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path: /etc/nginx/sites-available/cruk-eb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: '(privkey|cert)\.cruk\.org\.(key\.pem|crt)'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: '\g\<1\>3.pem'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: yes

Dick

---

<div class="post-metadata">

**Author:** ![danish09](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@danish09](https://forum.ansible.com/u/danish09)\
**Post date:** [December 13, 2018, 4:42pm UTC](https://forum.ansible.com/t/using-regex-in-replace-module/29256/3 "2018-12-13T16:42:18Z")

</div>

amazing. thanks a lot. tried it just now and it did the magic. However, tried to give it a little thought. I am assuming 1 is for the expression with in the first bracket (). But since it is an OR ‘|’, how does it know which word needs to be replaced with what and in which line. I mean could it not be possible that the words are replaced as privkey3.pem in the first line and cert3.pem in the second line., the other way round.

Hope I am clear

Thanks

Danish

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 13, 2018, 5:24pm UTC](https://forum.ansible.com/t/using-regex-in-replace-module/29256/4 "2018-12-13T17:24:02Z")

</div>

> amazing. thanks a lot. tried it just now and it did the magic. However, tried to give it a little thought. I am assuming 1 is for the expression with in the first bracket (). But since it is an OR '|', how does it know which word needs to be replaced with what and in which line. I mean could it not be possible that the words are replaced as privkey3.pem in the first line and cert3.pem in the second line., the other way round.

No. The pipe (OR) means it can match cert.cruk.org.something or  
privkey.cruk.org.something.  
Because of the parentheses around it, it can be used later on in the  
replacement, this is called backreferencing.  
And because it's the first match, it will normally be referenced in  
the replacement string by '\1'.  
However, because you need a number directly after that, this would  
mean '\13` - which is interpreted as the 13th match and will fail.  
We therefore have to use a numbered group to reference it, which is the '\g\<1\>'.  
The second pair of parentheses also has an OR, meaning that the  
extension can either be 'key.pem', or just 'crt'.  
Your example doesn't require using that in the replacement later on,  
so there is no '\2' or anything.

Dick

---

<div class="post-metadata">

**Author:** ![danish09](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@danish09](https://forum.ansible.com/u/danish09)\
**Post date:** [December 14, 2018, 10:13am UTC](https://forum.ansible.com/t/using-regex-in-replace-module/29256/5 "2018-12-14T10:13:07Z")

</div>

brilliant. thank you very much. finally seem to be making sense of regular expressions some what but still have a long way to go

Thanks again.
