win_lineinfile,根据 Ansible 文档
... the pattern to replace if found; only the last line found will be replaced
有时您想要做的就是用另一个字符串替换所有行中字符串的所有实例
- name: replace thin with oci in environment.properties
win_shell: |
(Get-Content "{{file_name}}").replace(':thin:',':oci:') | Set-Content -Path "{{file_name}}" -Encoding Ascii
是的,在很多情况下模板是更好的选择
是否有更好的 native Ansible 方法可以在 Windows 中执行此操作?
最佳答案
你为什么不使用:
win_lineinfile:
path: 'your/path/to/file.ext'
# Line to Search/Match against
regexp: '{{item.From}}'
# Line to Replace with
line: '{{item.To}}'
backrefs: yes
backrefs 选项允许您在整个文件中进行搜索。更多信息请点击 https://docs.ansible.com/ansible/2.3/win_lineinfile_module.html
关于Ansible 替换 Windows 文本文件中的所有匹配项 - win_lineinfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58457590/