Ansible Replace a value in a File

259 views
Skip to first unread message

Sai Prasha

unread,
Jan 31, 2020, 8:31:56 AM1/31/20
to Ansible Project
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

Dick Visser

unread,
Jan 31, 2020, 9:07:49 AM1/31/20
to ansible...@googlegroups.com
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
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/97458bf6-691e-4407-ad0b-c7b962e79230%40googlegroups.com.



--
Dick Visser
Trust & Identity Service Operations Manager
GÉANT
Reply all
Reply to author
Forward
0 new messages