# replace module: how to replace all matching files

**URL:** <https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020>\
**Category:** Ansible Project\
**Created:** [November 14, 2017, 4:16pm UTC](https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020 "2017-11-14T16:16:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrei\_Baban](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andrei\_Baban](https://forum.ansible.com/u/Andrei_Baban)\
**Post date:** [November 14, 2017, 4:16pm UTC](https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020/1 "2017-11-14T16:16:15Z")

</div>

Hi,

I have a variables.yml file in which I store variables in this format:

patterns:

- file: file\_1.xml  
regexp: 17701  
replace: 17706
- file: file\_2.xml  
regexp: 17701  
replace: 17706
- file: file\_3.xml  
regexp: 17701  
replace: 17706

then, in my playbook I first import these variables:

- name: Read pattern replaces from file and save them as variables  
include\_vars: ~/path\_to\_file/variables.yml

and then do replaces on these files like this:

- name: perform pattern replaces for files  
local\_action:  
module: replace  
follow: yes  
path: ‘~/path\_to\_files/{{ item.file }}’  
regexp: ‘{{ item.regexp }}’  
replace: ‘{{ item.replace }}’  
with\_items:
- “{{ patterns }}”

All works well, but I need to be able to specify files with regexp in the variables.yml file. Something like below:

patterns:

- file: file\_\*.xml  
regexp: 17701  
replace: 17706
- file: other\_file.xml  
regexp: a  
replace: b

This will fail as replace module will output that ‘file\_\*.xml’ cannot be found

I can do this by using shell module and sed command but I’d really like to stick to using the correct modules instead of falling back to bash. Do you have any suggestion on how to achieve something like this?

thanks!

---

<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:** [November 15, 2017, 2:37pm UTC](https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020/2 "2017-11-15T14:37:18Z")

</div>

To this you would need to use the find module to find the files matching the filename regexp/glob, then feed the filename result into the replace module.

---

<div class="post-metadata">

**Author:** ![Andrei\_Baban](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andrei\_Baban](https://forum.ansible.com/u/Andrei_Baban)\
**Post date:** [November 15, 2017, 3:08pm UTC](https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020/3 "2017-11-15T15:08:39Z")

</div>

thanks Kai for your answer!

the issue with find module would be that it would work fine if there would be only one type of file with one replace in it. But I store in the variables file multiple type of files that each have different patterns to search for and replace. I would need in pseudocode a for each syntax that would run over both find and replace, something like

for each type of file {  
find all matching files  
replace all resective patterns relative only to this file  
}

hope this makes sense 🙂

---

<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:** [November 15, 2017, 3:32pm UTC](https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020/4 "2017-11-15T15:32:56Z")

</div>

You have this functionality in Ansible by using include/include\_task, with\_item/with\_dict and loop\_control.

---

<div class="post-metadata">

**Author:** ![Andrei\_Baban](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andrei\_Baban](https://forum.ansible.com/u/Andrei_Baban)\
**Post date:** [November 17, 2017, 8:03am UTC](https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020/5 "2017-11-17T08:03:55Z")

</div>

thanks!

I’ll research this a bit more, as I still don’t see a clear way yet on how to condition the second loop to run only on a subset of properties read from the variable file; but the loop\_control is a good start.

---

<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:** [November 18, 2017, 3:17pm UTC](https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020/6 "2017-11-18T15:17:30Z")

</div>

This is not 100% correct code, just an idea on how it could be solved with the information you provided in the fist mail.

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;patterns:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- file: file\_\*.xml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: 17701  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: 17706  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- file: other\_file.xml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: b

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- include: change.yml  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: '{{ patterns }}'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_control:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loop\_var: outer\_item

change.yml

---

<div class="post-metadata">

**Author:** ![Andrei\_Baban](https://avatars.discourse-cdn.com/v4/letter/a/6de8d8/32.png) [@Andrei\_Baban](https://forum.ansible.com/u/Andrei_Baban)\
**Post date:** [November 18, 2017, 3:56pm UTC](https://forum.ansible.com/t/replace-module-how-to-replace-all-matching-files/26020/7 "2017-11-18T15:56:28Z")

</div>

I think this is exactly what I’ve needed! Thanks a lot for your time and patience explaining this. I’ll test it on Monday and reverse to proper module usage instead of shell + sed.

thanks!
