Skip to content

Complete

Patrick edited this page May 9, 2024 · 1 revision

Full example of a zsh shell.

  • Install the zsh package.
  • Set zsh as default shell for the specified users.
  • Distribute a custom .zshrc for each user.
  • Install autosuggestions, syntax-highlighting and exa.
  • Set history, color and cd settings.

main.yml file in your project folder:

- name: zsh
  hosts: all
  vars:
    zsh_config_backup: false
    zsh_config_overwrite: true

    zsh_users:
      - lorem
      - ipsum

    zsh_users_config:
      - template: "zshrc.j2"
        filename: ".zshrc"

    zsh_dependencies:
      - exa
      - zsh-autosuggestions
      - zsh-syntax-highlighting

    zsh_additional_lines: ""
  roles:
    - bec.shell.zsh
  tasks:
    - ansible.builtin.debug:
        msg: "ZSH was installed."

zshrc.j2 file in your templates folder:

{% if zsh_config_overwrite is true %}
#
# {{ ansible_managed }}
#
{% endif %}

autoload colors && colors

{% raw %}
PROMPT="%{%F{40}%}%n@%m"
PROMPT+="%{$fg[cyan]%} %c %(?:%{$fg[cyan]%}%# :%{$fg[red]%}%# )%{$reset_color%}"
{% endraw %}

source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

alias ls='exa --group-directories-first'
alias ll='exa --group-directories-first -a -l -b -g -F -G'
alias lx='exa --group-directories-first -a -l -b -g -h -H -i -m -S --time-style=long-iso'

HISTFILE="$HOME/.zsh_history"
HISTSIZE=50000
SAVEHIST=50000

setopt INC_APPEND_HISTORY
setopt AUTOCD

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi

{% if zsh_additional_lines is defined and zsh_additional_lines | length %}
#
# Host specific additions
#
{{ zsh_additional_lines }}
{% endif %}
Clone this wiki locally