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

问题描述 投票:0回答:1

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

我要在

core
节点下添加的数据是:

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

我正在运行的 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

我期待得到这个结果:

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

但我收到以下错误消息:

解析子元素时出错:包含的命名空间前缀 xi 未定义


β.εηοιτ.βε 建议的非常有用的选项后的更多信息。

幂等选项不满足我需要在 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 ">
    <xi:include href="/conf/addresses.xml"/>
    <xi:include href="/conf/address-settings.xml"/>
    <xi:include href="/conf/security-settings.xml"/>
  </core>
</configuration>
xml ansible namespaces
1个回答
2
投票

您手头的问题实际上有两种方法:

  1. 强烈推荐的:使用您期望的节点构造
    xpath
    /conf:configuration/core:core/xi:include
    — 然后添加所需的属性。
  2. 与您的实际试验一致,使用
    add_children
    ,但采用 YAML 形式,因为 XML 形式似乎不能很好地处理命名空间 reference

为什么强烈推荐第一个?因为它使你的任务幂等,所以,重新运行你的剧本不会不断地一遍又一遍地添加相同的节点,如果它已经存在并具有正确的属性。

所以,这是幂等任务:

- 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

这将给出您期望的 XML。


考虑到您确实需要多个包含,技巧就是进一步专门化您的 xpath 表达式,例如:

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

因此,添加三个包含节点的任务可能会陷入循环:

- 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

如果你真的想要非幂等选项,这里是:

- 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
© www.soinside.com 2019 - 2024. All rights reserved.