patbec.zsh

zsh 控制台图标

Ansible 角色: ZSH

Ansible 角色 Ansible 质量评分 GitHub

已废弃: 此角色已迁移到集合, 这里可以找到新版本

这是一个简单的角色,用于在 Linux 上安装和设置 zsh-shell

支持以下步骤:

  • 使用自定义软件包安装 zsh
  • 为指定用户将 zsh 设置为默认 shell
  • 可选的配置文件分发

该角色保持简单,使用标准的软件包管理器,并且开销最小。如果遇到任何问题,请随时创建一个问题。

准备

使用 Ansible Galaxy 安装此角色。

ansible-galaxy install patbec.zsh

变量

此剧本中的默认变量。

名称 描述 默认值
zsh_config_mode 分发文件的默认权限。 0644
zsh_config_backup 覆盖之前创建备份。 true
zsh_config_overwrite 如果存在差异,则覆盖现有文件。 false
zsh_users_config 要分发给用户的 zsh 文件列表。 []
zsh_system_config 要全局分发的 zsh 文件列表。 []
zsh_users 应该将 zsh 设置为默认 shell 的用户列表。 {{ ansible_user }}
zsh_dependencies 额外要安装的软件包列表。 []
zsh_executable 可执行文件的路径。 /usr/bin/zsh

Molecule: 如果为 zsh_executable 指定了符号链接,则单元测试将失败。

zsh_users_config 属性是一个字典:

zsh_users_config:
  - template: "zshrc.j2"
    filepath: "$HOME/.zshrc"

这个基本示例分发了一个用户的 .zshrc。Ansible 在你的 template 文件夹中查找 zshrc.j2 文件。

路径

以下是可能的路径列表来自 zsh 文档 (5.2 文件)

用户

zsh_users_config:
  - template: "zshrc.j2"
    filepath: "$HOME/.zshrc"
  - template: "zshenv.j2"
    filepath: "$HOME/.zshenv"
  - template: "zprofile.j2"
    filepath: "$HOME/.zprofile"
  - template: "zlogin.j2"
    filepath: "$HOME/.zlogin"
  - template: "zlogout.j2"
    filepath: "$HOME/.zlogout"

复制操作是在各自用户的上下文中执行的。

系统

zsh_system_config:
  - template: "zshrc.j2"
    filepath: "/etc/zshrc"
  - template: "zshenv.j2"
    filepath: "/etc/zshenv"
  - template: "zprofile.j2"
    filepath: "/etc/zprofile"
  - template: "zlogin.j2"
    filepath: "/etc/zlogin"
  - template: "zlogout.j2"
    filepath: "/etc/zlogout"

示例

以下是该角色使用的一些示例。

  1. 基本示例
    安装 zsh 并将其设置为 ansible 用户的默认 shell。
  2. 多个用户
    安装 zsh 并将其设置为指定用户列表的默认 shell。
  3. 用户自定义配置文件
    为用户列表分发自定义 .zshrc 文件。
  4. 自定义依赖项
    分发带有依赖项的自定义 .zshrc 文件。
  5. 额外功能
    分发带有 autosuggestionssyntax-highlighting 功能的自定义 .zshrc 文件。
  6. 完整示例
    zsh shell 的完整示例。示例预览

基本示例

安装 zsh 并将其设置为 ansible_user 的默认 shell。

  • 安装 zsh 软件包。
  • ansible_user 设置 zsh 为默认 shell。
- name: zsh
  hosts: all
  roles:
    - patbec.zsh
  tasks:
    ansible.builtin.debug:
        msg: "ZSH 已安装并设置为用户 {{ ansible_user }} 的默认 shell。"

多个用户

安装 zsh 并将其设置为指定用户列表的默认 shell。

  • 安装 zsh 软件包。
  • 为指定用户设置 zsh 为默认 shell。
- name: zsh
  hosts: all
  vars:
    zsh_users:
      - lorem
      - ipsum
  roles:
    - patbec.zsh
  tasks:
    ansible.builtin.debug:
        msg: "ZSH 已安装并设置为 2 个用户的默认 shell。"

用户自定义配置文件

为用户列表分发自定义 .zshrc 文件。

  • 安装 zsh 软件包。
  • 为指定用户设置 zsh 为默认 shell。
  • 为每个用户分发自定义 .zshrc
- name: zsh
  hosts: all
  vars:
    zsh_config_backup: false
    zsh_config_overwrite: true

    zsh_users:
      - lorem
      - ipsum

    zsh_users_config:
      - template: "zshrc.j2"
        filepath: "$HOME/.zshrc"

  roles:
    - patbec.zsh
  tasks:
    ansible.builtin.debug:
        msg: "ZSH 已安装并分发了自定义文件。"

zshrc.j2 文件在你的 templates 文件夹中:

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

# 在此添加你的内容。

自定义依赖项

分发带有依赖项的自定义 .zshrc 文件。

  • 安装 zsh 软件包。
  • 为指定用户设置 zsh 为默认 shell。
  • 为每个用户分发自定义 .zshrc
  • 安装一个依赖项。
- name: zsh
  hosts: all
  vars:
    zsh_config_backup: false
    zsh_config_overwrite: true

    zsh_users:
      - lorem
      - ipsum

    zsh_users_config:
      - template: "zshrc.j2"
        filepath: "$HOME/.zshrc"

    zsh_dependencies:
      - exa
  roles:
    - patbec.zsh
  tasks:
    ansible.builtin.debug:
        msg: "ZSH 已安装,exa 用于列出文件。"

zshrc.j2 文件在你的 templates 文件夹中:

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

alias ls='exa --group-directories-first'
alias ll='exa --group-directories-first --all --long --binary --group --classify --grid'
alias la='exa --group-directories-first --all --long --binary --group --header --links --inode --modified --blocks --time-style=long-iso --color-scale'
alias lx='exa --group-directories-first --all --long --binary --group --header --links --inode --modified --blocks --time-style=long-iso --color-scale --extended'

exa 是一个改善的文件列出器,具有更多功能和更好的默认值。它使用颜色来区分文件类型和元数据。

额外功能

分发带有 自动建议语法高亮 功能的自定义 .zshrc 文件。

  • 安装 zsh 软件包。
  • 为指定用户设置 zsh 为默认 shell。
  • 为每个用户分发自定义 .zshrc
  • 安装 自动建议语法高亮
- name: zsh
  hosts: all
  vars:
    zsh_config_backup: false
    zsh_config_overwrite: true

    zsh_users:
      - lorem
      - ipsum

    zsh_users_config:
      - template: "zshrc.j2"
        filepath: "$HOME/.zshrc"

    zsh_dependencies:
      - zsh-autosuggestions
      - zsh-syntax-highlighting
  roles:
    - patbec.zsh
  tasks:
    ansible.builtin.debug:
        msg: "ZSH 已安装并启用了额外功能。"

zshrc.j2 文件在你的 templates 文件夹中:

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

autoload colors && colors

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

完整示例

zsh shell 的完整示例。

  • 安装 zsh 软件包。
  • 为指定用户设置 zsh 为默认 shell。
  • 为每个用户分发自定义 .zshrc
  • 安装 自动建议语法高亮exa
  • 设置 historycolorcd 设置。
- name: zsh
  hosts: all
  vars:
    zsh_config_backup: false
    zsh_config_overwrite: true

    zsh_users:
      - lorem
      - ipsum

    zsh_users_config:
      - template: "zshrc.j2"
        filepath: "$HOME/.zshrc"

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

    zsh_additional_lines: ""
  roles:
    - patbec.zsh
  tasks:
    ansible.builtin.debug:
        msg: "ZSH 已安装。"

zshrc.j2 文件在你的 templates 文件夹中:

点击这里查看内容
{% if zsh_config_overwrite is true %}
#
# {{ ansible_managed }}
#
{% endif %}

autoload colors && colors

PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT+=" %{$fg[cyan]%}%c%{$reset_color%} "

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 --all --long --binary --group --classify --grid'
alias la='exa --group-directories-first --all --long --binary --group --header --links --inode --modified --blocks --time-style=long-iso --color-scale'
alias lx='exa --group-directories-first --all --long --binary --group --header --links --inode --modified --blocks --time-style=long-iso --color-scale --extended'

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

setopt INC_APPEND_HISTORY
setopt AUTOCD

{% if zsh_additional_lines is defined %}
#
# 主机特定的附加内容
#
{{ zsh_additional_lines }}
{% endif %}

预览

此配置的预览: zsh 控制台 你可以在这里找到使用的 ANSI 控制台颜色

许可证

该项目以 MIT 许可发布 - 详细信息请参见 LICENSE 文件。


回到顶部

关于项目

Installs and configures zsh for one or more users on Linux.

安装
ansible-galaxy install patbec.zsh
许可证
mit
下载
116
拥有者