简体   繁体   English

Ansible lineinfile 带空格的正则表达式行

[英]Ansible lineinfile regexp line with spaces

I have a line in a config file, to which I would like to modify the line.我在配置文件中有一行,我想修改该行。 I use the below code我使用下面的代码

- name: insert line
  lineinfile:
    path: /etc/config
    regexp: '^(.*)password root(.*)$'
    line: password root {{ hello }}

so in above case, when even i run the ansible task it just inserts a new line, but never modifies the line.所以在上述情况下,即使我运行 ansible 任务,它也只是插入一个新行,但从不修改该行。 How do i avoid the space of the string in the regexp?我如何避免正则表达式中的字符串空间? Ive tried "\s" and "/\s" but couldnt fix it.我试过 "\s" 和 "/\s" 但无法修复它。

the output file /etc/config looks like below for example.例如,output 文件 /etc/config 如下所示。

password root alpha

next time I run the code it becomes下次我运行代码时它会变成

password root beta

here in my case the {{ hello }} variables keeps changing so I want to replace instead of adding new line.在我的例子中,{{ hello }} 变量不断变化,所以我想替换而不是添加新行。

The problem identified in this case is, since the output line (ie in /etc/config) starts with no spaces,在这种情况下确定的问题是,由于 output 行(即 /etc/config 中)以无空格开头,

regexp: '^password root(.*)$'

the regex should be as above and no need of \s to overcome space between password and root.正则表达式应该如上,不需要 \s 来克服密码和 root 之间的空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM