From af65d16eb097522fc46f0744ca6f57f86794ea50 Mon Sep 17 00:00:00 2001 From: uzurka Date: Thu, 9 Jan 2025 19:57:47 +0100 Subject: [PATCH] WIP: Harmonizing the 2 roles i use into 1 big working --- defaults/main.yml | 10 ++---- tasks/oh-my-zsh-install.yml | 19 ++++-------- tasks/oh-my-zsh-zshrc.yml | 7 +++-- tasks/p10k-configure.yml | 10 +++--- tasks/p10k-install.yml | 55 +++++++++++++------------------- vars/main.yml | 62 +++++++------------------------------ 6 files changed, 50 insertions(+), 113 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 96102c8..b029945 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -3,7 +3,8 @@ omz_install_zsh: false # User vars. -omz_user: [] +zsh_users: + - "{{ ansible_user_id }}" # Oh My ZSH vars. omz_git_repository: "https://github.com/robbyrussell/oh-my-zsh.git" @@ -33,14 +34,9 @@ omz_plugins: [] # Powerlevel10k Git repository url p10k_repository_url: 'https://github.com/romkatv/powerlevel10k.git' -# Install p10k for the following linux users -# Default: the linux user running Ansible -p10k_users: - - "{{ ansible_user_id }}" - # Zsh plugin used, zsh, ohmyzsh, prezto, Zim, etc.. # All plugin names can be found here https://github.com/romkatv/powerlevel10k#installation -zsh_plugin: zsh +zsh_plugin: ohmyzsh # Setup p10k theme to use # Valid values: lean, classic, rainbow, pure diff --git a/tasks/oh-my-zsh-install.yml b/tasks/oh-my-zsh-install.yml index 2d18525..2a83f98 100644 --- a/tasks/oh-my-zsh-install.yml +++ b/tasks/oh-my-zsh-install.yml @@ -1,26 +1,19 @@ --- -- name: "OMZ | establish install location." - set_fact: - omz_install_path: > - {% if omz_user.name == 'root' %} - /root/.{{ omz_install_directory }} - {% else %} - /home/{{ omz_user.name }}/{{ omz_install_directory }} - {% endif %} - - name: "OMZ | clone Oh My ZSH repo for user." git: repo: "{{ omz_git_repository }}" - dest: "{{ omz_install_path }}" + dest: "{{ item.ansible_facts.user_home }}/{{ omz_install_directory }}" update: "true" accept_hostkey: "true" version: "master" register: "omz_clone" + with_items: "{{ paths.results }}" - name: "OMZ | set ownership on newly cloned repository." file: - path: "{{ omz_install_path }}" - owner: "{{ omz_user.name }}" - group: "{{ omz_user.group }}" + path: "{{ item.ansible_facts.user_home }}/{{ omz_install_directory }}" + owner: "{{ item.item }}" + group: "{{ item.item }}" recurse: "true" when: "omz_clone is changed" + with_items: "{{ paths.results }}" diff --git a/tasks/oh-my-zsh-zshrc.yml b/tasks/oh-my-zsh-zshrc.yml index a8f0e39..ab1b186 100644 --- a/tasks/oh-my-zsh-zshrc.yml +++ b/tasks/oh-my-zsh-zshrc.yml @@ -6,9 +6,10 @@ - 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 }}" + dest: "{{ item.ansible_facts.user_home }}/.zshrc" + owner: "{{ item.item }}" + group: "{{ item.item }}" backup: "{{ omz_zshrc_backup }}" force: "{{ omz_zshrc_force }}" when: "omz_zshrc_create" + with_items: "{{ paths.results }}" diff --git a/tasks/p10k-configure.yml b/tasks/p10k-configure.yml index 7be951c..e18e652 100644 --- a/tasks/p10k-configure.yml +++ b/tasks/p10k-configure.yml @@ -4,13 +4,13 @@ name: "{{ item }}" shell: "{{ zsh_bin_path }}" become: true - loop: "{{ p10k_users }}" + loop: "{{ zsh_users }}" - name: Setup powerlevel10k ansible.builtin.template: src: "p10k-{{ p10k_style[p10k_prompt_style] }}.zsh.j2" - dest: "{{ item['home'] }}/.p10k.zsh" - owner: "{{ item['name'] }}" - group: "{{ item['group'] }}" + dest: "{{ item.ansible_facts.user_home }}/.p10k.zsh" + owner: "{{ item.item }}" + group: "{{ item.item }}" mode: '0644' - loop: "{{ p10k_users_information }}" + with_items: "{{ paths.results }}" diff --git a/tasks/p10k-install.yml b/tasks/p10k-install.yml index 7009c1d..0707dcb 100644 --- a/tasks/p10k-install.yml +++ b/tasks/p10k-install.yml @@ -20,52 +20,39 @@ msg: "You are using ZSH version {{ zsh_version.stdout }}. The minimum required version for Powerlevel10k is 5.1" when: zsh_version.stdout|float < 5.1 -- name: Get users information - ansible.builtin.user: - name: "{{ item }}" - state: present - register: p10k_users_register - loop: "{{ p10k_users }}" - -- name: Extract only 'name', 'home' and 'group' fields from users information - ansible.builtin.set_fact: - p10k_users_information: "{{ p10k_users_information | default([]) + [{'name': item['name'], 'home': item['home'], 'group': item['group']}] }}" - no_log: true - loop: "{{ p10k_users_register.results }}" - - name: Install powerlevel10k. ansible.builtin.git: repo: "{{ p10k_repository_url }}" - dest: "{{ item['home'] }}/{{ p10k_path[zsh_plugin] | default('~/powerlevel10k', True) }}" + dest: "{{ item.ansible_facts.user_home }}/{{ p10k_path }}" depth: '1' update: no version: 'master' - loop: "{{ p10k_users_information }}" + with_items: "{{ paths.results }}" -- name: Setup powerlevel10k file permissions. +- name: Setup powerlevel10k file permissions ansible.builtin.file: - path: "{{ item['home'] }}/{{ p10k_path[zsh_plugin] | default('~/powerlevel10k', True) }}" - owner: "{{ item['name'] }}" - group: "{{ item['group'] }}" + path: "{{ item.ansible_facts.user_home }}/{{ p10k_path }}" + owner: "{{ item.item }}" + group: "{{ item.item }}" recurse: yes - loop: "{{ p10k_users_information }}" + with_items: "{{ paths.results }}" - name: Add powerlevel10k to zsh plugin ansible.builtin.lineinfile: - path: "{{ item['home'] }}/{{ p10k_zshrc_config[zsh_plugin]['zsh_file'] }}" - regexp: "{{ p10k_zshrc_config[zsh_plugin]['regexp'] }}" - line: "{{ p10k_zshrc_config[zsh_plugin]['line'] }}" - owner: "{{ item['name'] }}" - group: "{{ item['group'] }}" + path: "{{ item.ansible_facts.user_home }}/{{ p10k_path }}" + regexp: ".*powerlevel10k.*" + line: "source ~/{{ p10k_path }}/powerlevel10k.zsh-theme" + owner: "{{ item.item }}" + group: "{{ item.item }}" mode: '0644' create: yes - loop: "{{ p10k_users_information }}" + with_items: "{{ paths.results }}" - name: Enable powerlevel10 instant prompt ansible.builtin.blockinfile: - path: "{{ item['home'] }}/{{ p10k_zshrc_config[zsh_plugin]['zsh_file'] }}" - owner: "{{ item['name'] }}" - group: "{{ item['group'] }}" + path: "{{ item.ansible_facts.user_home }}/{{ p10k_path }}" + owner: "{{ item.item }}" + group: "{{ item.item }}" block: | if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" @@ -73,16 +60,16 @@ insertbefore: BOF marker_begin: "BEGIN P10K INSTANT PROMPT" marker_end: "END P10K INSTANT PROMPT" - loop: "{{ p10k_users_information }}" + with_items: "{{ paths.results }}" - name: Enable powerlevel10k config file ansible.builtin.blockinfile: - path: "{{ item['home'] }}/{{ p10k_zshrc_config[zsh_plugin]['zsh_file'] }}" - owner: "{{ item['name'] }}" - group: "{{ item['group'] }}" + path: "{{ item.ansible_facts.user_home }}/{{ p10k_path }}" + owner: "{{ item.item }}" + group: "{{ item.item }}" block: | [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh insertafter: EOF marker_begin: "BEGIN P10K CONFIG FILE" marker_end: "END P10K CONFIG FILE" - loop: "{{ p10k_users_information }}" + with_items: "{{ paths.results }}" \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml index 368cb85..737df18 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,64 +1,24 @@ --- # Global vars file for ansible-role-p10k + +- name: Set home location for each users + set_fact: + user_home: "{{ '/root' if item == 'root' else '/home/' + item }}" + with_items: "{{ zsh_users }}" + register: paths + p10k_style: lean: 'lean' classic: 'classic' rainbow: 'rainbow' pure: 'rainbow' -p10k_path: - zsh: 'powerlevel10k' - ohmyzsh: '.oh-my-zsh/custom/themes/powerlevel10k' - prezto: 'powerlevel10k' - zim: 'powerlevel10k' - antibody: 'powerlevel10k' - antigen: 'powerlevel10k' - zplug: 'powerlevel10k' - zgen: 'powerlevel10k' - zplugin: 'powerlevel10k' - zinit: 'powerlevel10k' +p10k_path: '.oh-my-zsh/custom/themes/powerlevel10k' p10k_zshrc_config: - zsh: - regexp: '^source ' - line: 'source ~/powerlevel10k/powerlevel10k.zsh-theme' - zsh_file: .zshrc - ohmyzsh: - regexp: '^ZSH_THEME=' - line: 'ZSH_THEME="powerlevel10k/powerlevel10k"' - zsh_file: .zshrc - prezto: - regexp: '^zstyle :prezto:module:prompt theme' - line: 'zstyle :prezto:module:prompt theme powerlevel10k' - zsh_file: .zpreztorc - zim: - regexp: '^zmodule ' - line: 'zmodule romkatv/powerlevel10k' - zsh_file: .zimrc - antibody: - regexp: '^antibody bundle ' - line: 'antibody bundle romkatv/powerlevel10k' - zsh_file: .zshrc - antigen: - regexp: '^antigen theme ' - line: 'antigen theme romkatv/powerlevel10k' - zsh_file: .zshrc - zplug: - regexp: '^zplug ' - line: 'zplug romkatv/powerlevel10k, as:theme, depth:1' - zsh_file: .zshrc - zgen: - regexp: '^zgen load ' - line: 'zgen load romkatv/powerlevel10k powerlevel10k' - zsh_file: .zshrc - zplugin: - regexp: '^zplugin ice depth=1; zplugin light' - line: 'zplugin ice depth=1; zplugin light romkatv/powerlevel10k' - zsh_file: .zshrc - zinit: - regexp: '^zinit ice depth=1; zinit light' - line: 'zinit ice depth=1; zinit light romkatv/powerlevel10k' - zsh_file: .zshrc + regexp: '^ZSH_THEME=' + line: 'ZSH_THEME="powerlevel10k/powerlevel10k"' + zsh_file: .zshrc time_config: no: