是否可以在带有 lineinfile ansible 模块的路径中使用变量?

Kon*_*kov 2 ansible

我有一本剧本

---
- 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",'
Run Code Online (Sandbox Code Playgroud)

工作正常,我有一个调试

ok: [127.0.0.1] => {
    "msg": "/somefolder"
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在路径中使用变量时


- 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",'
Run Code Online (Sandbox Code Playgroud)

有错误

- lineinfile:
    path: "{{ WORK_DIR }}"/some.file
                          ^ here
Run Code Online (Sandbox Code Playgroud)

问题是为什么?这是错误或功能还是其他什么?

Kon*_*rov 7

引用误用。

你应该使用path: "{{ WORK_DIR }}/some.file".