如何通过第二个列表来播放 [英] How to pass second list in to play
问题描述
我正在尝试将项目列表应用于 when 条件,但它没有按预期工作
I am trying to apply list of items in to when condition and it is not working as expected
循环中应用的第一个列表
First list which is applied in the loop
{
"list2": [
{
"apname": "Standard",
"dname": "dom-cn-1",
"name": "cluster-01",
"names": [
"device-cn-c1",
"device-cn-c2"
],
"type": "CpmiGatewayclusterter"
},
{
"apname": "PolicyPKG1",
"dname": "dom-cn-1",
"name": "cluster-cn-02",
"names": [
"device-cn-4",
"device-cn-c3"
],
"type": "CpmiGatewayclusterter"
},
{
"apname": "Standard",
"dname": "dom-cn-2",
"name": "cluster-cn-3",
"names": [
"device-cn-5",
"device-cn-6"
],
"type": "CpmiGatewayclusterter"
},
{
"apname": "Standard",
"dname": "dom-cn-2",
"name": "cluster-cn-4",
"names": [
"device-cn-c7",
"device-cn-c8"
],
"type": "CpmiGatewayclusterter"
},
{
"apname": null,
"dname": "dom-cn-1",
"name": "device-cn-4",
"names": null,
"type": "CpmiclusterterMember"
},
{
"apname": null,
"dname": "dom-cn-2",
"name": "device-cn-5",
"names": null,
"type": "CpmiclusterterMember"
},
{
"apname": null,
"dname": "dom-cn-2",
"name": "device-cn-6",
"names": null,
"type": "CpmiclusterterMember"
},
{
"apname": null,
"dname": "dom-cn-1",
"name": "device-cn-c1",
"names": null,
"type": "CpmiclusterterMember"
},
{
"apname": "Standard",
"dname": "dom-cn-1",
"name": "device-cn-c10",
"names": null,
"type": "simple-gateway"
},
{
"apname": null,
"dname": "dom-cn-1",
"name": "device-cn-c2",
"names": null,
"type": "CpmiclusterterMember"
},
{
"apname": null,
"dname": "dom-cn-1",
"name": "device-cn-c3",
"names": null,
"type": "CpmiclusterterMember"
},
{
"apname": null,
"dname": "dom-cn-2",
"name": "device-cn-c7",
"names": null,
"type": "CpmiclusterterMember"
},
{
"apname": null,
"dname": "dom-cn-2",
"name": "device-cn-c8",
"names": null,
"type": "CpmiclusterterMember"
},
{
"apname": null,
"dname": "dom-cn-1",
"name": "dom_cn_1",
"names": null,
"type": "CpmiHostCkp"
},
{
"apname": null,
"dname": "dom-cn-2",
"name": "dom_cn_2",
"names": null,
"type": "CpmiHostCkp"
}
]
}
我在下面的任务中使用上面的列表 (list2)
I am using the above list (list2) in to the below task
- name: Create a change request
snow_record:
state: present
table: u_device
username: admin
password: password
instance: dev970066
data:
u_name: "{{ item.name }}"
u_domain: "{{ item.dname }}"
u_policy: "{{ item.apname }}"
u_cluster: "{{ item.name }}"
loop: "{{ list2 }}"
when:
- item.type == 'CpmiGatewayCluster'
- "'device-cn-c1' in item.name
s"
上述任务按预期工作,但您可以在传递静态值device-cn-c1"的我们条件中看到,我想使用不同的项目列表而不是这个静态变量.
above task is working as expected but you can see in the we condition passing a static value "device-cn-c1", i want to use different list of items instead of this static variable.
示例 list3 有多个设备,我想在 when 条件下循环此列表.(-item.names 中的‘device-cn-c1’")
example list3 have multiple devices, i want to loop this list in when condition. (- "'device-cn-c1' in item.names")
list3:
- device-cn-c1
- device-cn-c2
- device-cn-c3
- device-cn-c10
我想在条件下使用相同的
I want to use the same when condition
when:
- item.type == 'CpmiGatewayCluster'
- "'device-cn-c1' in item.name
但是设备名称应该能够像下面的例子一样循环
But device name should be able to loop like below example
1.
when:
- item.type == 'CpmiGatewayCluster'
- "'device-cn-c1' in item.names"
when:
- item.type == 'CpmiGatewayCluster'
- "'device-cn-c2' in item.names"
when:
- item.type == 'CpmiGatewayCluster'
- "'device-cn-c3' in item.names"
when:
- item.type == 'CpmiGatewayCluster'
- "'device-cn-c4' in item.names"
推荐答案
这是您要寻找的条件吗?
Is this the condition you're looking for?
when: list3|intersect(item.names)|length > 0
可以循环 include_tasks.例如(或者更确切地说是一个提示)
It is possible to loop include_tasks. For example (or rather a hint)
- include_tasks: loop1.yml
loop: "{{ list2 }}"
loop_control:
loop_var: outer_item
when:
- outer_item.type == 'CpmiGatewayCluster'
- list3|intersect(outer_item.names)|length > 0
$ cat loop1.yml
- debug:
msg: "{{ outer_item.apname }} - {{ item }}"
loop: "{{ list3|intersect(outer_item.names) }}"
<小时>细节.list3|intersect(outer_item.names
在 list3
和 outer_item.names
中创建一个公共项目列表.如果此列表为空,循环将被跳过.如果它不是空文件 loop1.yml
将被包含在内.此文件中的循环将遍历公共项目.
Details.
list3|intersect(outer_item.names
creates a list of common items in in list3
and outer_item.names
. If this list is empty the loop will be skipped. If it's not empty file loop1.yml
will be included. The loop inside this file will iterate over the common items.
请参阅设置理论过滤器.
这篇关于如何通过第二个列表来播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!