- name: Create user
  user:
    name: "{{ item.key }}"
    group: "{{ item.value.group | default(item.key) }}"
    groups: "{{ item.value.groups | community.general.json_query('[*].name') }}"
    shell: "{{ item.value.shell | default('/usr/local/bin/zsh') }}"
    uid: "{{ item.value.uid | default(omit) }}"
  loop: "{{ users | dict2items | community.general.json_query('[?value.initialize==`true`]') }}"
  when: item.value.groups is defined

- name: Create user
  user:
    name: "{{ item.key }}"
    group: "{{ item.value.group | default(item.key) }}"
    shell: "{{ item.value.shell | default('/usr/local/bin/zsh') }}"
    uid: "{{ item.value.uid | default(omit) }}"
  loop: "{{ users | dict2items | community.general.json_query('[?value.initialize==`true`]') }}"
  when: item.value.groups is not defined
