From 4a1da188019586101bcee5cba6fbef606ef7d4e9 Mon Sep 17 00:00:00 2001 From: Tuan-Dat Tran Date: Sun, 22 Mar 2026 23:46:42 +0100 Subject: [PATCH] refactor: migrate to role-based variable management and blockinfile config --- bootstrap.sh | 11 +- .../starship/.config/starship/config.toml | 24 ---- roles/common/defaults/main.yml | 6 + roles/common/handlers/main.yml | 3 + roles/common/tasks/main.yml | 113 +++++++++++++----- roles/cs_student/tasks/main.yml | 22 ++-- roles/devops/defaults/main.yml | 1 + roles/devops/tasks/main.yml | 18 +-- roles/dotfiles/defaults/main.yml | 4 + roles/dotfiles/tasks/main.yml | 20 ++-- roles/hyprland/tasks/main.yml | 2 +- roles/multimedia/tasks/main.yml | 11 ++ site.yml | 4 +- 13 files changed, 146 insertions(+), 93 deletions(-) delete mode 100644 dotfiles/starship/.config/starship/config.toml create mode 100644 roles/common/defaults/main.yml create mode 100644 roles/common/handlers/main.yml create mode 100644 roles/devops/defaults/main.yml create mode 100644 roles/dotfiles/defaults/main.yml create mode 100644 roles/multimedia/tasks/main.yml diff --git a/bootstrap.sh b/bootstrap.sh index dff0e96..e575541 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,6 +1,9 @@ #!/usr/bin/env bash set -euo pipefail +ARCHSETUP_REPO="${ARCHSETUP_REPO:-https://github.com/YOUR_USERNAME/archsetup}" +ARCHSETUP_DIR="${ARCHSETUP_DIR:-$HOME/archsetup}" + echo "=== Arch Linux Hyprland Setup Bootstrap ===" echo "" @@ -25,18 +28,18 @@ sudo pacman -Sy --noconfirm git ansible # Step 3: Clone or update the repo echo "[3/4] Setting up archsetup..." -if [ -d "$HOME/archsetup/.git" ]; then +if [ -d "$ARCHSETUP_DIR/.git" ]; then echo "Updating archsetup..." - cd "$HOME/archsetup" + cd "$ARCHSETUP_DIR" git pull else echo "Cloning archsetup..." - git clone https://github.com/YOUR_USERNAME/archsetup.git "$HOME/archsetup" + git clone "$ARCHSETUP_REPO" "$ARCHSETUP_DIR" fi # Step 4: Run the playbook echo "[4/4] Running Ansible playbook..." -cd "$HOME/archsetup" +cd "$ARCHSETUP_DIR" ansible-playbook site.yml echo "" diff --git a/dotfiles/starship/.config/starship/config.toml b/dotfiles/starship/.config/starship/config.toml deleted file mode 100644 index 400d7cd..0000000 --- a/dotfiles/starship/.config/starship/config.toml +++ /dev/null @@ -1,24 +0,0 @@ -$formatters -format = """ -[─](bold green) -[❯](bold green) """ -add_newline = true - -[character] -success_symbol = "[❯](bold green)" -error_symbol = "[❯](bold red)" - -[directory] -home_symbol = "~" -truncation_length = 3 -truncate_to_repo = true - -[git] -branch_symbol = " " -stash_count_symbol = " " -format = "on [$symbol$branch]($style)$completions" -style = "bold purple" - -[docker_context] -format = "via [$symbol$context]($style) " -style = "blue bold" diff --git a/roles/common/defaults/main.yml b/roles/common/defaults/main.yml new file mode 100644 index 0000000..3002b75 --- /dev/null +++ b/roles/common/defaults/main.yml @@ -0,0 +1,6 @@ +--- +archsetup_user: "{{ ansible_user_id }}" +archsetup_repo: "{{ lookup('env', 'ARCHSETUP_REPO', default='https://github.com/YOUR_USERNAME/archsetup') }}" +archsetup_user_home: "{{ lookup('env', 'HOME') }}" +archsetup_dir: "{{ archsetup_user_home }}/archsetup" +aur_helper: paru diff --git a/roles/common/handlers/main.yml b/roles/common/handlers/main.yml new file mode 100644 index 0000000..3508b7b --- /dev/null +++ b/roles/common/handlers/main.yml @@ -0,0 +1,3 @@ +- name: Update font cache + become: true + command: fc-cache -fv diff --git a/roles/common/tasks/main.yml b/roles/common/tasks/main.yml index d783c57..9282faa 100644 --- a/roles/common/tasks/main.yml +++ b/roles/common/tasks/main.yml @@ -1,4 +1,21 @@ # tasks file for common +- name: Check for legacy zshrc content + shell: | + if grep -q "^# ~/.zshrc" "{{ archsetup_user_home }}/.zshrc" 2>/dev/null; then + echo "1" + else + echo "0" + fi + register: legacy_check + changed_when: false + +- name: Clean legacy zshrc content + replace: + path: "{{ archsetup_user_home }}/.zshrc" + regexp: '^# ~/.zshrc.*\n' + replace: '' + when: legacy_check.stdout | int > 0 + - name: Install base development tools become: true pacman: @@ -57,7 +74,7 @@ - name: Ensure ttf-joypixels is installed from AUR kewlfft.aur.aur: name: ttf-joypixels - use: paru + use: "{{ aur_helper | default('paru') }}" state: present notify: Update font cache @@ -85,24 +102,22 @@ become: true getent: database: passwd - key: "{{ user }}" + key: "{{ archsetup_user }}" register: user_info changed_when: false - name: Set zsh as default shell become: true - shell: chsh -s /bin/zsh {{ user }} - when: user_info.ansible_facts.getent_passwd[user][-1] != '/bin/zsh' + command: chsh -s /bin/zsh {{ archsetup_user }} + when: user_info.ansible_facts.getent_passwd[archsetup_user][-1] != '/bin/zsh' - name: Setup shell profile configuration blockinfile: - path: "{{ lookup('env', 'HOME') }}/.profile" + path: "{{ archsetup_user_home }}/.profile" marker: "# {mark} ANSIBLE MANAGED BLOCK" create: yes mode: '0644' - content: | - # ~/.profile - POSIX compatible environment - + block: | # Rust and cargo binaries export PATH="$HOME/.cargo/bin:$HOME/.local/bin:$PATH" @@ -113,26 +128,68 @@ export EDITOR="emacs -nw" -- name: Setup zprofile for Zsh compatibility - copy: - dest: "{{ lookup('env', 'HOME') }}/.zprofile" - content: | - # ~/.zprofile - Sourced by Zsh for login shells - # Source .profile for POSIX-compatible environment - emulate sh -c 'source ~/.profile' - - # Initialize zoxide for smart cd - eval "$(zoxide init zsh)" - - # Initialize atuin for shell history - eval "$(atuin init zsh)" - owner: "{{ lookup('env', 'USER') }}" - mode: '0644' - -- name: Setup shell plugins in zshrc +- name: Deploy zshrc base config blockinfile: - path: "{{ lookup('env', 'HOME') }}/.zshrc" - marker: "# {mark} ANSIBLE MANAGED BLOCK" - content: | + path: "{{ archsetup_user_home }}/.zshrc" + marker: "# {mark} ANSIBLE MANAGED - BASE" + block: | # Source .zprofile for login shell environment [[ -f ~/.zprofile ]] && emulate sh -c 'source ~/.zprofile' + + # Enable oh-my-zsh + export ZSH="$HOME/.oh-my-zsh" + ZSH_THEME="agnoster" + + # Plugins + plugins=( + git + history + z + ) + + source $ZSH/oh-my-zsh.sh + + # Initialize starship (must be after oh-my-zsh) + eval "$(starship init zsh)" + # + # Initialize zoxide (must be after oh-my-zsh) + eval "$(zoxide init zsh)" + + # Initialize atuin (must be after oh-my-zsh) + eval "$(atuin init zsh)" + +- name: Deploy zshrc history config + blockinfile: + path: "{{ archsetup_user_home }}/.zshrc" + marker: "# {mark} ANSIBLE MANAGED - HISTORY" + block: | + # History + export HISTSIZE=50000 + export SAVEHIST=50000 + export HISTTIMEFORMAT="[%F %T] " + + setopt INC_APPEND_HISTORY + setopt EXTENDED_HISTORY + setopt HIST_EXPIRE_DUPS_FIRST + setopt HIST_IGNORE_DUPS + setopt HIST_IGNORE_ALL_DUPS + setopt HIST_FIND_NO_DUPS + setopt HIST_IGNORE_SPACE + setopt HIST_SAVE_NO_DUPS + setopt SHARE_HISTORY + +- name: Deploy zshrc common aliases + blockinfile: + path: "{{ archsetup_user_home }}/.zshrc" + marker: "# {mark} ANSIBLE MANAGED - COMMON ALIASES" + block: | + # Common aliases + alias q="exit" + alias vim="nvim" + alias cat="bat" + alias ls="eza --icons" + alias ll="eza -l --icons" + alias la="eza -la --icons" + alias lt="eza --tree --level=2 --icons" + alias df="df -h" + alias wttr="curl wttr.in/Essen" diff --git a/roles/cs_student/tasks/main.yml b/roles/cs_student/tasks/main.yml index 434f541..7f86ba4 100644 --- a/roles/cs_student/tasks/main.yml +++ b/roles/cs_student/tasks/main.yml @@ -38,16 +38,12 @@ creates: ~/.emacs.d - name: Setup shell aliases - lineinfile: - path: "{{ lookup('env', 'HOME') }}/.zshrc" - line: "{{ item }}" - create: yes - loop: - - alias ls='eza --icons --group-directories-first' - - alias ll='eza -la --icons --group-directories-first' - - alias cat='bat --style=auto' - - alias find='fd' - - alias top='btm' - - alias tre='broot --sizes' - - alias e='emacs -nw' - - alias vim='nvim' + blockinfile: + path: "{{ archsetup_user_home }}/.zshrc" + marker: "# {mark} ANSIBLE MANAGED - CS STUDENT" + block: | + # CS student aliases + alias find='fd' + alias top='btm' + alias tre='broot --sizes' + alias e='emacs -nw' diff --git a/roles/devops/defaults/main.yml b/roles/devops/defaults/main.yml new file mode 100644 index 0000000..ed97d53 --- /dev/null +++ b/roles/devops/defaults/main.yml @@ -0,0 +1 @@ +--- diff --git a/roles/devops/tasks/main.yml b/roles/devops/tasks/main.yml index 1dfdcd4..be0c6fb 100644 --- a/roles/devops/tasks/main.yml +++ b/roles/devops/tasks/main.yml @@ -13,12 +13,12 @@ - kubectx state: present -- name: Setup shell aliases - lineinfile: - path: "{{ lookup('env', 'HOME') }}/.zshrc" - line: "{{ item }}" - create: yes - loop: - - alias kc='kubectl' - - alias kctx='kubectx' - - alias kns='kubens' +- name: Deploy DevOps aliases + blockinfile: + path: "{{ archsetup_user_home }}/.zshrc" + marker: "# {mark} ANSIBLE MANAGED DEVOPS" + block: | + # DevOps aliases + alias kc="kubectl" + alias kctx="kubectx" + alias kns="kubens" diff --git a/roles/dotfiles/defaults/main.yml b/roles/dotfiles/defaults/main.yml new file mode 100644 index 0000000..2b324d5 --- /dev/null +++ b/roles/dotfiles/defaults/main.yml @@ -0,0 +1,4 @@ +--- +dotfiles_stow_packages: + - hypr + - zellij diff --git a/roles/dotfiles/tasks/main.yml b/roles/dotfiles/tasks/main.yml index fb1f6f5..440a2a3 100644 --- a/roles/dotfiles/tasks/main.yml +++ b/roles/dotfiles/tasks/main.yml @@ -7,17 +7,15 @@ state: present - name: Remove existing config files before stowing - shell: | - [ -L ~/.config/hypr/hyprland.conf ] || rm -f ~/.config/hypr/hyprland.conf - [ -L ~/.config/starship ] || rm -rf ~/.config/starship - [ -L ~/.config/zellij ] || rm -rf ~/.config/zellij - changed_when: false + file: + path: "{{ archsetup_user_home }}/.config/{{ item }}" + state: absent + loop: + - hypr + - zellij - name: Create symlinks with stow - shell: | - cd {{ lookup('env', 'HOME') }}/archsetup/dotfiles - stow -t ~ hypr starship zellij + command: stow --target={{ archsetup_user_home }} {{ dotfiles_stow_packages | join(' ') }} args: - executable: /bin/bash - creates: "{{ lookup('env', 'HOME') }}/.config/starship" - chdir: "{{ lookup('env', 'HOME') }}/archsetup/dotfiles" + creates: "{{ archsetup_user_home }}/.config/hypr" + chdir: "{{ archsetup_dir }}/dotfiles" diff --git a/roles/hyprland/tasks/main.yml b/roles/hyprland/tasks/main.yml index b36828f..efd2126 100644 --- a/roles/hyprland/tasks/main.yml +++ b/roles/hyprland/tasks/main.yml @@ -76,6 +76,6 @@ - name: Add user to docker group become: true user: - name: "{{ user }}" + name: "{{ archsetup_user }}" groups: docker append: yes diff --git a/roles/multimedia/tasks/main.yml b/roles/multimedia/tasks/main.yml new file mode 100644 index 0000000..c2bdcaf --- /dev/null +++ b/roles/multimedia/tasks/main.yml @@ -0,0 +1,11 @@ +# tasks file for multimedia +- name: Install multimedia packages + become: true + pacman: + name: + - vlc + - ffmpeg + - imagemagick + - ghostscript + - mpv + state: present diff --git a/site.yml b/site.yml index 6412d37..670934c 100644 --- a/site.yml +++ b/site.yml @@ -1,9 +1,6 @@ --- - hosts: localhost connection: local - vars: - aur_helper: paru - user: "{{ ansible_user_id }}" roles: - { role: common, tags: ['common'] } @@ -11,5 +8,6 @@ - { role: cs_student, tags: ['cs_student'] } - { role: devops, tags: ['devops'] } - { role: ctf, tags: ['ctf'] } + - { role: multimedia, tags: ['multimedia'] } - { role: hyprland, tags: ['hyprland'] } - { role: ai, tags: ['ai'] }