如何通过第二个清单播放 [英] 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.
example list3有多个设备,我想在条件出现时循环此列表. (-"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
我想在条件满足时使用
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屋!