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

预期的一个。

相关内容