![](/img/trans.png)
[英]Adding a parameter to a line with Ansible with lineinfile function and regexp
[英]Ansible lineinfile regexp line with spaces
我在配置文件中有一行,我想修改該行。 我使用下面的代碼
- name: insert line
lineinfile:
path: /etc/config
regexp: '^(.*)password root(.*)$'
line: password root {{ hello }}
所以在上述情況下,即使我運行 ansible 任務,它也只是插入一個新行,但從不修改該行。 我如何避免正則表達式中的字符串空間? 我試過 "\s" 和 "/\s" 但無法修復它。
例如,output 文件 /etc/config 如下所示。
password root alpha
下次我運行代碼時它會變成
password root beta
在我的例子中,{{ hello }} 變量不斷變化,所以我想替換而不是添加新行。
在這種情況下確定的問題是,由於 output 行(即 /etc/config 中)以無空格開頭,
regexp: '^password root(.*)$'
正則表達式應該如上,不需要 \s 來克服密碼和 root 之間的空格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.