list containing any item of another list

20 views
Skip to first unread message

Adam E

unread,
Apr 3, 2019, 6:49:50 PM4/3/19
to Ansible Project
Hi, wondering if there is any cleaner way to see if two lists have 1 or more intersecting values?

the following works for me

# returns true
{{ ['value1', 'value2'] | intersect(['value1', 'value2']) | length > 0 }}
# returns false
{{ ['value3', 'value4'] | intersect(['value1', 'value2']) | length > 0 }}

just wondering if there is a cleaner looking solution that I am not aware of as I am using it frequently.  
something like would be handy. 
{{ ['value1', 'value2].any(['value1', 'value2']) }}
Message has been deleted
Message has been deleted

Soniya panwar

unread,
Apr 5, 2019, 7:04:15 AM4/5/19
to Ansible Project
Hello Adam E,

You have got the implementation correct. Intersect is the correct method to be used.  
You may refer the playbook mentioned below: 

---
- hosts: localhost
  vars:
        list1: ['value1', 'value2]
        list2: ['value3', 'value4']

  tasks:
  - name: intersrction
    debug:
     msg: "{{ list1 | intersect(list2) | length > 0}}"
    register: List

  - name: Print message
    debug:
      msg: "{{ List }}"

Thanks
Soniya
Reply all
Reply to author
Forward
0 new messages