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’,则为属性名称。 如果  | |
| 确定是否应创建备份。 当设置为  选项 
 | |
| 当设置为  选项 
 | |
| xpath 处期望的 XML 片段的字符串表示形式。自 Ansible 2.9 起,当 state=absent 或当 count=yes 时,不需要此项。 | |
| 要操作的文件的路径。 | |
| 设置或删除与 xpath 匹配的节点(或属性)。 选项 
 | |
| 您正在使用的 XML 节点的类型。 选项 
 | |
| 用于选择要操作的节点或节点的 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=yes 示例:  | |
| xpath 匹配的节点数。 返回: 如果 count=yes 示例:  | |
| XML 比较异常。 返回: 始终,对于类型元素和 -vvv 或更高版本 示例:  | |
| 完成了什么。 返回: 始终 示例:  | 
