[英]ansible lineinfile module to replace a single line with multiple lines
[英]Ansible lineInFile insertbefore multiple times
我已經嘗試並四處搜索以解決此問題,但沒有任何適合我的問題。
我正在使用 ansible 檢索配置,我想以某種方式操作此配置。 我想做的一件事是在帶有正則表達式匹配的行之前添加一個標簽,但在所有這些行上。
hello
something
something
hello
something
hello
應該:
tag:
hello
something
something
tag:
hello
something
tag:
hello
我試過使用模塊 lineinfile 和 insertbefore 選項,但是這個只在最后一場比賽中添加一次標簽。 我也嘗試了 with_items 循環,但我的選項沒有改變,我只想在相同正則表達式的所有匹配項上完成它,之前添加了相同的字符串。
有什么想法可以實現嗎?
你可以使用替換模塊:
- name: replace txt
hosts: localhost
tasks:
- name: Replace before
ansible.builtin.replace:
path: "./file.txt"
regexp: '^(hello)$'
replace: 'tag:\n\1'
結果:
tag:
hello
something
something
tag:
hello
something
tag:
hello
如果你使用這個正則表達式:
regexp: '^(?:tag:\n)?(hello)$'
replace: 'tag:\n\1'
不添加新tag:
如果您重播任務
如果您對群組有疑問:
regexp: '^hello$'
replace: 'tag:\nhello'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.