Last active
October 20, 2025 22:07
-
-
Save k3karthic/72492bb6303ef4843cb868dd6c48f501 to your computer and use it in GitHub Desktop.
Fedora Silverblue - Run Container Ansible Playbooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Exit immediately if a command exits with a non-zero status. | |
| set -eo pipefail | |
| # --- 1. Verify we are inside a Toolbox container --- | |
| echo "▶️ Checking environment..." | |
| if [ ! -f /run/.containerenv ]; then | |
| echo "❌ Error: This script must be run inside a Toolbox container." >&2 | |
| exit 1 | |
| fi | |
| echo "✅ Running inside a Toolbox container." | |
| # --- 2. Install Ansible if not present --- | |
| echo "▶️ Checking for Ansible..." | |
| if command -v ansible-playbook &> /dev/null; then | |
| echo "✅ Ansible is already installed." | |
| else | |
| echo "⏳ Ansible not found. Attempting installation..." | |
| if command -v dnf &> /dev/null; then | |
| # For Fedora, RHEL, etc. | |
| sudo dnf install -y ansible-core | |
| elif command -v apt-get &> /dev/null; then | |
| # For Debian, Ubuntu, etc. | |
| sudo apt-get update && sudo apt-get install -y ansible-core | |
| elif command -v pacman &> /dev/null; then | |
| # For Arch Linux, etc. | |
| sudo pacman -Syu --noconfirm ansible | |
| else | |
| echo "❌ Error: Could not find a known package manager (dnf, apt, pacman)." >&2 | |
| echo "Please install Ansible manually." >&2 | |
| exit 1 | |
| fi | |
| echo "✅ Ansible installed successfully." | |
| fi | |
| # --- 2b. Arch Linux: Fix Locale --- | |
| # If pacman exists, we assume it's an Arch-based container. | |
| if command -v pacman &> /dev/null; then | |
| echo "▶️ Arch Linux detected. Checking for en_IN.UTF-8 locale..." | |
| LOCALE_TO_GEN="en_IN UTF-8" | |
| LOCALE_FILE="/etc/locale.gen" | |
| # Check if the locale is already enabled in the config file. | |
| # The '!' negates the grep result, so the block runs if the line is NOT found. | |
| if ! grep -q "^${LOCALE_TO_GEN}" "${LOCALE_FILE}"; then | |
| echo "⏳ Locale not generated. Enabling and generating..." | |
| sudo pacman -S --noconfirm glibc | |
| # Uncomment the locale line in the file. | |
| sudo sed -i "s/^#${LOCALE_TO_GEN}/${LOCALE_TO_GEN}/" "${LOCALE_FILE}" | |
| # Regenerate all locales based on the updated file. | |
| sudo locale-gen | |
| echo "✅ Locale ${LOCALE_TO_GEN} generated." | |
| else | |
| echo "✅ Locale ${LOCALE_TO_GEN} is already configured." | |
| fi | |
| fi | |
| # --- 3. Determine Container Name --- | |
| echo "▶️ Determining container name..." | |
| CONTAINER_NAME=$(awk -F'=' '$1=="name" {print $2}' /run/.containerenv | tr -d '"') | |
| if [ -z "$CONTAINER_NAME" ]; then | |
| echo "❌ Error: Could not determine container name from /run/.containerenv." >&2 | |
| exit 1 | |
| fi | |
| echo "✅ Container name is: ${CONTAINER_NAME}" | |
| # --- 4. Navigate and Run Playbook --- | |
| PLAYBOOK_DIR="$HOME/container-playbooks" | |
| PLAYBOOK_FILE="$PLAYBOOK_DIR/${CONTAINER_NAME}.yml" | |
| echo "▶️ Looking for playbook: $PLAYBOOK_FILE" | |
| # Ensure the playbook directory exists | |
| if [ ! -d "$PLAYBOOK_DIR" ]; then | |
| echo "⚠️ Warning: Playbook directory '$PLAYBOOK_DIR' not found. Will not run playbook." >&2 | |
| exit 0 | |
| fi | |
| if [ ! -f "$PLAYBOOK_FILE" ]; then | |
| echo "❌ Error: Playbook file not found!" >&2 | |
| echo "Please create '$PLAYBOOK_FILE' before running this script." >&2 | |
| exit 1 | |
| fi | |
| # Change to the playbook directory. This is good practice for Ansible | |
| # as it can help with locating roles and other resources. | |
| cd "$PLAYBOOK_DIR" | |
| echo "🚀 Running Ansible playbook: ${CONTAINER_NAME}.yml..." | |
| echo "----------------------------------------------------" | |
| # Run the playbook | |
| ansible-playbook "${CONTAINER_NAME}.yml" -K | |
| echo "----------------------------------------------------" | |
| echo "🎉 Playbook execution finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment