diff --git a/README.md b/README.md index 6ea5aae..9d83a90 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,10 @@ oh_my_zsh_theme: robbyrussell oh_my_zsh_plugins: - git +# Wether to install by default for all specified users. +# May be overridden by `oh_my_zsh: install:` under each user. +oh_my_zsh_install: yes + # User configuration # Important: oh-my-zsh is installed per user so you need to specify the users to install it for. users: @@ -77,6 +81,9 @@ users: plugins: - git - mvn + - username: example3 + oh_my_zsh: + install: no ``` Example Playbook diff --git a/defaults/main.yml b/defaults/main.yml index 13e789a..29a4956 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -5,3 +5,7 @@ oh_my_zsh_theme: robbyrussell # Default plugins oh_my_zsh_plugins: - git + +# Wether to install by default for all specified users. +# May be overridden by `oh_my_zsh_install` under each user. +oh_my_zsh_install: yes diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 4405c2a..1c0ba6d 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -19,6 +19,9 @@ with_items: - test_usr1 - test_usr2 + - test_usr3 + - test_usr4 + - test_usr5 - name: install console-setup file become: yes @@ -44,3 +47,22 @@ plugins: - test_plugin3 - test_plugin4 + - username: test_usr3 + oh_my_zsh: + install: no + + - role: ansible-role-oh-my-zsh + oh_my_zsh_theme: test_theme1 + oh_my_zsh_plugins: + - test_plugin1 + - test_plugin2 + oh_my_zsh_install: no + users: + - username: test_usr4 + - username: test_usr5 + oh_my_zsh: + install: yes + theme: test_theme2 + plugins: + - test_plugin3 + - test_plugin4 diff --git a/molecule/default/tests/test_role.py b/molecule/default/tests/test_role.py index a0d5a01..adfda5c 100644 --- a/molecule/default/tests/test_role.py +++ b/molecule/default/tests/test_role.py @@ -4,6 +4,7 @@ @pytest.mark.parametrize('username', [ 'test_usr1', 'test_usr2', + 'test_usr5', ]) def test_oh_my_zsh_install(host, username): oh_my_zsh = host.file('/home/' + username + '/.oh-my-zsh') @@ -13,6 +14,17 @@ def test_oh_my_zsh_install(host, username): assert oh_my_zsh.group in [username, 'users'] +@pytest.mark.parametrize('username', [ + 'test_usr3', + 'test_usr4', +]) +def test_oh_my_zsh_is_not_installed_for_excluded_users(host, username): + oh_my_zsh = host.file('/home/' + username + '/.oh-my-zsh') + zshrc = host.file('/home/' + username + '/.zshrc') + assert not oh_my_zsh.exists + assert not zshrc.exists + + @pytest.mark.parametrize('username,theme,plugins', [ ('test_usr1', 'test_theme1', 'test_plugin1 test_plugin2'), ('test_usr2', 'test_theme2', 'test_plugin3 test_plugin4'), diff --git a/tasks/install.yml b/tasks/install.yml index 66ad363..8cdf95a 100644 --- a/tasks/install.yml +++ b/tasks/install.yml @@ -13,37 +13,43 @@ # Git module doesn't allow us to set `core.autocrlf=input`. - skip_ansible_lint become: yes - become_user: '{{ username }}' + become_user: '{{ user.username }}' # core.autocrlf=input prevents https://github.com/robbyrussell/oh-my-zsh/issues/4402 command: 'git clone -c core.autocrlf=input --depth=1 https://github.com/robbyrussell/oh-my-zsh.git .oh-my-zsh' args: - chdir: '~{{ username }}' - creates: '~{{ username }}/.oh-my-zsh' - with_items: "{{ users | map(attribute='username') | list }}" + chdir: '~{{ user.username }}' + creates: '~{{ user.username }}/.oh-my-zsh' + with_items: "{{ users }}" + when: "((user.oh_my_zsh | default({})).install | default(oh_my_zsh_install)) | bool" loop_control: - loop_var: username + loop_var: user + label: '{{ user.username }}' - name: set permissions of oh-my-zsh for users become: yes file: - path: '~{{ username }}/.oh-my-zsh' + path: '~{{ user.username }}/.oh-my-zsh' # Prevent the cloned repository from having insecure permissions. Failing to do # so causes compinit() calls to fail with "command not found: compdef" errors # for users with insecure umasks (e.g., "002", allowing group writability). mode: 'go-w' recurse: yes - with_items: "{{ users | map(attribute='username') | list }}" + with_items: "{{ users }}" + when: "((user.oh_my_zsh | default({})).install | default(oh_my_zsh_install)) | bool" loop_control: - loop_var: username + loop_var: user + label: '{{ user.username }}' - name: set default shell for users become: yes user: - name: '{{ username }}' + name: '{{ user.username }}' shell: /bin/zsh - with_items: "{{ users | map(attribute='username') | list }}" + with_items: "{{ users }}" + when: "((user.oh_my_zsh | default({})).install | default(oh_my_zsh_install)) | bool" loop_control: - loop_var: username + loop_var: user + label: '{{ user.username }}' - name: write .zshrc for users become: yes @@ -54,6 +60,7 @@ backup: yes mode: 'u=rw,go=r' with_items: '{{ users }}' + when: "((user.oh_my_zsh | default({})).install | default(oh_my_zsh_install)) | bool" loop_control: loop_var: user label: '{{ user.username }}'