From 30480f61d51aa23667efca01096d523cdf503452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20D=C3=A9siles?= <1536672+cdesiles@users.noreply.github.com> Date: Fri, 18 Sep 2026 11:32:06 +0200 Subject: [PATCH] generalize zsh role to support darwin --- roles/zsh/defaults/main.yml | 20 +++++++++++++++++-- roles/zsh/tasks/main.yml | 14 +++++++++++-- roles/zsh/tasks/plugins.yml | 22 ++++++++++++++------- roles/zsh/tasks/user-setup.yml | 21 ++++++++++---------- roles/zsh/templates/{aliases => aliases.j2} | 3 +++ roles/zsh/templates/zshrc.j2 | 4 ++-- 6 files changed, 61 insertions(+), 23 deletions(-) rename roles/zsh/templates/{aliases => aliases.j2} (86%) diff --git a/roles/zsh/defaults/main.yml b/roles/zsh/defaults/main.yml index 1e2a731..5bbe4e4 100644 --- a/roles/zsh/defaults/main.yml +++ b/roles/zsh/defaults/main.yml @@ -1,5 +1,12 @@ --- -zsh_home: "{{ '/root' if zsh_user == 'root' else '/home/' + zsh_user }}" +zsh_os_darwin: "{{ ansible_facts['os_family'] == 'Darwin' }}" +zsh_home: >- + {{ (zsh_os_darwin | bool) + | ternary(ansible_env.HOME, + ('/root' if zsh_user == 'root' else '/home/' + zsh_user)) }} +# Primary group of the managed files. macOS has no per-user group (it's `staff`). +zsh_group: >- + {{ (zsh_os_darwin | bool) | ternary('staff', zsh_user) }} zsh_base_config: "{{ zsh_home }}/.zshrc" zsh_config_path: "{{ zsh_home }}/.config/zsh" zsh_config_file: "{{ zsh_config_path }}/.zshrc" @@ -7,4 +14,13 @@ zsh_p10k_theme_config: "{{ zsh_config_path }}/p10k.zsh" zsh_users: - jokester - root -zsh_plugins_path: "/opt/zsh/plugins" +# System-wide on Linux (root:users); per-user on macOS (user:staff). +zsh_plugins_path: >- + {{ (zsh_os_darwin | bool) | ternary(zsh_config_path + '/plugins', '/opt/zsh/plugins') }} +zsh_plugins_owner: >- + {{ (zsh_os_darwin | bool) | ternary(ansible_user, 'root') }} +zsh_plugins_group: >- + {{ (zsh_os_darwin | bool) | ternary('staff', 'users') }} +# fzf shell integrations (key-bindings.zsh / completion.zsh). +zsh_fzf_shell_path: >- + {{ (zsh_os_darwin | bool) | ternary('$(brew --prefix)/opt/fzf/shell', '/usr/share/fzf') }} diff --git a/roles/zsh/tasks/main.yml b/roles/zsh/tasks/main.yml index 2b7fb91..5486e7d 100644 --- a/roles/zsh/tasks/main.yml +++ b/roles/zsh/tasks/main.yml @@ -1,16 +1,26 @@ --- -- name: Install zsh +- name: Install zsh (Linux) ansible.builtin.package: name: zsh state: present + when: not (zsh_os_darwin | bool) -- name: Install eza (better ls) +- name: Install eza (Linux) ansible.builtin.package: name: eza state: present + when: not (zsh_os_darwin | bool) + +- name: Install eza (macOS) + community.general.homebrew: + name: eza + state: present + become: false + when: zsh_os_darwin | bool - name: Install alacritty terminfo ansible.builtin.include_tasks: alacritty-terminfo.yml + when: not (zsh_os_darwin | bool) - name: Install zsh plugins ansible.builtin.include_tasks: plugins.yml diff --git a/roles/zsh/tasks/plugins.yml b/roles/zsh/tasks/plugins.yml index 5a52bed..aae4c7c 100644 --- a/roles/zsh/tasks/plugins.yml +++ b/roles/zsh/tasks/plugins.yml @@ -3,8 +3,8 @@ ansible.builtin.file: path: "{{ zsh_plugins_path }}" state: directory - owner: root - group: users + owner: "{{ zsh_plugins_owner }}" + group: "{{ zsh_plugins_group }}" mode: "0755" - name: Add a readme file to advice from where this comes @@ -14,8 +14,8 @@ # zsh plugins Managed by Ansible mode: "0644" - group: users - owner: root + group: "{{ zsh_plugins_group }}" + owner: "{{ zsh_plugins_owner }}" - name: "Git clone plugins" ansible.builtin.git: @@ -36,16 +36,24 @@ dest: "{{ zsh_plugins_path }}/fzf-tab" version: master -- name: Install fzf package +- name: Install fzf package (Linux) ansible.builtin.package: name: fzf state: present + when: not (zsh_os_darwin | bool) + +- name: Install fzf package (macOS) + community.general.homebrew: + name: fzf + state: present + become: false + when: zsh_os_darwin | bool - name: Assert plugins are available for any user ansible.builtin.file: path: "{{ item }}" - owner: root - group: users + owner: "{{ zsh_plugins_owner }}" + group: "{{ zsh_plugins_group }}" mode: "0755" state: directory loop: diff --git a/roles/zsh/tasks/user-setup.yml b/roles/zsh/tasks/user-setup.yml index 005f341..6285bde 100644 --- a/roles/zsh/tasks/user-setup.yml +++ b/roles/zsh/tasks/user-setup.yml @@ -4,7 +4,7 @@ src: zshenv.j2 dest: "{{ zsh_home }}/.zshenv" owner: "{{ zsh_user }}" - group: "{{ zsh_user }}" + group: "{{ zsh_group }}" mode: "0600" - name: Setup .zprofile (login banner) @@ -12,7 +12,7 @@ src: zprofile.j2 dest: "{{ zsh_home }}/.zprofile" owner: "{{ zsh_user }}" - group: "{{ zsh_user }}" + group: "{{ zsh_group }}" mode: "0600" - name: Setup zsh base config @@ -20,7 +20,7 @@ src: main.zshrc.j2 dest: "{{ zsh_base_config }}" owner: "{{ zsh_user }}" - group: "{{ zsh_user }}" + group: "{{ zsh_group }}" mode: "0600" - name: Setup .config/zsh directory @@ -28,7 +28,7 @@ path: "{{ zsh_config_path }}" state: directory owner: "{{ zsh_user }}" - group: "{{ zsh_user }}" + group: "{{ zsh_group }}" mode: "0700" - name: Configure zsh config @@ -36,15 +36,15 @@ src: zshrc.j2 dest: "{{ zsh_config_file }}" owner: "{{ zsh_user }}" - group: "{{ zsh_user }}" + group: "{{ zsh_group }}" mode: "0600" - name: Copy aliases - ansible.builtin.copy: - src: ./templates/aliases + ansible.builtin.template: + src: aliases.j2 dest: "{{ zsh_config_path }}/aliases" owner: "{{ zsh_user }}" - group: "{{ zsh_user }}" + group: "{{ zsh_group }}" mode: "0600" - name: Configure fzf-tab plugin @@ -56,18 +56,19 @@ zstyle ':fzf-tab:*' group-colors '' zstyle ':fzf-tab:*' preview-contained-categories true owner: "{{ zsh_user }}" - group: "{{ zsh_user }}" + group: "{{ zsh_group }}" mode: "0600" - name: Change default shell to zsh ansible.builtin.user: name: "{{ zsh_user }}" shell: /bin/zsh + when: not (zsh_os_darwin | bool) - name: Configure powerlevel10k theme ansible.builtin.copy: src: "./templates/{{ 'root.p10k.zsh' if zsh_user == 'root' else 'user.p10k.zsh' }}" dest: "{{ zsh_p10k_theme_config }}" owner: "{{ zsh_user }}" - group: "{{ zsh_user }}" + group: "{{ zsh_group }}" mode: "0600" diff --git a/roles/zsh/templates/aliases b/roles/zsh/templates/aliases.j2 similarity index 86% rename from roles/zsh/templates/aliases rename to roles/zsh/templates/aliases.j2 index 936abd4..d901b88 100644 --- a/roles/zsh/templates/aliases +++ b/roles/zsh/templates/aliases.j2 @@ -1,6 +1,8 @@ +# {{ ansible_managed }} alias ls="eza --icons --group-directories-first" alias ll="eza --icons --group-directories-first -l" alias la="eza --icons --group-directories-first -la" +{% if not (zsh_os_darwin | bool) %} alias p="podman" alias pp="podman play" alias pl="podman logs -f" @@ -10,4 +12,5 @@ alias jd="journalctl -xe" alias docker="echo 'use podman instead'" alias docker-compose="echo 'use podman-compose instead'" alias dc="echo 'use podman-compose instead'" +{% endif %} alias w='cd $HOME/workspace' diff --git a/roles/zsh/templates/zshrc.j2 b/roles/zsh/templates/zshrc.j2 index 9795f33..28a37ea 100644 --- a/roles/zsh/templates/zshrc.j2 +++ b/roles/zsh/templates/zshrc.j2 @@ -61,8 +61,8 @@ source $ZSH_PLUGINS/zsh-history-substring-search/zsh-history-substring-search.zs source $ZSH_PLUGINS/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh # Enable fzf integration (Ctrl+R, Ctrl+T, Alt+C) -source /usr/share/fzf/key-bindings.zsh -source /usr/share/fzf/completion.zsh +source {{ zsh_fzf_shell_path }}/key-bindings.zsh +source {{ zsh_fzf_shell_path }}/completion.zsh # Enable fzf-tab for completions (after compinit) source $ZSH_PLUGINS/fzf-tab/fzf-tab.zsh