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

On Fri, Aug 4, 2017 at 1:55 PM, Tim Gaastra <[email protected]> wrote:

> 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
> ----------
> active_system_roles:
>    - Admin
>    - DBA
>
> group-b
> ----------
> active_system_roles:
>    - Admin
>    - Developer
>
> I'd like a task that could do something like (aware this isn't real code):
>
> tasks:
>    - name: Test
>      debug:
>          msg: "Do a thing to that user on this host"
>      with_items: "{{ users }}"
>      when: item.employee_roles in hostvars[inventory_hostname].
> active_system_roles
>
> I.E. if any of the employee's roles are in the list of
> active_system_roles, do a thing to that user on that host.
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/ansible-project/95025760-a26b-4a13-bdf4-60294f84d325%40googlegroups.
> com
> <https://groups.google.com/d/msgid/ansible-project/95025760-a26b-4a13-bdf4-60294f84d325%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAPrnkaTpHjq9EQcVwNBjx0vbgMhd4%2BTX3QWyRHKyOe1JQHJEHQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to