Автоматизация установки Zsh с Oh-My-Zsh и темой Jovial через Ansible
Полное решение для массовой установки и настройки Zsh на нескольких серверах с учётом всех нюансов и подводных камней.
Структура проекта
ansible-zsh/
├── inventories/
│ └── inventory.ini
├── group_vars/
│ └── zsh_targets.yml
├── playbooks/
│ └── zsh_deploy.yml
└── README.md
1. Inventory: inventories/inventory.ini
# === INVENTORY ===
# Замените примеры на реальные данные ваших серверов
[docker]
n-1-dsw ansible_host=203.0.113.11 ansible_user=myuser ansible_ssh_private_key_file=~/.ssh/id_rsa_dsw1
n-2-dsw ansible_host=203.0.113.12 ansible_user=myuser ansible_ssh_private_key_file=~/.ssh/id_rsa_dsw2
n-3-dsw ansible_host=203.0.113.13 ansible_user=myuser ansible_ssh_private_key_file=~/.ssh/id_rsa_dsw3
[vpn]
srv-1 ansible_host=198.51.100.21 ansible_user=myuser ansible_ssh_private_key_file=~/.ssh/id_rsa_vpn
[zud]
zud-1 ansible_host=192.0.2.41 ansible_user=myuser ansible_ssh_private_key_file=~/.ssh/id_rsa_zud1
zud-2 ansible_host=192.0.2.42 ansible_user=myuser ansible_ssh_private_key_file=~/.ssh/id_rsa_zud2
# Объединяем все группы в одну для удобства
[zsh_targets:children]
docker
vpn
zud
[all:vars]
ansible_python_interpreter=/usr/bin/python3
2. Переменные: group_vars/zsh_targets.yml
---
# === ПЕРЕМЕННЫЕ (ИЗМЕНИТЕ ПОД СЕБЯ) ===
# Пользователь для которого настраивается zsh
zsh_user: myuser
# Домашняя директория (будет определена автоматически, но можно задать явно)
# zsh_home: /home/myuser
# Путь к zsh (определяется автоматически)
# zsh_shell_path: /usr/bin/zsh
# Создавать ли бэкап существующего .zshrc
zsh_backup_config: true
# Версии плагинов (ветки в git)
zsh_omz_version: master
zsh_plugins_version: master
# Редактор по умолчанию
zsh_editor: nano
# Для VS Code раскомментируйте:
# zsh_editor: "code --wait --reuse-window"
3. Playbook: playbooks/zsh_deploy.yml
---
- name: Install and configure Zsh with Oh-My-Zsh and Jovial theme
hosts: zsh_targets
gather_facts: yes
become: yes
vars:
# Домашняя директория пользователя (определяется автоматически)
zsh_home: "{{ ansible_facts['user_dir'] | default('/home/' + zsh_user) }}"
tasks:
# ============================================================================
# ЭТАП 1: ПРОВЕРКИ И ПОДГОТОВКА
# ============================================================================
- name: Ensure user exists
ansible.builtin.user:
name: "{{ zsh_user }}"
state: present
create_home: yes
register: user_info
- name: Get user home directory
ansible.builtin.getent:
database: passwd
key: "{{ zsh_user }}"
register: user_getent
- name: Set zsh_home fact
ansible.builtin.set_fact:
zsh_home: "{{ user_getent.ansible_facts.getent_passwd[zsh_user][4] }}"
- name: Display configuration info
ansible.builtin.debug:
msg:
- "Target user: {{ zsh_user }}"
- "Home directory: {{ zsh_home }}"
- "OS family: {{ ansible_os_family }}"
# ============================================================================
# ЭТАП 2: УСТАНОВКА ПАКЕТОВ
# ============================================================================
- name: Install base packages (Debian/Ubuntu)
ansible.builtin.apt:
name:
- zsh
- git
- curl
- ca-certificates
- autojump
state: present
update_cache: yes
when: ansible_os_family == "Debian"
- name: Install base packages (RedHat/CentOS/Fedora)
ansible.builtin.dnf:
name:
- zsh
- git
- curl
- ca-certificates
- autojump-zsh
state: present
when: ansible_os_family == "RedHat"
- name: Install base packages (Arch Linux)
community.general.pacman:
name:
- zsh
- git
- curl
- ca-certificates
- autojump
state: present
when: ansible_os_family == "Archlinux"
# eza installation
- name: Check if eza is available in repositories (Debian/Ubuntu)
ansible.builtin.shell: apt-cache show eza
register: eza_available
failed_when: false
changed_when: false
when: ansible_os_family == "Debian"
- name: Install eza from package manager (Debian/Ubuntu)
ansible.builtin.apt:
name: eza
state: present
when:
- ansible_os_family == "Debian"
- eza_available.rc == 0
ignore_errors: yes
- name: Install eza (RedHat/Fedora)
ansible.builtin.dnf:
name: eza
state: present
when: ansible_os_family == "RedHat"
ignore_errors: yes
- name: Install eza (Arch Linux)
community.general.pacman:
name: eza
state: present
when: ansible_os_family == "Archlinux"
ignore_errors: yes
# bat installation
- name: Install bat (Debian/Ubuntu)
ansible.builtin.apt:
name: bat
state: present
when: ansible_os_family == "Debian"
ignore_errors: yes
- name: Create symlink for batcat (Debian/Ubuntu)
ansible.builtin.file:
src: /usr/bin/batcat
dest: /usr/local/bin/bat
state: link
when:
- ansible_os_family == "Debian"
ignore_errors: yes
- name: Install bat (RedHat/Fedora)
ansible.builtin.dnf:
name: bat
state: present
when: ansible_os_family == "RedHat"
ignore_errors: yes
- name: Install bat (Arch Linux)
community.general.pacman:
name: bat
state: present
when: ansible_os_family == "Archlinux"
ignore_errors: yes
# ============================================================================
# ЭТАП 3: ОПРЕДЕЛЕНИЕ ПУТИ К ZSH И УСТАНОВКА КАК SHELL ПО УМОЛЧАНИЮ
# ============================================================================
- name: Find zsh binary path
ansible.builtin.command: command -v zsh
register: zsh_path_result
changed_when: false
- name: Set zsh_shell_path fact
ansible.builtin.set_fact:
zsh_shell_path: "{{ zsh_path_result.stdout }}"
- name: Display zsh path
ansible.builtin.debug:
msg: "Zsh binary found at: {{ zsh_shell_path }}"
- name: Set zsh as default shell for user
ansible.builtin.user:
name: "{{ zsh_user }}"
shell: "{{ zsh_shell_path }}"
# ============================================================================
# ЭТАП 4: УСТАНОВКА OH-MY-ZSH
# ============================================================================
- name: Backup existing .zshrc if exists
ansible.builtin.copy:
src: "{{ zsh_home }}/.zshrc"
dest: "{{ zsh_home }}/.zshrc.backup.{{ ansible_date_time.epoch }}"
remote_src: yes
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
mode: "0644"
when: zsh_backup_config | bool
failed_when: false
- name: Check if Oh-My-Zsh is already installed
ansible.builtin.stat:
path: "{{ zsh_home }}/.oh-my-zsh"
register: omz_dir
- name: Create Oh-My-Zsh directory
ansible.builtin.file:
path: "{{ zsh_home }}/.oh-my-zsh"
state: directory
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
mode: "0755"
when: not omz_dir.stat.exists
- name: Clone Oh-My-Zsh repository
ansible.builtin.git:
repo: https://github.com/ohmyzsh/ohmyzsh.git
dest: "{{ zsh_home }}/.oh-my-zsh"
version: "{{ zsh_omz_version }}"
update: yes
force: no
become_user: "{{ zsh_user }}"
register: omz_clone
# ============================================================================
# ЭТАП 5: СОЗДАНИЕ ДИРЕКТОРИЙ ДЛЯ КАСТОМНЫХ ПЛАГИНОВ И ТЕМ
# ============================================================================
- name: Create custom plugins directory
ansible.builtin.file:
path: "{{ zsh_home }}/.oh-my-zsh/custom/plugins"
state: directory
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
mode: "0755"
- name: Create custom themes directory
ansible.builtin.file:
path: "{{ zsh_home }}/.oh-my-zsh/custom/themes"
state: directory
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
mode: "0755"
# ============================================================================
# ЭТАП 6: УСТАНОВКА ПЛАГИНОВ
# ============================================================================
- name: Clone zsh-autosuggestions plugin
ansible.builtin.git:
repo: https://github.com/zsh-users/zsh-autosuggestions.git
dest: "{{ zsh_home }}/.oh-my-zsh/custom/plugins/zsh-autosuggestions"
version: "{{ zsh_plugins_version }}"
update: yes
force: no
become_user: "{{ zsh_user }}"
- name: Clone zsh-syntax-highlighting plugin
ansible.builtin.git:
repo: https://github.com/zsh-users/zsh-syntax-highlighting.git
dest: "{{ zsh_home }}/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting"
version: "{{ zsh_plugins_version }}"
update: yes
force: no
become_user: "{{ zsh_user }}"
- name: Clone zsh-history-enquirer plugin
ansible.builtin.git:
repo: https://github.com/zthxxx/zsh-history-enquirer.git
dest: "{{ zsh_home }}/.oh-my-zsh/custom/plugins/zsh-history-enquirer"
version: "{{ zsh_plugins_version }}"
update: yes
force: no
become_user: "{{ zsh_user }}"
- name: Clone jovial plugin/theme
ansible.builtin.git:
repo: https://github.com/zthxxx/jovial.git
dest: "{{ zsh_home }}/.oh-my-zsh/custom/plugins/jovial"
version: "{{ zsh_plugins_version }}"
update: yes
force: no
become_user: "{{ zsh_user }}"
# ============================================================================
# ЭТАП 7: УСТАНОВКА ТЕМЫ JOVIAL
# ============================================================================
- name: Create symlink for jovial theme
ansible.builtin.file:
src: "{{ zsh_home }}/.oh-my-zsh/custom/plugins/jovial/jovial.zsh-theme"
dest: "{{ zsh_home }}/.oh-my-zsh/custom/themes/jovial.zsh-theme"
state: link
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
force: yes
# ============================================================================
# ЭТАП 8: СОЗДАНИЕ .ZSHRC
# ============================================================================
- name: Install .zshrc configuration
ansible.builtin.copy:
dest: "{{ zsh_home }}/.zshrc"
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
mode: "0644"
content: |
# === GUARD: выполнять только в zsh ===
[ -n "$ZSH_VERSION" ] || return
# === Oh-My-Zsh ===
export ZSH="{{ zsh_home }}/.oh-my-zsh"
ZSH_THEME="jovial"
# === Плагины ===
# ВАЖНО: zsh-syntax-highlighting должен быть последним!
plugins=(
git
autojump
bgnotify
zsh-history-enquirer
zsh-autosuggestions
jovial
zsh-syntax-highlighting
)
source "$ZSH/oh-my-zsh.sh"
# === Настройки Jovial ===
typeset -g JOVIAL_EXEC_THRESHOLD_SECONDS=1
# === Терминал ===
export TERM="xterm-256color"
export COLORTERM="truecolor"
# === Локаль (для корректного отображения emoji) ===
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
# === История команд ===
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt SHARE_HISTORY
setopt HIST_IGNORE_DUPS
setopt HIST_FIND_NO_DUPS
# === Автодополнения (case-insensitive) ===
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Za-z}'
# === Алиасы для eza (с безопасным fallback на ls) ===
if command -v eza >/dev/null 2>&1; then
alias ls='eza --icons=auto --group-directories-first --git'
alias ll='eza -l --icons=auto --group-directories-first --git'
alias la='eza -la --icons=auto --group-directories-first --git'
alias lt='eza --tree --level=2 --icons=auto'
alias lsize='eza -l --sort=size --icons=auto'
else
alias ls='ls --color=auto'
alias ll='ls -l --color=auto'
alias la='ls -la --color=auto'
fi
# === Алиасы для bat/batcat (с fallback на cat) ===
if command -v bat >/dev/null 2>&1; then
alias cat='bat --paging=never --style=plain'
elif command -v batcat >/dev/null 2>&1; then
alias cat='batcat --paging=never --style=plain'
fi
export BAT_PAGER="less -FR"
export BAT_THEME="ansi"
# === Контекстные "эмоции" стрелки Jovial ===
typeset -g JOV_ARROW_DEFAULT='%(?.(◕‿◕).(╥﹏╥%))'
typeset -g JOV_ARROW_KUBE='%(?.(\_(ツ)_/¯).(╥﹏╥%))'
typeset -g JOV_ARROW_CONT='%(?.(⌐■_■).(╥﹏╥%))'
autoload -Uz add-zsh-hook
typeset -g JOV_LAST_CMD_BASE=''
jov_store_last_cmd() {
local line="${2:-$1}"
emulate -L zsh -o extendedglob
line="${line##[[:space:]]##}"
local base="${line%%[[:space:]]*}"
if [[ "$base" == (sudo|doas) ]]; then
local rest="${line#*[[:space:]]}"
base="${rest%%[[:space:]]*}"
fi
JOV_LAST_CMD_BASE="$base"
}
preexec_functions=(${preexec_functions:#jov_store_last_cmd})
preexec_functions+=('jov_store_last_cmd')
jov_apply_context_face() {
local expr="$JOV_ARROW_DEFAULT"
case "$JOV_LAST_CMD_BASE" in
kubectl|k|helm) expr="$JOV_ARROW_KUBE" ;;
docker|docker-compose|podman|nerdctl) expr="$JOV_ARROW_CONT" ;;
*) expr="$JOV_ARROW_DEFAULT" ;;
esac
JOVIAL_SYMBOL[arrow]="$expr"
JOVIAL_SYMBOL[arrow.git-clean]="$expr"
JOVIAL_SYMBOL[arrow.git-dirty]="$expr"
}
precmd_functions=(${precmd_functions:#jov_apply_context_face})
precmd_functions+=('jov_apply_context_face')
# === Редакторы ===
export EDITOR="{{ zsh_editor }}"
export VISUAL="{{ zsh_editor }}"
{% if zsh_editor.startswith('code') %}
export SUDO_EDITOR="{{ zsh_editor }}"
export SYSTEMD_EDITOR="{{ zsh_editor }}"
export KUBE_EDITOR="{{ zsh_editor }}"
{% endif %}
# === Дополнительные алиасы ===
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
# ============================================================================
# ЭТАП 9: ФИНАЛЬНЫЕ НАСТРОЙКИ ПРАВ
# ============================================================================
- name: Ensure ownership on entire .oh-my-zsh directory
ansible.builtin.file:
path: "{{ zsh_home }}/.oh-my-zsh"
state: directory
recurse: yes
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
- name: Ensure ownership on .zshrc
ansible.builtin.file:
path: "{{ zsh_home }}/.zshrc"
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
mode: "0644"
# ============================================================================
# ЭТАП 10: ПРОВЕРКА И ТЕСТИРОВАНИЕ
# ============================================================================
- name: Test zsh configuration
ansible.builtin.shell: |
{{ zsh_shell_path }} -c 'echo $ZSH_VERSION'
register: zsh_test
become_user: "{{ zsh_user }}"
changed_when: false
- name: Display zsh version
ansible.builtin.debug:
msg: "Zsh version: {{ zsh_test.stdout }}"
- name: Verify default shell is set
ansible.builtin.shell: |
getent passwd {{ zsh_user }} | cut -d: -f7
register: shell_check
changed_when: false
- name: Display configured shell
ansible.builtin.debug:
msg:
- "User: {{ zsh_user }}"
- "Default shell: {{ shell_check.stdout }}"
- "Expected: {{ zsh_shell_path }}"
- name: Final summary
ansible.builtin.debug:
msg:
- "✓ Zsh installation completed successfully"
- "✓ Oh-My-Zsh installed at: {{ zsh_home }}/.oh-my-zsh"
- "✓ Configuration file: {{ zsh_home }}/.zshrc"
- "✓ Theme: Jovial"
- "✓ Plugins: git, autojump, bgnotify, zsh-autosuggestions, jovial, zsh-syntax-highlighting, zsh-history-enquirer"
- ""
- "⚠ User must re-login or run 'source {{ zsh_home }}/.zshrc' to apply changes"
4. Запуск плейбука
Базовый запуск
ansible-playbook -i inventories/inventory.ini playbooks/zsh_deploy.yml
Запуск для конкретной группы
ansible-playbook -i inventories/inventory.ini playbooks/zsh_deploy.yml -l docker
Запуск с проверкой (dry-run)
ansible-playbook -i inventories/inventory.ini playbooks/zsh_deploy.yml --check
Запуск с подробным выводом
ansible-playbook -i inventories/inventory.ini playbooks/zsh_deploy.yml -v
Запуск для одного хоста
ansible-playbook -i inventories/inventory.ini playbooks/zsh_deploy.yml -l n-1-dsw
5. Проверка после установки
Подключаемся к серверу
ssh myuser@203.0.113.11
Проверяем shell
echo $SHELL
# Ожидается: /usr/bin/zsh или /bin/zsh
Проверяем тему
echo $ZSH_THEME
# Ожидается: jovial
Проверяем плагины
# Начните вводить команду — должны появиться серые автодополнения
# Введите команду — она должна подсвечиваться
6. Обновление конфигурации
Если нужно обновить только .zshrc без переустановки всего:
ansible-playbook -i inventories/inventory.ini playbooks/zsh_deploy.yml --tags config
Для этого добавьте тег к задаче установки .zshrc:
- name: Install .zshrc configuration
ansible.builtin.copy:
# ... параметры ...
tags:
- config
7. Откат изменений (деинсталляция)
Создайте файл playbooks/zsh_uninstall.yml:
---
- name: Uninstall Zsh and Oh-My-Zsh
hosts: zsh_targets
gather_facts: yes
become: yes
tasks:
- name: Get user home directory
ansible.builtin.getent:
database: passwd
key: "{{ zsh_user }}"
register: user_getent
- name: Set zsh_home fact
ansible.builtin.set_fact:
zsh_home: "{{ user_getent.ansible_facts.getent_passwd[zsh_user][4] }}"
- name: Restore default shell to bash
ansible.builtin.user:
name: "{{ zsh_user }}"
shell: /bin/bash
- name: Remove Oh-My-Zsh directory
ansible.builtin.file:
path: "{{ zsh_home }}/.oh-my-zsh"
state: absent
- name: Remove .zshrc
ansible.builtin.file:
path: "{{ zsh_home }}/.zshrc"
state: absent
- name: Find .zshrc backups
ansible.builtin.find:
paths: "{{ zsh_home }}"
patterns: ".zshrc.backup.*"
register: zshrc_backups
- name: Restore latest .zshrc backup if exists
ansible.builtin.copy:
src: "{{ (zshrc_backups.files | sort(attribute='mtime') | last).path }}"
dest: "{{ zsh_home }}/.zshrc"
remote_src: yes
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
when: zshrc_backups.matched > 0
- name: Remove packages (Debian/Ubuntu)
ansible.builtin.apt:
name:
- zsh
- eza
- bat
- autojump
state: absent
purge: yes
when: ansible_os_family == "Debian"
- name: Remove packages (RedHat/Fedora)
ansible.builtin.dnf:
name:
- zsh
- eza
- bat
- autojump-zsh
state: absent
when: ansible_os_family == "RedHat"
- name: Autoremove unused packages (Debian/Ubuntu)
ansible.builtin.apt:
autoremove: yes
when: ansible_os_family == "Debian"
Запуск деинсталляции:
ansible-playbook -i inventories/inventory.ini playbooks/zsh_uninstall.yml
8. Настройка VS Code Remote-SSH
Для корректной работы с VS Code добавьте в settings.json:
{
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh",
"args": ["-l"]
}
},
"terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.inheritEnv": false
}
9. Типичные проблемы и решения
Проблема: "Permission denied" при клонировании репозиториев
Решение: Проверьте что become_user указан правильно и пользователь существует
- name: Clone repository
ansible.builtin.git:
# ...
become_user: "{{ zsh_user }}" # Важно!
Проблема: Git показывает изменения даже когда их нет
Решение: Используйте force: no чтобы не перезаписывать локальные изменения
- name: Clone Oh-My-Zsh repository
ansible.builtin.git:
# ...
force: no # Не перезаписывать
Проблема: Владельцы файлов некорректные
Решение: Всегда явно указывайте владельца через become_user при создании файлов
- name: Create directory
ansible.builtin.file:
path: "{{ zsh_home }}/.oh-my-zsh"
owner: "{{ zsh_user }}"
group: "{{ zsh_user }}"
10. Преимущества этого решения
✅ Кросс-платформенность — поддержка Debian, Ubuntu, CentOS, Fedora, Arch
✅ Автоматическое определение путей — не нужно хардкодить /usr/bin/zsh
✅ Идемпотентность — можно запускать многократно без проблем
✅ Бэкапы — автоматическое сохранение существующих конфигов
✅ Проверки — тестирование после установки
✅ Безопасность — корректная работа с правами и владельцами
✅ Прозрачность — все параметры в переменных, легко настроить
✅ Откат — простая деинсталляция при необходимости
11. Дополнительные возможности
Добавление собственных алиасов через переменные
В group_vars/zsh_targets.yml:
zsh_custom_aliases:
- { alias: 'update', command: 'sudo apt update && sudo apt upgrade -y' }
- { alias: 'gs', command: 'git status' }
- { alias: 'dps', command: 'docker ps' }
В плейбуке добавьте в .zshrc:
{% for item in zsh_custom_aliases | default([]) %}
alias {{ item.alias }}='{{ item.command }}'
{% endfor %}
Установка специфичных настроек для групп
Создайте group_vars/docker.yml:
zsh_custom_plugins:
- docker
- docker-compose
И подключайте их условно в плейбуке.
Заключение
Это полное, production-ready решение для автоматизации установки Zsh с Oh-My-Zsh и темой Jovial. Оно учитывает все подводные камни, которые были в оригинальной версии, и добавляет множество улучшений для стабильной работы в различных окружениях.