Test for set membership from a set?

Is there any way in Ansible to succinctly test for set membership in a list, from a list?

An example in psuedo-Ansible code:

users:

  • name: Jim
    employee_roles:
  • Admin
  • name: Bob
    employee_roles:
  • DBA
  • name: Suz
    employee_roles:
  • Developer
  • name: Kev
    employee_roles:
  • DBA
  • Developer

Then, in group vars for a set of hosts:

group-a

I think the intersect() filter is what you are looking for:
http://docs.ansible.com/ansible/latest/playbooks_filters.html#set-theory-filters

Here’s what I think (untested) your task would look like:

tasks:

  • name: Test

debug:

msg: “Do a thing to {{ item }} on {{ inventory_hostname }}”

with_items: “{{ users }}”

when: item.employee_roles | intersect(hostvars[inventory_hostname].active_system_roles)

-Toshio

This does seem to do the trick. Now I just need to experiment with it a bit to see how it works with “with_subelements” type iterators.