你如何在 Ansible 中解决这个问题?
我有一个文件/etc/foo/foo.config
。我想将此文件中的字符串“DisallowBar”替换为“AlllowBarUntilMidnight”。
Ansible 应该在这些情况下采取如下行动:
Case3 对我很重要,因为这种状态不应该存在。这是一个错误,不应无声无息地通过。
您可以使用replacevalidate
参数来确保将要写入的文件包含和不再包含.AllowBarUntilMidnight
DisallowBar
tasks:
- name: replace DisallowBar
replace:
path: /etc/foo/foo.config
regexp: 'DisallowBar'
replace: "AllowBarUntilMidnight"
validate: 'grep "AllowBarUntilMidnight" %s'
Run Code Online (Sandbox Code Playgroud)
该validate
命令在生成的临时文件上运行,然后在replace
运行后将其复制到位。在这种情况下,如果grep
失败,则意味着没有进行替换,并且您的原始文件从未包含DisallowBar
在内。然后播放失败,文件不变。