使用 Ansible 模塊community.general.xml,我試圖填充屬性值,洗掉現有的子級并添加我自己的。給定檔案,我無法識別正確的 xpath:
<?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>
這些是我打算使用的任務的片段,但我無法識別 xpath。我對 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"'
有人可以給我一個提示嗎?
uj5u.com熱心網友回復:
您的第一個問題是您的 XML 檔案設定了默認名稱空間,這意味著它沒有名為Account. 它有一個Account 在命名空間中http://www.pulseway.com/linuxconfig命名的元素......所以你需要提供一個namespaces字典作為你的模塊呼叫的一部分。
這讓我們:
- 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"
在您的示例資料上運行上述內容會產生:
<?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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/521249.html
標籤:xml可靠的
