xml - 如何从 Ansible 中的 xml 响应中读取和检索多个值
问题描述
我正在使用 Ansible,我正在尝试从一个 xml 中检索多个标签的值。我找到了如何从 1 个标签中检索值,但我需要从许多不同的标签中检索值。特别是,我知道如果你想检索 1 个标签的值,你可以在你的剧本中这样做:How to parse a XML response in ansible?
所以我可以使用这个 xml 模块,但是如果我想拥有多个 'xpath' 怎么办?
解决方案
如果我理解正确并基于您链接中提供的示例。我会使用带有子键的 Ansible 循环:
剧本.yml
---
- hosts: localhost
gather_facts: no
tasks:
- name: Retrieve multiple xml tags value
xml:
xmlstring: "{{ item.string }}"
xpath: "{{ item.path }}"
content: text
loop:
- { path: "/value", string: "<value>foo</value>" }
- { path: "/tag/other-value", string: "<tag><other-value>bar</other-value></tag>" }
register: tags_value
- debug:
msg: "{{ item.matches }}"
loop: "{{ tags_value.results }}"
loop_control:
label: "{{ item.matches }}"
结果
PLAY [localhost] *******************************************************************************************************************************************************************************************
TASK [Retrieve multiple xml tags value] ********************************************************************************************************************************************************************
ok: [localhost] => (item={u'path': u'/value', u'string': u'<value>foo</value>'})
ok: [localhost] => (item={u'path': u'/tag/other-value', u'string': u'<tag><other-value>bar</other-value></tag>'})
TASK [debug] ***********************************************************************************************************************************************************************************************
ok: [localhost] => (item=[{u'value': u'foo'}]) => {
"msg": [
{
"value": "foo"
}
]
}
ok: [localhost] => (item=[{u'other-value': u'bar'}]) => {
"msg": [
{
"other-value": "bar"
}
]
}
PLAY RECAP *************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0
推荐阅读
- python - 如何将日期时间列标题(例如 2007-03-01 00:00:00)转换为日期-月-年格式,即 2007-03-01
- python - ksize和k在cornerHarris中是什么意思?
- python - Python中带有matplotlib的线性模型的3d曲面图
- c# - 两个不同的用户可以访问同一个数据库表来插入不同的数据吗?
- php - 在搜索结果中显示 2 列中 1 个表的信息。(PHP - Laravel)
- ios - Firebase 错误 - 来自此 ios 应用程序的请求
被封锁 - android - 从一个片段导航到另一个片段
- c# - 班级内的后台工作人员,我必须等待再次运行
- sql - Postgres SQL 选择查询语法
- random - 在单元格更改时从选定范围返回随机日期