Ansible: 读取属性值和访问i的XML模块

2024-10-16 20:40:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Ansible playbook从一个XML文件中提取一个特定的属性值,如the Ansible XML module page中所述,但是得到一个错误“Xpath/business/website/validxhtml不引用节点”。

XML文件:

<business type="bar">
  <name>Tasty Beverage Co.</name>
    <beers>
      <beer>Rochefort 10</beer>
      <beer>St. Bernardus Abbot 12</beer>
      <beer>Schlitz</beer>
    </beers>
  <rating subjective="true">10</rating>
  <website>
    <mobilefriendly/>
    <address>http://tastybeverageco.com</address>
  </website>
</business>

YAML文件:

^{pr2}$

回应:

PLAY [testing XMl values] ************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [localhost]

TASK [Read an element's attribute values] ********************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Xpath /business/website/validxhtml does not reference a node!"}

PLAY RECAP ***************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

我使用的示例和Ansible xml页面中指定的相同,但不确定它失败的原因。请帮助。


Tags: 文件namelocalhostplayaddresswebsitexmlansible
1条回答
网友
1楼 · 发布于 2024-10-16 20:40:27

这是因为示例xml中既不存在validxhtml元素,也不存在{}。首先,类似于XML的HTML有元素和属性,两者都是不同的东西Ref

因此,您需要添加带有要提取的属性值的validxhtml元素,或者在xml文件中使用其他具有属性例如<rating subjective="true">10</rating>的元素。我希望这有帮助。在

[更新] 如请求的那样,如果您只想获得元素值,只需尝试它应该返回10。在

- name: Read an element's attribute values
  xml:
    path: /home/ansible/test.xml
    xpath: /business/rating
    content: text
  register: xmlresp

- name: Show an attribute value
  debug:
    var: xmlresp.matches

相关问题 更多 >