ansible lineinfile モジュールが Variable_value と一致します。

ansible lineinfile モジュールが Variable_value と一致します。

lineinfileモジュールの正規表現パラメータを介して変数の値を一致させる方法は?

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

上記で一重引用符/二重引用符を試しましたが、うまくいかないようです。

ベストアンサー1

ファイルの場合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

予想されるもの。

おすすめ記事