Need help with nested variables to use in a "when:" directive inside of a role playbook

I have a requirement to create directories only if the specified user(s) exist on the remote host… given my role defintion below, what is the proper syntax to use with “when” (or other method) to only create the directory only if that user exists on the host and skip the task if not present?

…/vars/main.yml:


my_user_list:

  • user1
  • user2

…/tasks/main.yml:

  • name: Check for existence of users
    getent: database=passwd key={{item}} fail_key=False
    with_items: my_user_list
    register: passinfo
  • name: Ensure user directory exists (user1)
    file:
    path=/opt/user1
    state=directory
    owner=user1
    group=user1
    mode=0755
    when: ??? filter to only run if user1 exists ???
    ignore_errors: yes
  • name: Ensure user directory exists (user2)
    file:
    path=/opt/user2
    state=directory
    owner=user2
    group=user2
    mode=0755
    when: ??? filter to only run if user2 exists ???
    ignore_errors: yes

here is the “output” of the passinfo variable:

Try this:

  • name: Ensure user directory exists
    file:
    path=/opt/{{ item.item }}
    state=directory
    owner={{ item.item }}
    group={{ item.item }}
    mode=0755
    with_items: passinfo.results

not tested though.