如何解析Ansible中的XML响应? [英] How to parse a XML response in ansible?
本文介绍了如何解析Ansible中的XML响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在运行panos_op ansible模块,并努力解析输出.
I'm running the panos_op ansible module and struggling to parse the output.
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>"
}
}
这与分配结果"的值差不多.
This is as close as I can get to assigning the value for "result".
ok: [localhost] => {
"result.stdout": {
"response": {
"@status": "success",
"result": "no"
}
}
}
我的目标是为ansible任务设置条件循环.
My goal is to set a conditional loop for the ansible task.
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
我该如何进行这项工作?
How can I make this work?
更新:我已经尝试了另一种方法,但是现在遇到了一个新问题,试图处理文字<"字符.
UPDATE: I've tried it another way, but now I have a new issue trying to deal with a literal "<" char.
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
错误:找不到预期的密钥
ERROR: did not find expected key
任何帮助都将不胜感激.
Any help at all would be very appreciated.
推荐答案
正如我刚才提到的在另一个答案中,自Ansible 2.4起,出现了一个xml模块.
As I just mentioned in another answer, since Ansible 2.4, there's 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
这篇关于如何解析Ansible中的XML响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文