Search

ansible lineinfile 模組以匹配 variable_value

  • October 10, 2022

如何通過 lineinfile 模組中的正則表達式參數匹配變數的值?

- name: emptyline before search
 lineinfile:
   dest: ~/file
   insertbefore: '^{{ search_text }}$'
   line: ''

我在上面嘗試了單引號/雙引號,它似乎不起作用

對於一個文件test.txt

hello
something
test
hello
something

執行一個測試劇本,比如

---
- hosts: localhost
 become: false
 gather_facts: false

 vars:

   BEFORE: "test"

 tasks:

 - name: Add line before
   lineinfile:
     path: test.txt
     insertbefore: '^{{ BEFORE }}$'
     line: ''

結果輸出為

hello
something

test
hello
something

預期的。

引用自:https://unix.stackexchange.com/questions/720362