gpt4 book ai didi

ansible - 用 Ansible 替换文件中的单词

转载 作者:行者123 更新时间:2023-12-04 01:39:15 26 4
gpt4 key购买 nike

我试图替换文件中的一个字符串。例如:

$PASSWORD="oldpassword"

与:

$PASSWORD="newpassword"

这是应该执行此操作的 Ansible 任务:

- name: change password with lineinfile 
lineinfile:
dest: test.txt
regexp: '^$PASSWORD='
line: '^$PASSWORD="newpassword"'
state: present
backrefs: yes

不幸的是,我找不到它不起作用的原因。我无法用新字符串替换它。

我也在尝试不使用 backrefs 并且添加了字符串而不是替换了字符串。

请指教。谢谢。

最佳答案

来自 Regular expression operations :

$: Matches the end of the string or just before the newline at the end of the string

因此,使用反斜杠转义 $

  - lineinfile:
dest: /tmp/test.txt
regexp: '^\$PASSWORD='
line: '$PASSWORD="newpassword"'
state: present

此外,您不需要在示例中使用 backrefs 参数,因为您的正则表达式没有反向引用。

关于ansible - 用 Ansible 替换文件中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41417150/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com