community.windows.win_xml 模块 – 管理 Windows 主机上的 XML 文件内容

注意

此模块是 community.windows 集合(版本 2.3.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install community.windows

要在 Playbook 中使用它,请指定: community.windows.win_xml

概要

  • 使用 xpath 选择需要管理的 XML 节点,从而管理 XML 节点、属性和文本。

  • 格式化为字符串的 XML 片段用于指定远程 Windows 服务器上 XML 文件的一部分或多部分的所需状态。

  • 对于非 Windows 目标,请改用 community.general.xml 模块。

参数

参数

注释

attribute

字符串

如果类型为 ‘attribute’,则为属性名称。

如果 type=attribute,则为必填项。

backup

布尔值

确定是否应创建备份。

当设置为 yes 时,创建一个包含时间戳信息的备份文件,以便您可以在错误地损坏文件后恢复原始文件。

选项

  • false ← (默认)

  • true

count

布尔值

当设置为 yes 时,返回与 xpath 匹配的节点数。

选项

  • false ← (默认)

  • true

fragment

别名:xmlstring

字符串

xpath 处期望的 XML 片段的字符串表示形式。自 Ansible 2.9 起,当 state=absent 或当 count=yes 时,不需要此项。

path

别名:dest, file

路径 / 必填

要操作的文件的路径。

state

字符串

设置或删除与 xpath 匹配的节点(或属性)。

选项

  • "present" ← (默认)

  • "absent"

type

字符串

您正在使用的 XML 节点的类型。

选项

  • "attribute"

  • "element" ← (默认)

  • "text"

xpath

字符串 / 必填

用于选择要操作的节点或节点的 Xpath。

注释

注意

  • 仅支持对 XML 元素、属性和文本进行操作。

  • 命名空间、处理指令、命令和文档节点类型不能使用此模块进行修改。

另请参阅

另请参阅

community.general.xml

用于 Posix 主机的 XML 操作。

w3shools XPath 教程

关于 XPath 的有用教程

示例

- name: Apply our filter to Tomcat web.xml
  community.windows.win_xml:
    path: C:\apache-tomcat\webapps\myapp\WEB-INF\web.xml
    fragment: '<filter><filter-name>MyFilter</filter-name><filter-class>com.example.MyFilter</filter-class></filter>'
    xpath: '/*'

- name: Apply sslEnabledProtocols to Tomcat's server.xml
  community.windows.win_xml:
    path: C:\Tomcat\conf\server.xml
    xpath: '//Server/Service[@name="Catalina"]/Connector[@port="9443"]'
    attribute: 'sslEnabledProtocols'
    fragment: 'TLSv1,TLSv1.1,TLSv1.2'
    type: attribute

- name: remove debug configuration nodes from nlog.conf
  community.windows.win_xml:
    path: C:\IISApplication\nlog.conf
    xpath: /nlog/rules/logger[@name="debug"]/descendant::*
    state: absent

- name: count configured connectors in Tomcat's server.xml
  community.windows.win_xml:
    path: C:\Tomcat\conf\server.xml
    xpath: //Server/Service/Connector
    count: true
  register: connector_count

- name: show connector count
  ansible.builtin.debug:
    msg: Connector count is {{ connector_count.count }}

- name: ensure all lang=en attributes to lang=nl
  community.windows.win_xml:
    path: C:\Data\Books.xml
    xpath: //@[lang="en"]
    attribute: lang
    fragment: nl
    type: attribute

返回值

常见的返回值记录在此处,以下是此模块特有的字段

描述

backup_file

字符串

创建的备份文件的名称。

返回: 如果 backup=yes

示例: "C:\\Path\\To\\File.txt.11540.20150212-220915.bak"

count

integer

xpath 匹配的节点数。

返回: 如果 count=yes

示例: 33

err

列表 / elements=string

XML 比较异常。

返回: 始终,对于类型元素和 -vvv 或更高版本

示例: ["attribute mismatch for actual=string"]

msg

字符串

完成了什么。

返回: 始终

示例: "xml added"

作者

  • Richard Levenberg (@richardcs)

  • Jon Hawkesworth (@jhawkesworth)