From 045447757f6f18bf366b11aa9aa62dd34d92ac7e Mon Sep 17 00:00:00 2001 From: uzurka Date: Thu, 9 Jan 2025 16:02:22 +0100 Subject: [PATCH] first commit --- .gitignore | 4 + LICENSE | 21 ++++++ README.md | 127 ++++++++++++++++++++++++++++++++ defaults/main.yml | 34 +++++++++ meta/main.yml | 25 +++++++ tasks/main.yml | 34 +++++++++ tasks/oh-my-zsh-install.yml | 21 ++++++ tasks/oh-my-zsh-zshrc.yml | 14 ++++ tasks/zsh-zshrc.yml | 14 ++++ tasks/zsh.yml | 32 ++++++++ templates/zshrc.zsh-template.j2 | 89 ++++++++++++++++++++++ 11 files changed, 415 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 defaults/main.yml create mode 100644 meta/main.yml create mode 100644 tasks/main.yml create mode 100644 tasks/oh-my-zsh-install.yml create mode 100644 tasks/oh-my-zsh-zshrc.yml create mode 100644 tasks/zsh-zshrc.yml create mode 100644 tasks/zsh.yml create mode 100644 templates/zshrc.zsh-template.j2 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..75da035 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.venv +pytestdebug.log +__pycache__ +*.pyc diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1edf080 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Christopher Torgalson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e6efc6d --- /dev/null +++ b/README.md @@ -0,0 +1,127 @@ +# Ansible Role Oh My ZSH + +This is a basic Ansible role to enable and configure Oh My Zsh on Fedora, +Ubuntu, or MacOS. It should also work on many other \*nix variants. It +performs the following tasks: + +- Install and minimally configure Zsh: + - make sure it exists, + - set it as the default shell for the user specified by the role. +- Install Oh My Zsh for each specified user (in `~/.oh-my-zsh` by default). +- Configure (Oh My) Zsh by optionally creating a `.zshrc` file for each + specified user. +- Alternately, configure Zsh by adding a block of lines to individual + users' `.zshrc` files. + +## Role variables + +| Variable name | Default value | Description | +|----------------|---------------|-------------| +| `omz_install_zsh` | `false` | Defines whether or not the role should attempt to install Zsh. | +| `omz_user` | `[]` | The user to install/configure (Oh My) Zsh for. See below for its properties. | +| `omz_user.name` | `-` | The name of the user. | +| `omz_user.group` | `-` | The group of the user | +| `omz_user.settings` | `-` | Extra settings (as a mult-line string) such as variable exports or aliases to add to the user's `.zshrc` file. Only used if `omz_zshrc_create` is `true`. | +| `omz_git_repository` | `https://github.com/robbyrussell/oh-my-zsh.git` | The git repo to clone Oh My Zsh from. | +| `omz_install_directory` | `.oh-my-zsh` | The name of the directory to clone Oh My Zsh into. | +| `omz_zshrc_create` | `true` | Whether or not to create `.zshrc`. If `true`, will create `.zshrc` from a template. | +| `omz_zshrc_template` | `templates/zshrc.zsh-template.j2` | The template used to create the user's `.zshrc` file when `omz_zshrc_create` is `true`. | +| `omz_zshrc_backup` | `true` | Whether or not to create backup the existing `.zshrc` files when the role changes it. | +| `omz_zsh_theme` | `robbyrussell` | See `templates/zshrc.zsh-template`. | +| `omz_case_sensitive` | `false` | See `templates/zshrc.zsh-template`. | +| `omz_hyphen_insensitive` | `false` | See `templates/zshrc.zsh-template`. | +| `omz_disable_auto_update` | `false` | See `templates/zshrc.zsh-template`. | +| `omz_update_zsh_days` | `13` | See `templates/zshrc.zsh-template`. | +| `omz_disable_ls_colors` | `false` | See `templates/zshrc.zsh-template`. | +| `omz_disable_auto_title` | `false` | See `templates/zshrc.zsh-template`. | +| `omz_enable_correction` | `false` | See `templates/zshrc.zsh-template`. | +| `omz_completion_waiting_dots` | `false` | See `templates/zshrc.zsh-template`. | +| `omz_disable_untracked_files_dirty` | `false` | See `templates/zshrc.zsh-template`. | +| `omz_hist_stamps` | `mm/dd/yyyy` | See `templates/zshrc.zsh-template`. | +| `omz_zsh_custom` | `$ZSH/custom` | See `templates/zshrc.zsh-template`. | +| `omz_plugins` | `[]` | A list of Oh My Zsh plugins to enable. | + +## Role task files + +### `main.yml`: task coordination + +This file includes files that peform specific subsets of tasks. + +### `zsh.yml`: Zsh setup + +This task installs and sets zsh as the default shell for a user. + +#### Variables used + +- `omz_user` + +### `oh-my-zsh-install.yml`: Oh My Zsh installation + +This task clones the Oh My Zsh repository into the user directory of each +specified user and sets the appropriate permissions on the directory. + +#### Variables used + +- `omz_user` +- `omz_install_directory` +- `omz_git_repository` +- `omz_install_path` + +### `oh-my-zsh-zshrc.yml`: Oh My Zsh configuration + +This task creates the user a `.zshrc` file containing global values for various +Oh My Zsh options based on [the `.zshrc` template in the oh-my-zsh repository](https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/templates/zshrc.zsh-template). +The task can be configured to back up any existing `.zshrc` file. + +This task only runs when `omz_zshrc_create` is set to `true`. + +#### Variables used + +- `omz_user` +- `omz_zshrc_template` +- `omz_zshrc_backup` + +### `zsh-zshrc.yml`: final Zsh configuration + +This task adds individual lines to the `.zshrc` file. This is useful for adding +Zsh settings on an already-existing `.zshrc` file without creating it +from scratch. + +This task only runs when `omz_zshrc_create` is set to `false`. + +#### Variables used + +- `omz_user` +- `omz_zshrc_backup` + +## Sample playbook + + --- + - name: Playbook + hosts: all + become: true + + vars: + omz_install_zsh: true + users: + - name: "lorem" + group: "lorem" + settings: "" + - name: "ipsum" + group: "ipsum" + settings: | + export PATH="/usr/local/sbin:$path" + alias l="ls -AF" + tasks: + - name: Run ansible-role-oh-my-zsh. + include_role: + name: "ansible-role-oh-my-zsh" + vars: + omz_user: "{{ item }}" + # Only create `.zshrc` for user 'lorem'; item.settings will be + # appended to `.zshrc` for the user 'ipsum'. + omz_zshrc_create: "{{ (item.name == 'lorem') | ternary(true, false) }}" + omz_plugins: + - "autojump" + - "git" + with_items: "{{ users }}" diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..fad5118 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,34 @@ +--- +# Role vars. +omz_install_zsh: false + +# User vars. +omz_user: [] +# - name: "someuser" +# group: "somegroup" +# settings: | +# export PATH="/usr/local/sbin:$path" +# alias l="ls -AF" + +# Oh My ZSH vars. +omz_git_repository: "https://github.com/robbyrussell/oh-my-zsh.git" +omz_install_directory: ".oh-my-zsh" + +# Oh My ZSH template vars. +omz_zshrc_create: true +omz_zshrc_template: "templates/zshrc.zsh-template.j2" +omz_zshrc_backup: true +omz_zshrc_force: true +omz_zsh_theme: "robbyrussell" +omz_case_sensitive: false +omz_hyphen_insensitive: false +omz_disable_auto_update: false +omz_update_zsh_days: 13 +omz_disable_ls_colors: false +omz_disable_auto_title: false +omz_enable_correction: false +omz_completion_waiting_dots: false +omz_disable_untracked_files_dirty: false +omz_hist_stamps: "mm/dd/yyyy" +omz_zsh_custom: "$ZSH/custom" +omz_plugins: [] diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..c3bab43 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,25 @@ +--- +galaxy_info: + author: "ctorgalson" + description: "Installs and configures oh-my-zsh for one or more users on Fedora, macOS, or Ubuntu." + + license: "MIT" + + min_ansible_version: "2.0" + + platforms: + - name: "MacOSX" + versions: + - "all" + - name: "Ubuntu" + versions: + - "all" + - name: "Fedora" + versions: + - "all" + + galaxy_tags: + - "zsh" + - "ohmyzsh" + +dependencies: [] diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..31dc64f --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,34 @@ +--- +# Make sure zsh is installed and set to the user's default shell. +- name: "OMZ | include zsh.yml tasks." + include_tasks: "zsh.yml" + tags: + - "zsh" + - "configure" + - "configurezsh" + +# Install oh-my-zsh. +- name: "OMZ | include oh-my-zsh.yml tasks." + include_tasks: oh-my-zsh-install.yml + tags: + - "oh-my-zsh" + - "install" + - "installohmyzsh" + +# Configure oh-my-zsh with a custom .zshrc template if omz_zshrc_create +# is set to 'true'. +- name: "OMZ | include oh-my-zsh-zshrc.yml tasks." + include_tasks: oh-my-zsh-zshrc.yml + tags: + - "oh-my-zsh" + - "configure" + - "configureohmyzsh" + +# Finally, add exports etc to .zshrc /last/ (i.e. so they get added to whaterver +# .zshrc exists. +- name: "OMZ | include zsh-zshrc.yml tasks." + include_tasks: zsh-zshrc.yml + tags: + - "zsh" + - "configure" + - "configurezsh" diff --git a/tasks/oh-my-zsh-install.yml b/tasks/oh-my-zsh-install.yml new file mode 100644 index 0000000..9e24c60 --- /dev/null +++ b/tasks/oh-my-zsh-install.yml @@ -0,0 +1,21 @@ +--- +- name: "OMZ | establish install location." + set_fact: + omz_install_path: "/{{ omz_user_home_dir }}/{{ omz_user.name }}/{{ omz_install_directory }}" + +- name: "OMZ | clone Oh My ZSH repo for user." + git: + repo: "{{ omz_git_repository }}" + dest: "{{ omz_install_path }}" + update: "true" + accept_hostkey: "true" + version: "master" + register: "omz_clone" + +- name: "OMZ | set ownership on newly cloned repository." + file: + path: "{{ omz_install_path }}" + owner: "{{ omz_user.name }}" + group: "{{ omz_user.group }}" + recurse: "true" + when: "omz_clone is changed" diff --git a/tasks/oh-my-zsh-zshrc.yml b/tasks/oh-my-zsh-zshrc.yml new file mode 100644 index 0000000..d6430aa --- /dev/null +++ b/tasks/oh-my-zsh-zshrc.yml @@ -0,0 +1,14 @@ +--- +- name: "OMZ | derive user .zshrc path." + set_fact: + omz_user_zshrc_path: "/{{ omz_user_home_dir }}/{{ omz_user.name }}/.zshrc" + +- name: "OMZ | template .zshrc into place if required." + template: + src: "{{ omz_zshrc_template }}" + dest: "{{ omz_user_zshrc_path }}" + owner: "{{ omz_user.name }}" + group: "{{ omz_user.group }}" + backup: "{{ omz_zshrc_backup }}" + force: "{{ omz_zshrc_force }}" + when: "omz_zshrc_create" diff --git a/tasks/zsh-zshrc.yml b/tasks/zsh-zshrc.yml new file mode 100644 index 0000000..45f4f06 --- /dev/null +++ b/tasks/zsh-zshrc.yml @@ -0,0 +1,14 @@ +--- +# Note that we assume this file exists! lineinfile will fail if the file is +# not present. +- name: "OMZ | export vars to .zshrc if required." + blockinfile: + dest: "{{ omz_user_zshrc_path }}" + block: "{{ omz_user.settings }}" + backup: "{{ omz_zshrc_backup }}" + when: + - "omz_user.settings is defined" + # Don't flag this line for checking if the value is empty--checking for an + # empty value makes perfect sense. + - "omz_user.settings != ''" # noqa 602 + - "not omz_zshrc_create" diff --git a/tasks/zsh.yml b/tasks/zsh.yml new file mode 100644 index 0000000..8b0bb72 --- /dev/null +++ b/tasks/zsh.yml @@ -0,0 +1,32 @@ +--- +- name: "OMZ | establish home directory." + set_fact: + omz_user_home_dir: "{{ (ansible_system == 'Darwin') | ternary('Users', 'home') }}" + +- name: "OMZ | ensure zsh is installed." + block: + - name: "OMZ | install zsh for Linux." + package: + name: "zsh" + state: "present" + when: + - "ansible_system == 'Linux'" + - "omz_install_zsh" + + - name: "OMZ | install zsh for macOS." + homebrew: + name: "zsh" + state: "present" + when: + - "ansible_system == 'Darwin'" + - "omz_install_zsh" + +- name: "OMZ | get zsh installed path." + shell: "command -v zsh" + register: omz_zsh_installed_path + changed_when: "false" + +- name: "OMZ | get user shell to zsh." + user: + name: "{{ omz_user.name }}" + shell: "{{ omz_zsh_installed_path.stdout }}" diff --git a/templates/zshrc.zsh-template.j2 b/templates/zshrc.zsh-template.j2 new file mode 100644 index 0000000..d52bf09 --- /dev/null +++ b/templates/zshrc.zsh-template.j2 @@ -0,0 +1,89 @@ +# Managed by Ansible. This file may be overwritten when playbooks are run! + +# If you come from bash you might have to change your $PATH. +# export PATH=$HOME/bin:/usr/local/bin:$PATH + +# Path to your oh-my-zsh installation. +export ZSH=/{{ omz_user_home_dir }}/{{ omz_user.name }}/{{ omz_install_directory }} + +# Set name of the theme to load. Optionally, if you set this to "random" +# it'll load a random theme each time that oh-my-zsh is loaded. +# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes +ZSH_THEME="{{ omz_zsh_theme }}" + +# Uncomment the following line to use case-sensitive completion. +CASE_SENSITIVE="{{ omz_case_sensitive }}" + +# Uncomment the following line to use hyphen-insensitive completion. Case +# sensitive completion must be off. _ and - will be interchangeable. +HYPHEN_INSENSITIVE="{{ omz_hyphen_insensitive }}" + +# Uncomment the following line to disable bi-weekly auto-update checks. +DISABLE_AUTO_UPDATE="{{ omz_disable_auto_update }}" + +# Uncomment the following line to change how often to auto-update (in days). +export UPDATE_ZSH_DAYS={{ omz_update_zsh_days }} + +# Uncomment the following line to disable colors in ls. +DISABLE_LS_COLORS="{{ omz_disable_ls_colors }}" + +# Uncomment the following line to disable auto-setting terminal title. +DISABLE_AUTO_TITLE="{{ omz_disable_auto_title }}" + +# Uncomment the following line to enable command auto-correction. +ENABLE_CORRECTION="{{ omz_enable_correction }}" + +# Uncomment the following line to display red dots whilst waiting for completion. +COMPLETION_WAITING_DOTS="{{ omz_completion_waiting_dots }}" + +# Uncomment the following line if you want to disable marking untracked files +# under VCS as dirty. This makes repository status check for large repositories +# much, much faster. +DISABLE_UNTRACKED_FILES_DIRTY="{{ omz_disable_untracked_files_dirty }}" + +# Uncomment the following line if you want to change the command execution time +# stamp shown in the history command output. +# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd" +HIST_STAMPS="{{ omz_hist_stamps }}" + +# Would you like to use another custom folder than $ZSH/custom? +ZSH_CUSTOM={{ omz_zsh_custom }} + +# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*) +# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/ +# Example format: plugins=(rails git textmate ruby lighthouse) +# Add wisely, as too many plugins slow down shell startup. +plugins=({{ omz_plugins | join(" ") }}) + +source $ZSH/oh-my-zsh.sh + +# User configuration + +# export MANPATH="/usr/local/man:$MANPATH" + +# You may need to manually set your language environment +# export LANG=en_US.UTF-8 + +# Preferred editor for local and remote sessions +# if [[ -n $SSH_CONNECTION ]]; then +# export EDITOR='vim' +# else +# export EDITOR='mvim' +# fi + +# Compilation flags +# export ARCHFLAGS="-arch x86_64" + +# ssh +# export SSH_KEY_PATH="~/.ssh/dsa_id" + +# Set personal aliases, overriding those provided by oh-my-zsh libs, +# plugins, and themes. Aliases can be placed here, though oh-my-zsh +# users are encouraged to define aliases within the ZSH_CUSTOM folder. +# For a full list of active aliases, run `alias`. +# +# Example aliases +# alias zshconfig="mate ~/.zshrc" +# alias ohmyzsh="mate ~/.oh-my-zsh" + +{{ omz_user.settings }}