Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Find List Membership in a List in Ansible

Tags:

ansible

I have a list that I would like to use to see if any of the members of the list are in a second list, and use that in an Ansible when clause. Can this be done in a one-liner?

- name: task include moar ansible
  include: more_tasks.yaml
  when: any_member_of_this_list in some_other_list
like image 734
Randy Avatar asked Oct 28 '25 09:10

Randy


1 Answers

This is quite easy. Ansible introduced some extra Jinja filters, and one of them is intersect, which returns unique elements which are contained in two lists. Since an empty list is falsy and a non-empty list is truthy, there is not more to be done than this:

when: any_member_of_this_list | intersect(some_other_list)
like image 65
udondan Avatar answered Oct 31 '25 07:10

udondan