How to pass second list in to play

被刻印的时光 ゝ 提交于 2019-12-11 19:34:03

问题


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"
            }
        ]
    }

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"

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 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"  

回答1:


Is this the condition you're looking for?

when: list3|intersect(item.names)|length > 0

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) }}"


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.

See Set Theory Filters.



来源:https://stackoverflow.com/questions/57724770/how-to-pass-second-list-in-to-play

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!