嵌套的 Jinja2 字典有价值的东西正在消失
Nested Jinja2 dictionary valuable is disappearing
我正在使用 ansible 和 jinja2,需要访问一个变量。
我有一个数据结构 structure
,并在剧本中执行:
- name: debug_1
debug:
msg: "{{ vars['structure'] }}"
产生这些结果:
ok: [debug-test] => {
"msg": {
"Covo": [
{
"enabled": true
}
]
}
}
此外,运行
- name: debug_1
debug:
msg: "{{ vars['structure']['covo'] }}"
给出了预期的结果:
ok: [debug-test] => {
"msg": [
{
"enabled": true
}
]
}
但是,尝试访问 enabled
变量:
- name: debug_1
debug:
msg: "{{ vars['structure']['covo']['enabled'] }}"
会抛出错误:
fatal: [debug-test]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'enabled'\n\nThe error appears to be in '/task/main.yml': line 16, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n when: vars['structure']['covo']['enabled']|default(False) == True\n - name: debug_1\n ^ here\n"}
如何访问这个变量?
covo是数组,所以要声明索引:
- name: debug_1
debug:
msg: "{{ vars['structure']['covo'][0]['enabled'] }}"
我正在使用 ansible 和 jinja2,需要访问一个变量。
我有一个数据结构 structure
,并在剧本中执行:
- name: debug_1
debug:
msg: "{{ vars['structure'] }}"
产生这些结果:
ok: [debug-test] => {
"msg": {
"Covo": [
{
"enabled": true
}
]
}
}
此外,运行
- name: debug_1
debug:
msg: "{{ vars['structure']['covo'] }}"
给出了预期的结果:
ok: [debug-test] => {
"msg": [
{
"enabled": true
}
]
}
但是,尝试访问 enabled
变量:
- name: debug_1
debug:
msg: "{{ vars['structure']['covo']['enabled'] }}"
会抛出错误:
fatal: [debug-test]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'enabled'\n\nThe error appears to be in '/task/main.yml': line 16, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n when: vars['structure']['covo']['enabled']|default(False) == True\n - name: debug_1\n ^ here\n"}
如何访问这个变量?
covo是数组,所以要声明索引:
- name: debug_1
debug:
msg: "{{ vars['structure']['covo'][0]['enabled'] }}"