【问题标题】:Ansible XML Array get elements inside an arrayAnsible XML Array 获取数组中的元素
【发布时间】:2020-02-11 21:13:51
【问题描述】:

我有一个从其他 API 获得的 xmldocument 数组。

<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 每次调用只接受一个输入元素。

谢谢。

问候。

【问题讨论】:

  • Jorge,发帖的一些技巧... 1. 尽量保持输出简短,当输出很多时阅读会很累。我已经编辑以帮助解决这个问题。 2. 更清楚你的问题。我不清楚您具体要调用什么或这里出了什么问题。
  • 嗨,谢谢 PAL,我需要使用大量 迭代 XML 文档每个输入都是对另一个 API 的调用,我使用 Ansible 使用 API 迭代 XML,并且对于每个 我需要发布到另一个 API 来处理。 ODL (docs.opendaylight.org/en/stable-nitrogen/user-guide/…) ---- link 这个部分具体

标签: arrays xml get ansible element


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2021-12-15
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多