#!/bin/bash PLAYBOOK_DIR="./playbooks" # Ensure gum is installed if ! command -v gum &> /dev/null; then echo "gum could not be found. Please install gum first." exit 1 fi # Ensure pass is installed if ! command -v pass &> /dev/null; then echo "pass could not be found. Please install pass first." exit 1 fi echo "Fetching ansible-vault password" VAULT_PASS=$(pass show gluttony-cluster/ansible-vault) if [[ -z "$VAULT_PASS" ]]; then echo "Failed to retrieve Ansible Vault password from pass." exit 1 fi # Create a list of playbooks in the specified directory PLAYBOOKS=$(ls -1 "$PLAYBOOK_DIR"/*.yaml 2> /dev/null) if [[ -z "$PLAYBOOKS" ]]; then echo "No playbooks found in $PLAYBOOK_DIR." exit 1 fi # Use gum to select a playbook SELECTED_PLAYBOOK=$(echo "$PLAYBOOKS" | gum choose) if [[ -z "$SELECTED_PLAYBOOK" ]]; then echo "No playbook selected. Exiting." exit 1 fi # Confirmation step with gum echo "You selected: $SELECTED_PLAYBOOK" if ! gum confirm "Are you sure you want to run this playbook? ($SELECTED_PLAYBOOK)"; then echo "Operation cancelled. Exiting." exit 1 fi # Run the selected playbook with the Ansible Vault password echo "Running playbook: $SELECTED_PLAYBOOK" ANSIBLE_VAULT_PASSWORD_FILE=$(mktemp) echo "$VAULT_PASS" > "$ANSIBLE_VAULT_PASSWORD_FILE" ansible-playbook -i inventory.yaml "$SELECTED_PLAYBOOK" --vault-password-file "$ANSIBLE_VAULT_PASSWORD_FILE" # Clean up the temporary password file rm -f "$ANSIBLE_VAULT_PASSWORD_FILE"