# Ansible Replace a value in a File

**URL:** <https://forum.ansible.com/t/ansible-replace-a-value-in-a-file/32109>\
**Category:** Ansible Project\
**Created:** [January 31, 2020, 1:31pm UTC](https://forum.ansible.com/t/ansible-replace-a-value-in-a-file/32109 "2020-01-31T13:31:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sai\_Prasha](https://avatars.discourse-cdn.com/v4/letter/s/5fc32e/32.png) [@Sai\_Prasha](https://forum.ansible.com/u/Sai_Prasha)\
**Post date:** [January 31, 2020, 1:31pm UTC](https://forum.ansible.com/t/ansible-replace-a-value-in-a-file/32109/1 "2020-01-31T13:31:56Z")

</div>

I am trying to Replace a String in an xml file . but using win\_lineinfile module only the last occurence of it is replaced  
how can i replace a String in all occurences .  
One Solution i found is tro run from powershell  
**(Get-Content C:\Program Files\dd\config.xml).replace(‘ToReplace’,‘replaced’) | Set-Content C:\Program Files\dd\config.xml**

But when i try to use the same powershell from playbook  
**win\_shell: (Get-Content C:\Program Files\dd\config.xml).replace(‘ToReplace’,‘sss’) | Set-Content C:\Program Files\dd\config.xml**

Its executing but chnages are not happening ., Any solutions please

---

<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:** [January 31, 2020, 2:07pm UTC](https://forum.ansible.com/t/ansible-replace-a-value-in-a-file/32109/2 "2020-01-31T14:07:14Z")

</div>

Hi

Search and replacing of strings in XML tends to be very fragile, as  
you experience.  
Adding win\_shell powershell and pipes will only make that worse.

If your desired changes allow, you should use the dedicated XML module:

[https://docs.ansible.com/ansible/latest/modules/xml\_module.html](https://docs.ansible.com/ansible/latest/modules/xml_module.html)
