gantsign / ansible-role-oh-my-zsh

Ansible role for installing and configuring oh-my-zsh
https://galaxy.ansible.com/gantsign/oh-my-zsh
MIT License
165 stars 41 forks source link

how to install ohmyzsh for users using loop? #143

Closed Gictorbit closed 2 years ago

Gictorbit commented 2 years ago

hello, I just want to install oh-my-zsh for multiple users, so how can I do this? Here is my Ansible playbook, but it throws an error (item not found)

- name: setup ohmyzsh for users
  hosts: all
  vars:
    userlist:
      - username: "viktor"
        password: "viktor123"
  become: yes
  roles:
    - role: gantsign.oh-my-zsh
      users:
        - username: "{{ item.username }}"
          oh_my_zsh:
            theme: gnzh
            plugins:
              - git
              - zsh-syntax-highlighting
              - zsh-autosuggestions
              - docker
      loop: "{{ userlist }}"
freemanjp commented 2 years ago

Hi @Gictorbit, unfortunately, loop doesn't work with Ansible roles (only with tasks). Try the following instead:

- name: setup ohmyzsh for users
  hosts: all
  vars:
    userlist:
      - username: "viktor"
        password: "viktor123"
  become: yes
  pre_tasks:
    - name: build oh_my_zsh_users 
      set_fact:
        oh_my_zsh_users: '{{ (oh_my_zsh_users | default([])) + [oh_my_zsh_user] }}'
      vars:
        oh_my_zsh_user:
          username: '{{ item.username }}'
          oh_my_zsh:
            theme: gnzh
            plugins:
              - git
              - zsh-syntax-highlighting
              - zsh-autosuggestions
              - docker
      loop: '{{ userlist }}'
  roles:
    - role: gantsign.oh-my-zsh
      users: '{{ oh_my_zsh_users }}'
Gictorbit commented 2 years ago

Thank you, I also asked this question in stack overflow and solved this using include_role you can check that from link below: stack overflow

freemanjp commented 2 years ago

Yeah, include_role also works, but the advantage of the approach I suggested is it allows you to sequence gantsign.oh-my-zsh relative to other roles in the playbook in the normal way. With include_role you're limited to before (pre_tasks) or after (tasks or post_tasks) all the other roles in the playbook (unless you invoke them with include_role as well).