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



使用Ansible模块community.general.xml,我试图将子节点添加到以下XML数据:(保存到namspace -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>

但我得到以下错误信息,而不是:

解析子元素时出错:include上的命名空间前缀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>

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

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

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

那么,这就是幂等任务:

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


关于您确实需要多个include的事实,那么诀窍是进一步专门化您的xpath表达式,例如:

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

因此,添加三个include节点的任务可能会在循环中结束:

- 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

最新更新