Ansible 遍历复杂字典

Ansible iterate over complex dictionary

我有以下 XML 文件,并尝试根据创建的字典替换每个属性的值,但在遍历复杂字典时遇到问题。

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>
 <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>

A​​nsible 剧本如下所示:

---
- name: modify XML
  hosts: localhost
  gather_facts: no

  vars:
    xml_file: config.xml
    comp:
      iDRAC.Embedded.1: [
        {name: "IPv4Static.1#DNS1", val: 10.10.10.10},
        {name: "IPv4Static.1#DNS2", val: 20.20.20.20}]
      System.Embedded.1: [
        {name: "LCD.1#Configuration", val: "OS System Name"}]

  tasks:
    - name: replace value in XML
      xml:
        path: "{{ xml_file }}"
        xpath: /SystemConfiguration/Component[@FQDD={{ item.key }}]/Attribute[@Name="{{ item.value.name }}"]
        value: item.value.val
      loop: "{{ lookup('dict', comp,wantlist=true) }}"

我无法从字典中的列表中获取值,也不知道如何处理。

下面的任务完成工作

    - 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>