0

how can one possibly match value of a variable through regex parameter in lineinfile module?

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

I tried above with both single/double quotes around, it doesn't seem to work

1
  • 1
    "It doesn't work" is rather unclear; what happens? Do you get an error message? Is it a task, a play etc.? Does the task finish with an "ok" (i.e. successful execution)? You should include more info. Also, what type of text is {{ search_text }}?
    – telometto
    Commented Oct 9, 2022 at 17:17

1 Answer 1

1

For a file test.txt

hello
something
test
hello
something

a run of a test playbook like

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

  vars:

    BEFORE: "test"

  tasks:

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

results into an output of

hello
something

test
hello
something

the expected one.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .