Using the Ansible module community.general.xml
, I am attempting to populate attribute values, delete existing children and add my own. I can't get past identifying the correct xpath given the file:
<?xml version="1.0" encoding="utf-8"?>
<config xmlns="http://www.pulseway.com/linuxconfig">
<Account Username="" Password="" UseCustomServer="false" CustomServerAddress="" Token=""/>
<ComputerInformation Name="MyLittleServer" Group="Linux"/>
<MonitoredServices>
<Service Name="chrony.service" DisplayName="Chrony" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="false" Enabled="false"/>
<Service Name="firewalld.service" DisplayName="Firewalld" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="false" Enabled="false"/>
<Service Name="postfix.service" DisplayName="Postfix" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="false" Enabled="true"/>
<Service Name="sshd.service" DisplayName="SSHD" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="false" Enabled="true"/>
</MonitoredServices>
</config>
These are snippets of the tasks I intend to use, but I fail to identify xpath. I'm completely ignorant on XML.
- name: Set the Password
community.general.xml:
path: /etc/pulseway/config.xml
xpath: /Account[@Username=""]
attribute: Password
value: "SecretSquirrel"
state: present
- name: Remove all children from the 'MonitoredServices' element
community.general.xml:
path: /etc/pulseway/config.xml
xpath: /config/MonitoredServices/*
state: absent
- name: Add Services to the 'MonitoredServices' element
community.general.xml:
path: /etc/pulseway/config.xml
xpath: /config/MonitoredServices/
add_children:
- Service Name: '"chronyd.service" DisplayName="ChronyD" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="true" Enabled="true"'
- Service Name: '"firewalld.service" DisplayName="FirewallD" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="true" Enabled="true"'
- Service Name: '"postfix.service" DisplayName="Postfix" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="true" Enabled="true"'
- Service Name: '"sshd.service" DisplayName="SSHD" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="true" Enabled="true"'
Could someone give me a hint?
CodePudding user response:
Your first problem is that your XML document sets a default namespace, which means it doesn't have an element named Account
. It has an element named Account
in the http://www.pulseway.com/linuxconfig
namespace...so you need to provide a namespaces
dictionary as part of your module invocation.
That gets us:
- hosts: localhost
gather_facts: false
tasks:
- name: Set the Password
community.general.xml:
path: config.xml
namespaces:
pulseway: http://www.pulseway.com/linuxconfig
xpath: "/pulseway:config/pulseway:Account[@User='']"
attribute: Password
value: "SecretSquirrel"
state: present
- name: Remove all children from the 'MonitoredServices' element
community.general.xml:
path: config.xml
namespaces:
pulseway: http://www.pulseway.com/linuxconfig
xpath: /pulseway:config/pulseway:MonitoredServices/*
state: absent
- name: Add Services to the 'MonitoredServices' element
community.general.xml:
path: config.xml
namespaces:
pulseway: http://www.pulseway.com/linuxconfig
xpath: /pulseway:config/pulseway:MonitoredServices
add_children:
- Service:
Name: "chronyd.service"
DisplayName: "ChronyD"
IsDaemon: "true"
DaemonType: "SYSTEMD"
Path: ""
StartParameters: ""
CanBeStopped: "true"
Enabled: "true"
- Service:
Name: "firewalld.service"
DisplayName: "FirewallD"
IsDaemon: "true"
DaemonType: "SYSTEMD"
Path: ""
StartParameters: ""
CanBeStopped: "true"
Enabled: "true"
Running the above on your example data produces:
<?xml version='1.0' encoding='UTF-8'?>
<config xmlns="http://www.pulseway.com/linuxconfig">
<Account Username="" Password="" UseCustomServer="false" CustomServerAddress="" Token=""/>
<ComputerInformation Name="MyLittleServer" Group="Linux"/>
<MonitoredServices>
<Service Name="chronyd.service" DisplayName="ChronyD" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="true" Enabled="true"/><Service Name="firewalld.service" DisplayName="FirewallD" IsDaemon="true" DaemonType="SYSTEMD" Path="" StartParameters="" CanBeStopped="true" Enabled="true"/></MonitoredServices>
<Account User="" Password="SecretSquirrel"/></config>