首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ansible XML Array获取数组内的元素

Ansible XML Array获取数组内的元素
EN

Stack Overflow用户
提问于 2020-02-12 05:13:52
回答 1查看 181关注 0票数 1

我有一个从rest API获得的和数组的xmldocument。

代码语言:javascript
复制
<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。

这只是一个输入元素的例子:

代码语言:javascript
复制
  <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>

这是我的策略:

代码语言:javascript
复制
  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和其他选项,但始终获得以下输出。

代码语言:javascript
复制
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在每次调用中只接受一个输入元素。

谢谢。

致以问候。

EN

回答 1

Stack Overflow用户

发布于 2020-02-12 06:02:45

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

在后面的代码块中,您将能够迭代已注册的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60177455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档