如何解决如何在Ansible中用“复杂”节点代替XML节点?
我如何使用幂等的XML模块(两次运行都没有“改变”)达到示例所需的状态?
示例:
初始状态,bar
节点仅包含文本:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo>foo</foo>
<bar>bar</bar>
<baz>baz</baz>
</root>
所需状态,bar
节点包含:
- 各种节点
- 此节点同时包含标签和文本
<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo>foo</foo>
<bar name="bar">
<content name="1" type="text">lorem</content>
<content name="2" type="yml">lorem: ipsum</content>
</verbose>
</bar>
<baz>baz</baz>
</root>
我的尝试:
---
- hosts: localhost
vars:
xml_text: >-
<?xml version='1.0' encoding='UTF-8'?>
<root>
<foo>foo</foo>
<bar><barbar/></bar>
<baz>baz</baz>
</root>
tasks:
- name: "Substitute bar. Bad: wipes the other nodes"
xml:
xmlstring: "{{ xml_text }}"
xpath: /root
input_type: xml
set_children: |-
<bar>
<verbose />
<content name="1" type="text">lorem</content>
<content name="2" type="yml">lorem: ipsum</content>
</bar>
register: result
- debug:
var: result
# Imitating https://github.com/ansible-collections/community.general/blob/main/tests/integration/targets/xml/tasks/test-set-children-elements-level.yml
- name: "Substitute bar. Bad: Can't do both tags ans values"
xml:
xmlstring: "{{ xml_text }}"
xpath: /root/bar
input_type: yaml
set_children:
- verbose: "1"
- content:
name: "1"
type: text
# _ : lorem
# "": lorem
# ~ : lorem
- content: "lorem: ipsum"
# name: 2
# type: yml
register: result
- debug:
var: result
- name: "Substitute bar. Bad: Fails"
xml:
xmlstring: "{{ xml_text }}"
xpath: /root/bar
input_type: xml
set_children: |-
<verbose />
<content name="1" type="text">lorem</content>
<content name="2" type="yml">lorem: ipsum</content>
register: result
- debug:
var: result
解决方法
我认为您需要为 XML 内容使用空节点(键“_”)。见https://github.com/cmprescott/ansible-xml/issues/101
例如
Vaar-Version
,
在 2021-05-26 是不可能的。
一个错误正在等待贡献者:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。