Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use variable in path with lineinfile ansible module?

Tags:

ansible

I have a playbook

---
- hosts: 127.0.0.1
  connection: local

  vars:
    WORK_DIR: /somefolder

  tasks:

  - debug:
      msg: "{{ WORK_DIR }}"

  - lineinfile:
      path: /somefolder/some.file
      regexp: '"display_name":'
      line: '  "display_name": "another_name",'

works fine and I have a debug

ok: [127.0.0.1] => {
    "msg": "/somefolder"
}

but when I try to use variable in path like


- hosts: 127.0.0.1
  connection: local

  vars:
    WORK_DIR: /somefolder

  tasks:

  - debug:
      msg: "{{ WORK_DIR }}"

  - lineinfile:
      path: "{{ WORK_DIR }}"/some.file
      regexp: '"display_name":'
      line: '  "display_name": "another_name",'

there is an error

- lineinfile:
    path: "{{ WORK_DIR }}"/some.file
                          ^ here

The question is why ? Is this a bug or feature or something else ?

like image 209
Konstantin Shestakov Avatar asked Dec 13 '25 17:12

Konstantin Shestakov


1 Answers

Quotes misuse.

You should use path: "{{ WORK_DIR }}/some.file".

like image 141
Konstantin Suvorov Avatar answered Dec 16 '25 21:12

Konstantin Suvorov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!