# Replacing variable in multiple file

**URL:** <https://forum.ansible.com/t/replacing-variable-in-multiple-file/33752>\
**Category:** Ansible Project\
**Created:** [September 7, 2020, 4:03am UTC](https://forum.ansible.com/t/replacing-variable-in-multiple-file/33752 "2020-09-07T04:03:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abdulrazzaq\_shaik](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@Abdulrazzaq\_shaik](https://forum.ansible.com/u/Abdulrazzaq_shaik)\
**Post date:** [September 7, 2020, 4:03am UTC](https://forum.ansible.com/t/replacing-variable-in-multiple-file/33752/1 "2020-09-07T04:03:24Z")

</div>

vars:

- MACHINE: [gpack.tmt.com](http://gpack.tmt.com)
- AMSPORT: 9933
- ENVIRONMENT: ‘PRD’
- PROJECT: ‘GTA’
- AMSPORT: ‘333’
- MACHINE\_LOG: ’ gpack ’
- STOREFILE\_PATH: ‘/home/linux/store’
- AMS\_VERSION: ‘1.5’

I have few file in the node need to change all above variable in the different place in the file.

Kindly share how i can do it in one shot

- name: Replace string in files  
replace:  
dest: “{{ item.path }}”  
regexp: ‘AMSPORT’  
replace: ‘{{AMSPORT}}’  
loop\_control:  
label: “{{ item.path }}”  
with\_items: “{{ files\_to\_change.files }}”

by above i can able to change only one variable at a time,

Please help in sharing how this can be done in all files replacing all variables.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [September 7, 2020, 5:50am UTC](https://forum.ansible.com/t/replacing-variable-in-multiple-file/33752/2 "2020-09-07T05:50:57Z")

</div>

In this case, the best option is the "nested" loop  
[https://docs.ansible.com/ansible/latest/plugins/lookup/nested.html#nested-composes-a-list-with-nested-elements-of-other-lists](https://docs.ansible.com/ansible/latest/plugins/lookup/nested.html#nested-composes-a-list-with-nested-elements-of-other-lists)

Two lists are needed. A list of the files and a list of the  
variables. Instead of the list of hashes it would be easier to use a  
dictionary

&nbsp;&nbsp;my\_vars:  
&nbsp;&nbsp;&nbsp;&nbsp;MACHINE: gpack.tmt.com  
&nbsp;&nbsp;&nbsp;&nbsp;AMSPORT: 9933

The output of the task below shows how to create the loop  
and the parameters

&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "dest: {{ item.0.path }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "regexp: {{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "replace: {{ my\_vars[item.1] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ files\_to\_change.files }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ my\_vars.keys()|list }}"

If this is what you want replace the variables in the files

&nbsp;&nbsp;&nbsp;&nbsp;- name: Replace string in files  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ item.0.path }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp: "{{ item.1 }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;replace: "{{ my\_vars[item.1] }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ files\_to\_change.files }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ my\_vars.keys()|list }}"
