ansible-collections / community.windows

Windows community collection for Ansible
https://galaxy.ansible.com/community/windows
GNU General Public License v3.0
198 stars 153 forks source link

win_xml: Prevent issue when no childnode is defined #482

Closed RyanBijkerk closed 1 year ago

RyanBijkerk commented 1 year ago
SUMMARY

It is not possible to add a new element to an empty element. To prevent this I have added logic to add an element when the node does not contain any childnodes. Please take a look at the additional information for more details and examples.

ISSUE TYPE
COMPONENT NAME

win_xml

ADDITIONAL INFORMATION

My goal was to add a simple httpRedirect configuration in an existing web.config of an IIS server. In my example, I only had an empty configuration as by base XML.

Playbook:

- name: Create httpRedirect
  win_xml:
    path: C:\inetpub\wwwroot\web.config
    xpath: "/configuration/system.webServer"
    fragment: "<httpRedirect />"

Base XML:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
  </system.webServer>
</configuration>

In this example, no changes are made. When adjusting the XML by adding a text element:

Base XML:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    example
  </system.webServer>
</configuration>

Result XML:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    example
  <httpRedirect /></system.webServer>
</configuration>

Behavior when using the pull request adjustment:

Base XML:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
  </system.webServer>
</configuration>

Result:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <httpRedirect />
  </system.webServer>
</configuration>

Tested this in two separate environments on both Windows Server 2019 and Windows Server 2022 with the ansible versions: 2.10.17 & 2.13.7

RyanBijkerk commented 1 year ago

Can someone please explain why these Sanity tests fail? Never mind, I found it! Added a minor update to prevent failed tests.

RyanBijkerk commented 1 year ago

Added both test and change log fragment.