Dictionary 复杂字典上的可分解迭代
我有以下XML文件,并试图根据创建的字典替换每个属性的值,但在迭代复杂的dict时遇到困难 config.xml文件Dictionary 复杂字典上的可分解迭代,dictionary,ansible,json-query,Dictionary,Ansible,Json Query,我有以下XML文件,并试图根据创建的字典替换每个属性的值,但在迭代复杂的dict时遇到困难 config.xml文件 <?xml version='1.0' encoding='UTF-8'?> <SystemConfiguration> <Component FQDD="iDRAC.Embedded.1"> <Attribute Name="IPv4Static.1#DNS1">1.1.1.1</Attribute> <At
<?xml version='1.0' encoding='UTF-8'?>
<SystemConfiguration>
<Component FQDD="iDRAC.Embedded.1">
<Attribute Name="IPv4Static.1#DNS1">1.1.1.1</Attribute>
<Attribute Name="IPv4Static.1#DNS2">2.2.2.2</Attribute>
</Component>
<Component FQDD="System.Embedded.1">
<Attribute Name="LCD.1#Configuration">null</Attribute>
</Component>
</SystemConfiguration>
我无法从字典中的列表中获取值,也不知道如何处理此问题。下面的任务完成了此任务
- xml:
path: "{{ xml_file }}"
xpath: '/SystemConfiguration/Component[@FQDD="{{ item.0.key }}"]/Attribute[@Name="{{ item.1.name }}"]'
value: '{{ item.1.val }}'
with_subelements:
- "{{ comp|dict2items }}"
- value
>diff config.xml config.xml.orig
4,5c4,5
< 10.10.10.10
< 20.20.20.20
---
> 1.1.1.1
> 2.2.2.2
8c8
空的
这种迭代就像一种魅力。我只需要稍微调整xpath行的语法-我遇到了错误-我觉得xpath处理字符串的方式存在错误。
- xml:
path: "{{ xml_file }}"
xpath: '/SystemConfiguration/Component[@FQDD="{{ item.0.key }}"]/Attribute[@Name="{{ item.1.name }}"]'
value: '{{ item.1.val }}'
with_subelements:
- "{{ comp|dict2items }}"
- value
> diff config.xml config.xml.orig
4,5c4,5
< <Attribute Name="IPv4Static.1#DNS1">10.10.10.10</Attribute>
< <Attribute Name="IPv4Static.1#DNS2">20.20.20.20</Attribute>
---
> <Attribute Name="IPv4Static.1#DNS1">1.1.1.1</Attribute>
> <Attribute Name="IPv4Static.1#DNS2">2.2.2.2</Attribute>
8c8
< <Attribute Name="LCD.1#Configuration">OS System Name</Attribute>
---
> <Attribute Name="LCD.1#Configuration">null</Attribute>