| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2018, Ansible Project |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
|
| 7 |
DOCUMENTATION = r''' |
| 8 |
--- |
| 9 |
module: win_xml |
| 10 |
short_description: Manages XML file content on Windows hosts |
| 11 |
description: |
| 12 |
- Manages XML nodes, attributes and text, using xpath to select which xml nodes need to be managed. |
| 13 |
- XML fragments, formatted as strings, are used to specify the desired state of a part or parts of XML files on remote Windows servers. |
| 14 |
- For non-Windows targets, use the M(community.general.xml) module instead. |
| 15 |
options: |
| 16 |
attribute: |
| 17 |
description: |
| 18 |
- The attribute name if the type is 'attribute'. |
| 19 |
- Required if C(type=attribute). |
| 20 |
type: str |
| 21 |
count: |
| 22 |
description: |
| 23 |
- When set to C(yes), return the number of nodes matched by I(xpath). |
| 24 |
type: bool |
| 25 |
default: false |
| 26 |
backup: |
| 27 |
description: |
| 28 |
- Determine whether a backup should be created. |
| 29 |
- When set to C(yes), create a backup file including the timestamp information |
| 30 |
so you can get the original file back if you somehow clobbered it incorrectly. |
| 31 |
type: bool |
| 32 |
default: no |
| 33 |
fragment: |
| 34 |
description: |
| 35 |
- 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). |
| 36 |
type: str |
| 37 |
required: false |
| 38 |
aliases: [ xmlstring ] |
| 39 |
path: |
| 40 |
description: |
| 41 |
- Path to the file to operate on. |
| 42 |
type: path |
| 43 |
required: true |
| 44 |
aliases: [ dest, file ] |
| 45 |
state: |
| 46 |
description: |
| 47 |
- Set or remove the nodes (or attributes) matched by I(xpath). |
| 48 |
type: str |
| 49 |
default: present |
| 50 |
choices: [ present, absent ] |
| 51 |
type: |
| 52 |
description: |
| 53 |
- The type of XML node you are working with. |
| 54 |
type: str |
| 55 |
default: element |
| 56 |
choices: [ attribute, element, text ] |
| 57 |
xpath: |
| 58 |
description: |
| 59 |
- Xpath to select the node or nodes to operate on. |
| 60 |
type: str |
| 61 |
required: true |
| 62 |
author: |
| 63 |
- Richard Levenberg (@richardcs) |
| 64 |
- Jon Hawkesworth (@jhawkesworth) |
| 65 |
notes: |
| 66 |
- Only supports operating on xml elements, attributes and text. |
| 67 |
- Namespace, processing-instruction, command and document node types cannot be modified with this module. |
| 68 |
seealso: |
| 69 |
- module: community.general.xml |
| 70 |
description: XML manipulation for Posix hosts. |
| 71 |
- name: w3shools XPath tutorial |
| 72 |
description: A useful tutorial on XPath |
| 73 |
link: https://www.w3schools.com/xml/xpath_intro.asp |
| 74 |
''' |
| 75 |
|
| 76 |
EXAMPLES = r''' |
| 77 |
- name: Apply our filter to Tomcat web.xml |
| 78 |
community.windows.win_xml: |
| 79 |
path: C:\apache-tomcat\webapps\myapp\WEB-INF\web.xml |
| 80 |
fragment: '<filter><filter-name>MyFilter</filter-name><filter-class>com.example.MyFilter</filter-class></filter>' |
| 81 |
xpath: '/*' |
| 82 |
|
| 83 |
- name: Apply sslEnabledProtocols to Tomcat's server.xml |
| 84 |
community.windows.win_xml: |
| 85 |
path: C:\Tomcat\conf\server.xml |
| 86 |
xpath: '//Server/Service[@name="Catalina"]/Connector[@port="9443"]' |
| 87 |
attribute: 'sslEnabledProtocols' |
| 88 |
fragment: 'TLSv1,TLSv1.1,TLSv1.2' |
| 89 |
type: attribute |
| 90 |
|
| 91 |
- name: remove debug configuration nodes from nlog.conf |
| 92 |
community.windows.win_xml: |
| 93 |
path: C:\IISApplication\nlog.conf |
| 94 |
xpath: /nlog/rules/logger[@name="debug"]/descendant::* |
| 95 |
state: absent |
| 96 |
|
| 97 |
- name: count configured connectors in Tomcat's server.xml |
| 98 |
community.windows.win_xml: |
| 99 |
path: C:\Tomcat\conf\server.xml |
| 100 |
xpath: //Server/Service/Connector |
| 101 |
count: true |
| 102 |
register: connector_count |
| 103 |
|
| 104 |
- name: show connector count |
| 105 |
ansible.builtin.debug: |
| 106 |
msg: Connector count is {{ connector_count.count }} |
| 107 |
|
| 108 |
- name: ensure all lang=en attributes to lang=nl |
| 109 |
community.windows.win_xml: |
| 110 |
path: C:\Data\Books.xml |
| 111 |
xpath: //@[lang="en"] |
| 112 |
attribute: lang |
| 113 |
fragment: nl |
| 114 |
type: attribute |
| 115 |
''' |
| 116 |
|
| 117 |
RETURN = r''' |
| 118 |
backup_file: |
| 119 |
description: Name of the backup file that was created. |
| 120 |
returned: if backup=yes |
| 121 |
type: str |
| 122 |
sample: C:\Path\To\File.txt.11540.20150212-220915.bak |
| 123 |
count: |
| 124 |
description: Number of nodes matched by xpath. |
| 125 |
returned: if count=yes |
| 126 |
type: int |
| 127 |
sample: 33 |
| 128 |
msg: |
| 129 |
description: What was done. |
| 130 |
returned: always |
| 131 |
type: str |
| 132 |
sample: "xml added" |
| 133 |
err: |
| 134 |
description: XML comparison exceptions. |
| 135 |
returned: always, for type element and -vvv or more |
| 136 |
type: list |
| 137 |
sample: attribute mismatch for actual=string |
| 138 |
''' |