Home > Mobile >  Installing ohmyzsh for multiple users using ansible?
Installing ohmyzsh for multiple users using ansible?

Time:07-05

I want to install ohmyzsh for a list of users using gantsign.oh-my-zsh role which is available in ansible galaxy gantsign.oh-my-zsh so i have to run this role for users here is my playbook

- 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 }}"

  tasks:

but I got this error after running playbook

fatal: [worker1]: FAILED! => {"msg": "[{'username': '{{ item.username }}', 'oh_my_zsh': {'theme': 'gnzh', 'plugins': ['git', 'zsh-syntax-highlighting', 'zsh-autosuggestions', 'docker']}, 'loop': '{{ userlist }}'}]: 'item' is undefined"}
fatal: [worker2]: FAILED! => {"msg": "[{'username': '{{ item.username }}', 'oh_my_zsh': {'theme': 'gnzh', 'plugins': ['git', 'zsh-syntax-highlighting', 'zsh-autosuggestions', 'docker']}, 'loop': '{{ userlist }}'}]: 'item' is undefined"}
fatal: [registry1]: FAILED! => {"msg": "[{'username': '{{ item.username }}', 'oh_my_zsh': {'theme': 'gnzh', 'plugins': ['git', 'zsh-syntax-highlighting', 'zsh-autosuggestions', 'docker']}, 'loop': '{{ userlist }}'}]: 'item' is undefined"}

CodePudding user response:

You can't use loops inside the task level roles: keywords. Use include_role inside your tasks instead (untested example):

- name: setup ohmyzsh for users
  hosts: all
  vars:
    userlist:
      - username: "viktor"
        password: "viktor123"
  become: yes

  tasks:
    - name: run role for each user
      include_role:
        name: gantsign.oh-my-zsh
      vars:
        users:
          - username: "{{ item.username }}"
            oh_my_zsh:
              theme: gnzh
              plugins:
                - git
                - zsh-syntax-highlighting
                - zsh-autosuggestions
                - docker
      loop: "{{ userlist }}"

CodePudding user response:

Answer for the generic looping over ansible roles using include_role.

As for the specific problem, the role expects its list of users as a role variable, so you do not need to loop over the role at all, just pass all users to the role itself, as the oh-my-zsh role docs suggest.

- hosts: all
  vars:
    oh_my_zsh_defaults: 
      theme: gnzh
      plugins:
        - git
        - zsh-syntax-highlighting
        - zsh-autosuggestions
        - docker
  roles:
    - role: gantsign.oh-my-zsh
      users:
        - username: "viktor"
          oh_my_zsh: oh_my_zsh_defaults
        - username: "otheruser"
          oh_my_zsh: oh_my_zsh_defaults
  • Related