我正在尝试将项目列表应用到when条件,但无法按预期运行
在循环中应用的第一个列表
{
"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)
- 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”的条件下,我想使用其他项目列表代替此静态变量。
example list3有多个设备,我想在条件出现时循环此列表。 (-“ item.names中的'device-cn-c1'”)
list3:
- device-cn-c1
- device-cn-c2
- device-cn-c3
- device-cn-c10
我想在条件满足时使用
when:
- item.type == 'CpmiGatewayCluster'
- "'device-cn-c1' in item.name
但是设备名称应该可以像下面的示例一样循环
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"
答案 0 :(得分:0)
这是您要寻找的条件吗?
when: list3|intersect(item.names)|length > 0
可以循环include_tasks。例如(或更确切地说是一个提示)
- 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
。该文件内的循环将遍历常见项目。