<p>How about something like this:</p>

<pre><code>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
</code></pre>

CC BY-SA 3.0
2017-04-22T07:23:16.073Z
2017-04-22T07:23:16.073Z
Jiri Klouda
<p>I'm using the <a href="https://galaxy.ansible.com/willshersystems/users/" rel="noreferrer">willshersystems.users</a> 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.</p>

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

<pre><code>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
</code></pre>

<ansible>
Referencing current variable in Ansible conditional statement?
bradym
# Referencing current variable in Ansible conditional statement?

(Asked by: username_0 on 2017-04-21)

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

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2017-04-22. Score: 4 | Accepted answer

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


---

