我有一个字典定义的类型如下:
variables.yml
---
fileName1:
dict1:
prop1:
key: "key"
value: "value"
prop2:
key:"key"
value: "value"
prop3:
..and so on
dict2:
prop1:
key:"key"
value: "value"
prop2:
key: "key"
value: "value"
dict3:
fileName2:
...and similar structure follows for fileName 2,3, 4.
在我的ansible任务中,我想要迭代我正在做的事情:
- name: invlude variable file
include_vars: variables.yml
- name: iterate over the dict and sub dict
debug: msg="{{ item.value }}"
with_dict: "{{ var[filename] }}"
when: item.key == "dict1"
这里的输出给我的值为prop1:... prop2:... prop3:..依此类推。 我需要进一步迭代像我尝试使用
的prop *- name: iterate over the dict and sub dict
debug: msg="{{ item.value.item }}"
with_dict: "{{ var[filename] }}"
when: item.key == "dict1"
但是因为dict没有任何元素作为项目返回错误。 我需要对此进行泛型迭代。任何帮助将不胜感激。
答案 0 :(得分:0)
必须有一种更简单的方法来做到这一点,但我画了一个空白,这种方式可以迭代道具:
testvars.yml:
---
fileName1:
dict1:
prop1:
key: "key111"
value: "value111"
prop2:
key: "key112"
value: "value112"
prop3:
key: "key113"
value: "value113"
dict2:
prop1:
key: "key121"
value: "value121"
prop2:
key: "key123"
value: "value122"
fileName2:
dict1:
prop1:
key: "key211"
value: "value211"
prop2:
key: "key212"
value: "value212"
prop3:
key: "key213"
value: "value213"
dict2:
prop1:
key: "key221"
value: "value221"
prop2:
key: "key222"
value: "value222"
test.yml:
---
- hosts: 127.0.0.1
gather_facts: no
tasks:
- include_vars: testvars.yml
- name: Iterate
include_tasks: test2.yml
vars:
loopitem: "{{ item.value }}"
with_dict: "{{ fileName1 }}"
when: item.key == 'dict1'
test2.yml
---
- debug:
msg: "Property {{prop.key}} has the keypair {{prop.value.key}} : {{prop.value.value}}"
with_dict: "{{ loopitem }}"
loop_control:
loop_var: prop