From f628303ebd445e46468e6b809cf2d6f890a4bf70 Mon Sep 17 00:00:00 2001 From: Nicola Belluti Date: Sat, 4 Oct 2025 09:33:49 +0200 Subject: [PATCH] Added the script to run the playbook --- README.md | 4 +- home/.chezmoiscripts/run_before_arch.sh.tmpl | 49 ++++++++++++++++++++ home/.gitkeep | 0 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100755 home/.chezmoiscripts/run_before_arch.sh.tmpl delete mode 100644 home/.gitkeep diff --git a/README.md b/README.md index 590a0b8..004ce4d 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,13 @@ # ~/.dotfiles 🏡 [![chezmoi](https://img.shields.io/badge/chezmoi-4051b5)](https://chezmoi.io/) +[![Ansible](https://img.shields.io/badge/Ansible-ee0000?logo=ansible)](https://ansible.com/) [![MIT License](https://img.shields.io/badge/License-MIT-dark_green)](https://choosealicense.com/licenses/mit/)
-> My dotfiles, managed with [chezmoi](https://chezmoi.io/) +> My dotfiles, managed with [chezmoi](https://chezmoi.io/) and +> [Ansible](https://ansible.com/) ## Usage diff --git a/home/.chezmoiscripts/run_before_arch.sh.tmpl b/home/.chezmoiscripts/run_before_arch.sh.tmpl new file mode 100755 index 0000000..153a949 --- /dev/null +++ b/home/.chezmoiscripts/run_before_arch.sh.tmpl @@ -0,0 +1,49 @@ +#!/bin/sh + +# Check is the script is being run using chezmoi +{{- if false }} +echo "This script must be run using chezmoi! Aborting..." +exit 1 +{{- end }} + +# Run this script only on Arch Linux +{{- if ne .chezmoi.osRelease.id "arch" }} +exit 0 +{{- end }} + +# Make the script stop if something goes wrong: +# -e: any command fails +# -u: unset variable is used +# -o pipefail: a pipeline fails if any command fails +set -euo pipefail + +# Show a clear error before exiting +SCRIPT_PATH="{{ trimPrefix "/" (trimPrefix .chezmoi.workingTree (joinPath .chezmoi.sourceDir .chezmoi.sourceFile)) }}" +trap 'echo -e "\n\033[1;31mError: Script \"$SCRIPT_PATH\" failed at line $LINENO\033[0m\n" >&2; exit 1' ERR + +# +# + +# Check if python3 and python3-venv are installed +if ! command -v python3 >/dev/null 2>&1; then + echo -e "\e[1;34mpython3 not found. Installing it...\e[0m" + sudo pacman --noconfirm -Sy python +fi + +ANSIBLE_ROOT="{{ .chezmoi.workingTree }}/setup-script" + +# Check if there is a venv in "setup-script/" +if [ ! -d "$ANSIBLE_ROOT/.venv" ]; then + echo -e "\e[1;34mNo venv found in "$ANSIBLE_ROOT". Creating one...\e[0m" + python3 -m venv "$ANSIBLE_ROOT/.venv" + + echo -e "\e[1;34mInstalling the dependencies...\e[0m" + "$ANSIBLE_ROOT/.venv/bin/pip3" install -r "$ANSIBLE_ROOT/requirements.txt" +fi + +# Run the playbook +echo -e "\e[1;34mRunning the playbook...\e[0m" +ANSIBLE_CONFIG="$ANSIBLE_ROOT/ansible.cfg" \ + "$ANSIBLE_ROOT/.venv/bin/ansible-playbook" \ + "$ANSIBLE_ROOT/playbook.yaml" \ + --extra-vars "$(chezmoi data --format=json)" diff --git a/home/.gitkeep b/home/.gitkeep deleted file mode 100644 index e69de29..0000000