gpt4 book ai didi

xml-parsing - 如何解析 ansible 中的 XML 响应?

转载 作者:行者123 更新时间:2023-12-05 02:16:30 29 4
gpt4 key购买 nike

我正在运行 panos_op ansible 模块并努力解析输出。

ok: [localhost] => {
"result": {
"changed": true,
"failed": false,
"msg": "Done",
"stdout": "{\"response\": {\"@status\": \"success\", \"result\": \"no\"}}",
"stdout_lines": [
"{\"response\": {\"@status\": \"success\", \"result\": \"no\"}}"
],
"stdout_xml": "<response status=\"success\"><result>no</result></response>"
}
}

这已经接近于为“结果”赋值了。

ok: [localhost] => {
"result.stdout": {
"response": {
"@status": "success",
"result": "no"
}
}
}

我的目标是为 ansible 任务设置一个条件循环。

tasks:
- name: Checking for pending changes
panos_op:
ip_address: '{{ host }}'
password: '{{ operator_pw }}'
username: '{{ operator_user}}'
cmd: 'check pending-changes'
register: result
until: result.stdout.result = no
retries: 10
delay: 5
tags: check

我怎样才能让它工作?

更新:我试过另一种方法,但现在我遇到了一个新问题,试图处理文字“<”字符。

tasks:
- name: Checking for pending changes
panos_op:
ip_address: '{{ host }}'
password: '{{ operator_pw }}'
username: '{{ operator_user}}'
cmd: 'check pending-changes'
register: result

- fail:
msg: The Firewall has pending changes to commit.
when: '"<result>no"' not in result.stdout_xml

错误: 没有找到预期的 key

如有任何帮助,我们将不胜感激。

最佳答案

正如我刚才提到的 in another answer , 从 Ansible 2.4 开始,有 an xml module .

剧本

---
- hosts: localhost
gather_facts: false

tasks:
- name: Get result from xml.
xml:
xmlstring: "<response status=\"success\"><result>no</result></response>"
content: "text"
xpath: "/response/result"

输出

PLAY [localhost] ***************************************************************

TASK [Get result from xml.] ****************************************************
ok: [localhost] => changed=false
actions:
namespaces: {}
state: present
xpath: /response/result
count: 1
matches:
- result: 'no'
msg: 1
xmlstring: |-
<?xml version='1.0' encoding='UTF-8'?>
<response status="success"><result>no</result></response>

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

关于xml-parsing - 如何解析 ansible 中的 XML 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49866698/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com