Referencing current variable in Ansible conditional statement? I'm using the willshersystems.users role from ansible galaxy for creating users on ubuntu servers. Now that I've got it working consistently, I'm trying to set the state attribute based on whether the user should have access to a specific server given a list.

Is something like the below possible? My google fu seems to be failing me at the moment, any advice would be very appreciated!

vars:
  user_servers:
    user1:
      - testing1

  users:

    - name: user1
      groups: '{{ user_groups }}'
      state: "{{ ( inventory_hostname_short in user_servers[self.name] ) | ternary('present','absent') }}"

roles:
  - role: willshersystems.users
How about something like this:

hosts: all

vars:
  user_servers:
    user1:
      - testing1

  users:
    -name: user1
    -groups: '{{ user_groups }}'
    -state: present

roles:
  - name: Add filtered users
    tasks:
      - name: Filter users 
        debug: User {{ item }}
        with_items: "{{ users | default([]) }}"
        when: {{ inventory_hostname_short }} in user_servers[item]
        register: filtered_users  

      - name: Add filtered users
        include_role: wilshersystems.users
        with_items:
        - {{ filtered_users }}
        loop_control:
        - loop_var: users