community.windows.win_xml (2.2.0) — module

Manages XML file content on Windows hosts

Authors: Richard Levenberg (@richardcs), Jon Hawkesworth (@jhawkesworth)

Install collection

Install with ansible-galaxy collection install community.windows:==2.2.0


Add to requirements.yml

  collections:
    - name: community.windows
      version: 2.2.0

Description

Manages XML nodes, attributes and text, using xpath to select which xml nodes need to be managed.

XML fragments, formatted as strings, are used to specify the desired state of a part or parts of XML files on remote Windows servers.

For non-Windows targets, use the M(community.general.xml) module instead.

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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: '/*'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: count configured connectors in Tomcat's server.xml
  community.windows.win_xml:
   path: C:\Tomcat\conf\server.xml
   xpath: //Server/Service/Connector
   count: yes
  register: connector_count
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: show connector count
  debug:
    msg="Connector count is {{connector_count.count}}"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- 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

Inputs

    
path:
    aliases:
    - dest
    - file
    description:
    - Path to the file to operate on.
    required: true
    type: path

type:
    choices:
    - attribute
    - element
    - text
    default: element
    description:
    - The type of XML node you are working with.
    type: str

count:
    default: false
    description:
    - When set to C(yes), return the number of nodes matched by I(xpath).
    type: bool

state:
    choices:
    - present
    - absent
    default: present
    description:
    - Set or remove the nodes (or attributes) matched by I(xpath).
    type: str

xpath:
    description:
    - Xpath to select the node or nodes to operate on.
    required: true
    type: str

backup:
    default: false
    description:
    - Determine whether a backup should be created.
    - When set to C(yes), create a backup file including the timestamp information so
      you can get the original file back if you somehow clobbered it incorrectly.
    type: bool

fragment:
    aliases:
    - xmlstring
    description:
    - The string representation of the XML fragment expected at xpath.  Since ansible
      2.9 not required when I(state=absent), or when I(count=yes).
    required: false
    type: str

attribute:
    description:
    - The attribute name if the type is 'attribute'.
    - Required if C(type=attribute).
    type: str

Outputs

backup_file:
  description: Name of the backup file that was created.
  returned: if backup=yes
  sample: C:\Path\To\File.txt.11540.20150212-220915.bak
  type: str
count:
  description: Number of nodes matched by xpath.
  returned: if count=yes
  sample: 33
  type: int
err:
  description: XML comparison exceptions.
  returned: always, for type element and -vvv or more
  sample: attribute mismatch for actual=string
  type: list
msg:
  description: What was done.
  returned: always
  sample: xml added
  type: str

See also