ansible-collections / community.general

Ansible Community General Collection
https://galaxy.ansible.com/ui/repo/published/community/general/
GNU General Public License v3.0
816 stars 1.5k forks source link

xml: change attribute only if xpath selection exists #5200

Open gevezex opened 2 years ago

gevezex commented 2 years ago

Summary

I am trying to change an attribute, only if the xpath exists. This works as designed if the xpath has a match. But if there is no xpath match then it creates also the element, what is not desired in my case. How can I change this default behaviour.

- name: Replace logstash hosts with xpath
     community.general.xml:
       path: /tmp/ansible/admin-log4j.xml
       xpath: /Configuration/Appenders/Socket[@remoteEnd='clv']
       attribute: 'host'
       value: '{{ clv_syslog_host }}'
    delegate_to: localhost
    become: no

Issue Type

Documentation Report

Component Name

xml

Ansible Version

ansible [core 2.13.3] config file = /etc/ansible/ansible.cfg configured module search path = ['/home/dbxaci0/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /home/dbxaci0/miniconda3/lib/python3.9/site-packages/ansible ansible collection location = /home/dbxaci0/.ansible/collections:/usr/share/ansible/collections executable location = /home/dbxaci0/miniconda3/bin/ansible python version = 3.9.12 (main, Apr 5 2022, 06:56:58) [GCC 7.5.0] jinja version = 3.1.2 libyaml = True

Community.general Version

$ ansible-galaxy collection list community.general

Configuration

$ ansible-config dump --only-changed

OS / Environment

No response

Additional Information

No response

Code of Conduct

ansibullbot commented 2 years ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help

ansibullbot commented 2 years ago

cc @cmprescott @dagwieers @sm4rk0 @tbielawa click here for bot help

russoz commented 2 years ago

hi @gevezex Thanks for submitting this issue.

I haven't used the xml module myself, but taking a look at the docs it seems that: option content will give you the current state of the XML doc. Based on the results of that, you can have your playbook changing the attribute or not.

Keep in mind that Ansible modules are meant to be idempotent, so expressing a desired state will produce that outcome, regardless of the initial state.

ansibullbot commented 1 year ago

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the !component bot command.

click here for bot help