1
我是Ansible/Jinja的新手,所以这可能是一个基本问题。我使用的核心URI模块使一个REST API调用的网络设备如下:使用URI和XML模块的可变变量赋值
---
- name: Test PAN API
hosts: fw
connection: local
gather_facts: False
tasks:
- name: Calling API System Info
action: uri url=https://192.168.1.10/api/?type=op&cmd=<show><system><info></info></system></show>&key=thisismysecretkey return_content=yes validate_certs=no
register: result
- name: Set variable
set_fact: sysinfo="{{ result.content }}”
- name: Parsing XML response
action: xml xmlstring="{{ sysinfo }}" xpath=//system/* content=text
register: hn
- debug: var=hn.matches['hostname']
我想每个XML节点的解析为变量,使得主机名= PA-VM等这里是响应的样子:
TASK [debug var=sysinfo] *******************************************************
ok: [pan] => {
"changed": false,
"sysinfo": "<response status=\"success\"><result><system><hostname>PA-VM</hostname><ip-address>192.168.1.10</ip-address><netmask>255.255.255.0</netmask></system></result></response>"
}
TASK [Testing XML] *************************************************************
ok: [pan]
TASK [debug var=hn.matches] ****************************************************
ok: [pan] => {
"changed": false,
"hn.matches": [
{
"hostname": "PA-VM"
},
{
"ip-address": "192.168.1.10”
},
{
"netmask": "255.255.255.0"
}
]
}
我已经尝试过不同的金贾过滤器,但我觉得我好像缺少了一些简单的东西。看起来hn.matches是一个列表,每个键值都是一个字符串。例如,如果我...
- debug: var=hn.matches[0]
我得到...
TASK [debug var=hn.matches[0]] *************************************************
ok: [pan] => {
"changed": false,
"hn.matches[0]": {
"hostname": "PA-VM"
}
}
将是真正伟大的是什么...
set_fact: hn="{{ response.result.system.hostname }}"
只是为了寻找清洁/最佳没有使用正则表达式。
谢谢你 - 这做到了! – 66pontiac