首页 > 解决方案 > Ansible 中嵌套变量的语法错误

问题描述

我在我的剧本中包含了以下变量文件:

more vars/was_vars.yaml
::::::::::::::
10.9.12.112: "/was/IBM/WebSphere"
10.8.10.28: "/was/IBM/profiles"
10.7.120.129: "/app/tmp"

这是我的剧本:

- name: Configure nodes
  hosts: dest_nodes
  user: "{{ USER }}"
  tasks:
   - name: Construct File Path on "{{ inventory_hostname }}".
     command: "touch {{ BASEPATH }}/{{ ( item | splitext)[1] }}/del.tmp"
     when: "{{ Layer == 'APP' }}"
     file: path="{{ "{{ inventory_hostname }}" }}/{{ App_List }}/{{ Rel_Path }}/del.tmp state=directory mode=u=rwx,g=rw,o=r"
     when: "{{ Layer == 'WAS' }}"

"{{inventory_hostname }}" 被替换为 "10.9.12.112",然后应进一步替换为 "/was/IBM/WebSphere",如包含的 vars("was_vars.yaml") 文件中所述。

我的当前代码出现以下语法错误:

ERROR! conflicting action statements: command, file

The error appears to be in '/app/Ansible/deploy.yml': line 133, column 4, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
   - name: Construct File Path on "{{ inventory_hostname }}".
     ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"

我在ansible上的最新版本。

你能建议吗?

标签: variablesansible

解决方案


你可以试试:

"{{vars[inventory_hostname]}}"

或者,如果您想要更多级别,请使用:

"{{vars[vars[inventory_hostname]]}}"

推荐阅读