- name: install oh-my-zsh dependencies
  become: yes
  package:
    name:
      - git
      - zsh
    state: present

- name: clone oh-my-zsh for users
  git:
    repo: 'https://github.com/robbyrussell/oh-my-zsh.git'
    dest: '~{{ user.username }}/.oh-my-zsh'
    clone: yes
    depth: 1
  become: yes
  become_user: '{{ user.username }}'
  when: user.oh_my_zsh is defined
  with_items: "{{ users }}"
  loop_control:
    loop_var: user

- name: set permissions of oh-my-zsh for users
  file:
    path: '~{{ user.username }}/.oh-my-zsh'
    mode: 'go-w'
    recurse: yes
  when: user.oh_my_zsh is defined
  with_items: "{{ users }}"
  loop_control:
    loop_var: user

- name: set default shell for users
  user:
    name: '{{ user.username }}'
    shell: /bin/zsh
  when: user.oh_my_zsh is defined
  with_items: "{{ users }}"
  loop_control:
    loop_var: user

- name: write .zshrc for users
  template:
    src: zshrc.j2
    dest: '~{{ user.username }}/.zshrc'
    backup: yes
    mode: 'u=rw,go=r'
  become_user: '{{ user.username }}'
  when: user.oh_my_zsh is defined
  with_items: "{{ users }}"
  loop_control:
    loop_var: user