简体   繁体   English

如何使用 Ansible 模块 community.general.xml 编辑 XML 文件

[英]How do you edit XML file with Ansible module community.general.xml

Using the Ansible module community.general.xml , I am attempting to add a child node to the following XML data: (saved into namespace-test.xml )使用 Ansible 模块community.general.xml ,我正在尝试将子节点添加到以下 XML 数据:(保存到namespace-test.xml

<configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
  <core xmlns="urn:activemq:core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq:core ">
  </core>
</configuration>

The data I would like to add, under the core node, is:我想在core节点下添加的数据是:

<xi:include href="/conf/xmlconfig-to-include.xml"/>

The Ansible task I am running is:我正在运行的 Ansible 任务是:

- name: include modular XML configuration file
  community.general.xml:
    path: namespace-test.xml
    xpath: /conf:configuration/core:core
    input_type: xml
    add_children:
      - '<xi:include href="/conf/xmlconfig-to-include.xml"/>'
    namespaces:
      conf: urn:activemq
      core: urn:activemq:core
      xi: "http://www.w3.org/2001/XInclude"
    pretty_print: yes

I am expecting to get this result:我期待得到这个结果:

<configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
  <core xmlns="urn:activemq:core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq:core ">
    <xi:include href="/conf/xmlconfig-to-include.xml"/>
  </core>
</configuration>

But I am getting the following error message, instead:但我收到以下错误消息,而不是:

Error while parsing child element: Namespace prefix xi on include is not defined解析子元素时出错:未定义包含上的命名空间前缀 xi


More information after the very usefull options suggested by β.εηοιτ.βε.在 β.εηοιτ.βε 建议的非常有用的选项之后的更多信息。

The idempotent option did not meet the additionnal requirements (non-specified initially) that I need more than one include in the XML block.幂等选项不满足我需要在 XML 块中包含多个的附加要求(最初未指定)。

This is really what I was aiming for:这确实是我的目标:

<configuration xmlns="urn:activemq" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xi="http://www.w3.org/2001/XInclude" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
  <core xmlns="urn:activemq:core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq:core ">
    <xi:include href="/conf/addresses.xml"/>
    <xi:include href="/conf/address-settings.xml"/>
    <xi:include href="/conf/security-settings.xml"/>
  </core>
</configuration>

There are really two approaches for your issue at hand:您手头的问题实际上有两种方法:

  1. the highly recommended one : constructing the xpath with the node you are expecting — /conf:configuration/core:core/xi:include — and just adding the required attribute.强烈推荐的一个:使用您期望的节点构建xpath - /conf:configuration/core:core/xi:include - 并添加所需的属性。
  2. in the line with your actual trial, using an add_children , but in a yaml form, because the XML form does not seems to cope well with namespace reference .与您的实际试验一致,使用add_children ,但采用 yaml 形式,因为 XML 形式似乎不能很好地处理 namespace reference

Why is the first one highly recommended?为什么强烈推荐第一个? Because it makes your task idempotent , so, re-running your playbook won't keep on adding the same node over and over again, if it exists with the right attribute already.因为它使您的任务具有幂等性,因此,如果它已经存在且具有正确的属性,那么重新运行您的 playbook 将不会一遍又一遍地添加相同的节点。

So, here is the idempotent task:所以,这是幂等任务:

- name: include modular XML configuration file
  community.general.xml:
    path: namespace-test.xml
    xpath: /conf:configuration/core:core/xi:include
    attribute: href
    value: /conf/xmlconfig-to-include.xml
    namespaces:
      conf: urn:activemq
      core: urn:activemq:core
      xi: "http://www.w3.org/2001/XInclude"
    pretty_print: yes

Which would give your expected XML.这将给您预期的 XML。


And regarding the fact that you do need multiple include, the trick, then is to further specialize your xpath expression, for example:关于您确实需要多个包含的事实,诀窍就是进一步专门化您的 xpath 表达式,例如:

/conf:configuration
  /core:core
    /xi:include[@href='/conf/xmlconfig-to-include.xml']

So, your task to add the three include nodes could end up in a loop:因此,您添加三个包含节点的任务可能会陷入循环:

- name: include modular XML configuration file
  community.general.xml:
    path: namespace-test.xml
    xpath: /conf:configuration/core:core/xi:include[@href='{{ item }}']
    namespaces:
      conf: urn:activemq
      core: urn:activemq:core
      xi: "http://www.w3.org/2001/XInclude"
    pretty_print: yes
  loop:
    - /conf/addresses.xml
    - /conf/address-settings.xml
    - /conf/security-settings.xml

And if you really want the non-idempotent option, here it is:如果你真的想要非幂等选项,这里是:

 - name: include modular XML configuration file community.general.xml: path: namespace-test.xml xpath: /conf:configuration/core:core add_children: - "{http://www.w3.org/2001/XInclude}include": href: /conf/xmlconfig-to-include.xml namespaces: conf: urn:activemq core: urn:activemq:core xi: "http://www.w3.org/2001/XInclude" pretty_print: yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM