我正在编写一个 ansible 剧本来渲染一个模板。我有两种方法可以实现它,选项 1 有效为什么选项 2 不行?
-- 默认值/main.yml
cluster_name:
lab:
use1: elasticsearch_use1
use1:
lab:
cluster_name: elasticsearch_use1
location: use1
env: lab
cluster_config_file:
-- templet/cluster.yml
cluster.name: {{ location[env][cluster_name] }}
--任务/main.yml
- name: cluster congiguration
template:
src: "{{ cluster_config_file }}"
dest: "/{{ cluster_config_file }}"
force: true
owner: root
group: root
mode: 0774
backup: yes
方案一:工作模式
-- templet/cluster.yml
cluster.name: {{ cluster_name[env][location] }}
选项2:需要帮助模型
-- templet/cluster.yml
cluster.name: {{ location[env][cluster_name] }}
使用此选项,我在运行时遇到此错误
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.errors.AnsibleUndefinedVariable: 'list object' has no attribute 'lab'. 'list object' has no attribute 'lab' fatal: [127.0.0.1]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'list object' has no attribute 'lab'. 'list object' has no attribute 'lab'"}
我不确定我在这里遗漏了什么,它在两次运行中都是相同的列表对象,但选项 1 有效,为什么选项 2 无效?