![](/img/trans.png)
[英]Ansible "lineinfile": add new line (with PATH=) or append to existing line (with PATH=)
[英]Ansible: lineinfile keeps squashing my existing line instead of appending text to it
我看過一些有類似問題的帖子,但似乎沒有什么對我有用。 有些人提到需要在我的正則表達式中使用組,所以我添加了它們,但仍然有問題。 其他人說只需在我的 lineinfile 中添加 'backrefs: yes' 就可以了,到目前為止還沒有運氣。
現有線路:
PATH=$PATH:$HOME/.local/bin:$HOME/bin
執行后的預期行:
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/opt/somedir
執行后的實際行:
:/opt/somedir
我的 Ansible:
- name: update ~/.bash_profile
lineinfile:
backup: yes
backrefs: yes
path: ~/.bash_profile
regexp: '^(PATH=.*)$'
line: ':/opt/somedir'
我錯過了什么?
您需要將反向引用添加到將要插入的行。
例如:
- name: update ~/.bash_profile
lineinfile:
backup: yes
backrefs: yes
path: ~/.bash_profile
regexp: '^(PATH=.*)$' # PATH=.* is inside group 1 because of the parentheses
line: '\1:/opt/somedir' # group 1 can be referenced with \1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.