Win_xml help cannot edit attributes

I am trying to use win_xml module. Xml file is simple as following

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <setting name="mode" value="development"/>
  <setting name="timeout" value="30"/>
</config>

Play is

tasks:
    - name: Set timeout to 60 seconds
      win_xml:
        path: C:\temp\sample.xml
        xpath: /config/setting[@name='timeout']
        attribute: value
        value: '60'
        state: present

What I get as an error

The full traceback is:
You cannot call a method on a null-valued expression.
At line:188 char:13
+             $candidate = $xmlorig.CreateElement($xmlfragment.get_Docu ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvokeMethodOnNull

ScriptStackTrace:
at <ScriptBlock>, <No file>: line 188
fatal: [192.168.68.135]: FAILED! => {
    "changed": false,
    "msg": "Unhandled exception while executing module: You cannot call a method on a null-valued expression."

}
And I don’t understand why .

ansible [core 2.18.5]
  config file = None
  configured module search path = ['/home/ale/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/ale/ansible/ansible/lib/python3.12/site-packages/ansible
  ansible collection location = /home/ale/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/ale/ansible/ansible/bin/ansible
  python version = 3.12.3 (main, Feb  4 2025, 14:48:35) [GCC 13.3.0] (/home/ale/ansible/ansible/bin/python3)
  jinja version = 3.1.6
  libyaml = True

community.windows 2.4.0

Based on a quick look at the parameters section of the win_xml module doc it looks like you’re using value where the module is expecting fragment, and so fragment is a null-valued expression.

That’s where I’d start.

1 Like

Thank you :slight_smile:
Now it makes sense

tasks:
    - name: Set timeout to 60 seconds
      win_xml:
        path: C:\temp\sample.xml
        xpath: /config/setting[@name='timeout']
        attribute: value
        fragment: '60'
        type: attribute

And it worked could not understand it before :slight_smile: