Skip to content

Commit

Permalink
Added support for conditional install per user (#111)
Browse files Browse the repository at this point in the history
Useful for users specified at the playbook level instead of the role level.

Co-authored-by: nekeal <[email protected]>
  • Loading branch information
freemanjp and nekeal committed Sep 13, 2020
1 parent 062fa7a commit f5fee90
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -77,6 +81,9 @@ users:
plugins:
- git
- mvn
- username: example3
oh_my_zsh:
install: no
```
Example Playbook
Expand Down
4 changes: 4 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
with_items:
- test_usr1
- test_usr2
- test_usr3
- test_usr4
- test_usr5

- name: install console-setup file
become: yes
Expand All @@ -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
12 changes: 12 additions & 0 deletions molecule/default/tests/test_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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'),
Expand Down
29 changes: 18 additions & 11 deletions tasks/install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}'

0 comments on commit f5fee90

Please sign in to comment.