Ansible XML Array 获取数组中的元素



I Have and Array of xmldocument from a rest API 获取。

<ArrayOfXmlDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<XmlDocument>
<input xmlns="urn:opendaylight:params:xml:ns:yang:topology:pcep">
.....
</input>
</XmlDocument>
<XmlDocument>
<input xmlns="urn:opendaylight:params:xml:ns:yang:topology:pcep">
.....
</input>
</XmlDocument>
<XmlDocument>
<input xmlns="urn:opendaylight:params:xml:ns:yang:topology:pcep">
.....
</input>
</XmlDocument>
</ArrayOfXmlDocument>

使用 Ansible,我需要获取元素和输入元素中的所有项目,并将其发布到 API。

只是输入元素的示例:

<input xmlns="urn:opendaylight:params:xml:ns:yang:topology:pcep">
<node>pcc://7.188.102.54</node>
<name>LSP--LAB--LAB/LSP--LAB--LAB-PRI</name>
<arguments>
<lsp xmlns="urn:opendaylight:params:xml:ns:yang:pcep:ietf:stateful">
<delegate>true</delegate>
<administrative>true</administrative>
</lsp>
<ero>
<subobject>
<loose>false</loose>
<ip-prefix>
<ip-prefix>10.10.12.2/32</ip-prefix>
</ip-prefix>
</subobject>
<subobject>
<loose>false</loose>
<ip-prefix>
<ip-prefix>10.10.26.2/32</ip-prefix>
</ip-prefix>
</subobject>
</ero>
</arguments>
<network-topology-ref xmlns:topo="urn:TBD:params:xml:ns:yang:network-topology">/topo:network-topology/topo:topology[topo:topology-id="pcep-topology"]</network-topology-ref>
</input>
</XmlDocument>

这是我的剧本:

tasks:
- name: Check if service interface exists, fail if true and changes are not allowed
xml:
path: pcepxml.xml
xpath: /ArrayOfXmlDocument/XmlDocument/*
content: text
#count: yes
register: xmlresp
#failed_when: doccount.count == 0
- name: Print
debug:
var: xmlresp

尝试xmlresp.matches[0]xmlresp.matches.XmlDocument和其他选项,但始终获得以下输出。

TASK [Print] ******************************************************************************************************************************************************************************************
ok: [localhost] => {
"xmlresp": {
"actions": {
"namespaces": {},
"state": "present",
"xpath": "/ArrayOfXmlDocument/XmlDocument/*"
},
"changed": false,
"count": 24,
"failed": false,
"matches": [
{
"{urn:opendaylight:params:xml:ns:yang:topology:pcep}input": "n            "
},
{
"{urn:opendaylight:params:xml:ns:yang:topology:pcep}input": "n            "
},
...
],
"msg": 24
}
}

获取每个输入并将其发布到 API 的最佳选择是什么? API 每次调用仅接受一个输入选项。

谢谢。

问候。

仔细阅读xml模块文档。具体来说,我认为您会对 return-xmlstring 属性感兴趣。

您将能够在以后的块中迭代注册的结果。

最新更新