# HELP for Replace module regex

**URL:** <https://forum.ansible.com/t/help-for-replace-module-regex/35192>\
**Category:** Ansible Project\
**Created:** [May 19, 2021, 6:41am UTC](https://forum.ansible.com/t/help-for-replace-module-regex/35192 "2021-05-19T06:41:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manisha](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Manisha](https://forum.ansible.com/u/Manisha)\
**Post date:** [May 19, 2021, 6:41am UTC](https://forum.ansible.com/t/help-for-replace-module-regex/35192/1 "2021-05-19T06:41:14Z")

</div>

Hi All,

I am try to figure out the **regex exp** that will catch a key in a yaml file but i am not getting the way to pass through. I hope that you expertise on this can help me.

**Problem Statement:** I have a yaml file (which will be dynamic), where i need to replace a specific key value.

**Example:** in the below yaml file i only need to update **“id”** value which is under **payroll\_emp** in the all the file.

If i am using **regex: id:.\*** this will catch all the id in the file.

**Help Needed:** if there a way that i can have a regex to match the **id** under **payroll\_emp** and change the value id: 1000 to id: 2000 .

NOTE: we can’t use the after and before parameter as this file will be dynamic its not sure what will be before and after **id**

---

<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:** [May 19, 2021, 7:10am UTC](https://forum.ansible.com/t/help-for-replace-module-regex/35192/2 "2021-05-19T07:10:46Z")

</div>

What does your ansible task look like?

---

<div class="post-metadata">

**Author:** ![Manisha](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Manisha](https://forum.ansible.com/u/Manisha)\
**Post date:** [May 19, 2021, 7:25am UTC](https://forum.ansible.com/t/help-for-replace-module-regex/35192/3 "2021-05-19T07:25:34Z")

</div>

Hi Dick,

Thank you for the response.

as you see the commented code where i was trying use the **after: ‘payroll\_emp.\*’** it does not do anything and i use the replace without any guard then all the **id: xxxxx gets change with id: 2000**

---

<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:** [May 19, 2021, 7:41am UTC](https://forum.ansible.com/t/help-for-replace-module-regex/35192/4 "2021-05-19T07:41:10Z")

</div>

Ah Ok, i see.  
Using a string manipulating module like replace is inherently fragile,  
so whatever works now may break tomorrow (as you pointed out).  
Since your input is yaml, I would look into another approach, like  
lookup the file content, parse it as yaml, change the required  
keys/values, and then write the file back.  
This way is more robust in terms of input (the ordering/comments/etc  
won't matter), and also output, as that will be valid yaml.

---

<div class="post-metadata">

**Author:** ![Manisha](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Manisha](https://forum.ansible.com/u/Manisha)\
**Post date:** [May 19, 2021, 7:52am UTC](https://forum.ansible.com/t/help-for-replace-module-regex/35192/5 "2021-05-19T07:52:42Z")

</div>

Agreed to what you have pointed out.

But my apologies as i have lack of knowledge to do such level of parsing as i have just started with ansible.

it will be great help if you can help me on this. Also, a pointer the value that need to be change will be passed using the with\_items: xxxx, as value is fetch in previous task and store as a set fact.

---

<div class="post-metadata">

**Author:** ![Manisha](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Manisha](https://forum.ansible.com/u/Manisha)\
**Post date:** [May 19, 2021, 10:36am UTC](https://forum.ansible.com/t/help-for-replace-module-regex/35192/6 "2021-05-19T10:36:38Z")

</div>

Hi All,

can anyone me out on this. i had been trying to check around the regex but no luck

---

<div class="post-metadata">

**Author:** ![shrasti\_sahni](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@shrasti\_sahni](https://forum.ansible.com/u/shrasti_sahni)\
**Post date:** [May 19, 2021, 2:43pm UTC](https://forum.ansible.com/t/help-for-replace-module-regex/35192/7 "2021-05-19T14:43:10Z")

</div>

Hi Manisha,  
I just looked into your issue and found that you’re using only id for replacement which gives you wrong output so instead of using it use the below syntax

tasks:

- name: Replaces all the IDs from 1000 to 2000.  
replace:  
path:  
regexp: ‘id: 1000’  
replace: ‘id: 2000’

And yes similarly for replacing 1001 with 2001 you’ve to add one more replace module.  
If this is helpful for you then its really grateful for me.

Thanks & Regards,  
Shrasti Sahni
