在Ansible中组合多个循环的输出 [英] Combining the output of multiple loops in Ansible
问题描述
我正在与Ansible合作,在我的剧本中,我正在运行以下任务,其中有多个循环,以便从特定的xml检索不同的标签:
I am working with Ansible and in my playbook I am running the following task, in which I have multiple loops so as to retrieve different tags from a specific xml:
- name: Retrieve multiple xml tags valuei
xml:
xmlstring: "{{ item.string }}"
xpath: "{{ item.path }}"
content: text
loop:
- { path: "/rpc-reply/vlan-instance-information/vlan-instance-group/vlan-member/vlan-tag", string: "{{topology.xml}}" }
- { path: "/rpc-reply/vlan-instance-information/vlan-instance-group/vlan-member/vlan-member-interface", string: "{{topology.xml}}" }
register: tags_value
- debug:
msg: "{{ item.matches }}"
loop: "{{ tags_value.results }}"
loop_control:
label: "{{ item.matches }}"
所以我得到以下信息:
ok: [sss-sd1-02] => (item=[{u'vlan-member-interface': u'et-0/0/0.0*'}, {u'vlan-member-interface': u'et-0/0/1.0*'}]) => {
"msg": [
{
"vlan-member-interface": "et-0/0/0.0*"
},
{
"vlan-member-interface": "et-0/0/1.0*"
}
]
}
还有这个
ok: [sss-sd1-02] => (item=[{u'vlan-tag': u'4071'}, {u'vlan-tag': u'4072'}]) => {
"msg": [
{
"vlan-tag": "4071"
},
{
"vlan-tag": "4072"
}
]
}
有没有一种方法可以在此结果中或在另一结果中将"vlan-member-interface":"et-0/0/0.0 *"和"vlan-tag":"4071"分组?还有一种方法可以只创建{4071,4072}的列表?因为我现在无法处理它!!!
Is there a way to group "vlan-member-interface": "et-0/0/0.0*" and "vlan-tag": "4071" in 1 result , either in this task or in a different one ? And also is there a way to create a list with only {4071, 4072} ? Because i cannot handle it as it is right now !!!
推荐答案
问题可以简化.让我们来获取数据
The question can be simplified. Let's have the data
results:
- [A: a1, A: a2]
- [B: b1, B: b2]
Q1:"组"A:a1"和"B:b1"在1个结果中"
A:使用zip
功能.例如
- set_fact:
my_groups: "{{ results.0|zip(results.1)|list }}"
- debug:
var: my_groups
给予
"my_groups": [
[
{
"A": "a1"
},
{
"B": "b1"
}
],
[
{
"A": "a2"
},
{
"B": "b2"
}
]
]
}
Q2:仅创建一个[b1,b2]的列表"
A:使用dict2items
和json_query
功能.例如
A: Use dict2items
and json_query
functions. For example
- set_fact:
my_values: "{{ results.1|map('dict2items')|list|json_query('[].value') }}"
- debug:
var: my_values
给予
"my_values": [
"b1",
"b2"
]
问题1替换为:1个结果中的组"vlan-member-interface":"et-0/0/0.0 *"和"vlan-tag":"4071"
问题2替换为:仅创建一个{4071,4072}
的列表 变量 results 是 tags_value.results 的替换
Question 1 is substitution of: Group "vlan-member-interface": "et-0/0/0.0*" and "vlan-tag": "4071" in 1 result
Question 2 is substitution of: Create a list with only {4071, 4072}
Variable results is substitution of tags_value.results
这篇关于在Ansible中组合多个循环的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!