Host-Ansible-Setup/home_resources/.local/bin/spark_ansible-editvault.sh

58 lines
1.8 KiB
Bash
Executable File

#!/bin/bash
# {{ ansible_managed }}
if [ -z $1 ]; then
echo "No ansible vault provided."
exit 1
fi
USBKEYVAR=/dev/disk/by-uuid/{{ usbkey_uuid }}
USBKEYSLOCATION={{ usbkey_keysdir }}
# Figure out if path given is relative or absolute, assign variables accordingly
if [[ "$(echo $1 | cut -c 1)" == "/" ]]; then
# Absolute
ABSOLUTELOCATION=$1
else
# Relative
ABSOLUTELOCATION="$(pwd)/$1"
fi
# Probably cleaner to do this in awk or the like but good enough!
# FILEPARENTDIR="$(echo $ABSOLUTELOCATION | cut -d / --fields=$(echo $ABSOLUTELOCATION | grep -o / | wc -l))"
FILENAME="$(echo $ABSOLUTELOCATION | cut -d / --fields=$(($(echo $ABSOLUTELOCATION | grep -o / | wc -l) + 1))-)"
if [ -e $USBKEYVAR ]; then
echo "USB Key detected, will mount if not already."
if doas mount $USBKEYVAR; then
echo "Mounted USB Key, proceeding..."
else
echo "Failed to mount USB Key, assuming its already mounted and proceeding..."
fi
if [[ "$FILENAME" == *"server"* ]]; then
echo "Detected as a server vault, decrypting accordingly..."
if ansible-vault edit --vault-password-file $USBKEYSLOCATION/serversecrets $ABSOLUTELOCATION; then
echo "Ansible Vault edit success!"
else
echo "Ansible Vault edit failure!"
fi
elif [[ "$FILENAME" == *"service"* ]]; then
echo "Detected as a service vault, decrypting accordingly..."
if ansible-vault edit --vault-password-file $USBKEYSLOCATION/servicesecrets $ABSOLUTELOCATION; then
echo "Ansible Vault edit success!"
else
echo "Ansible Vault edit failure!"
fi
else
echo "Does not appear to be a server or service vault, please make sure the vault file includes either 'server' or 'service' in the name to identify!"
fi
if doas umount $USBKEYVAR; then
echo "Unmounted USB Key."
else
echo "Failed to unmount USB Key!"
fi
else
echo "USB Key not detected, please check if plugged in!"
exit 1
fi