This repository has been archived on 2026-04-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
ansible-vault-tools/ansible-vault-tools.sh

48 lines
1.2 KiB
Bash
Raw Normal View History

2023-07-04 17:43:07 +02:00
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2023 Max Mehl <https://mehl.mx>
#
# SPDX-License-Identifier: Apache-2.0
CMD=$1
2023-07-11 12:44:03 +02:00
# Encrypt string
if [[ $CMD == "encrypt-string" ]]; then
2023-07-04 17:43:07 +02:00
pass=$2
vaultpw=$(echo -n "$pass" | ansible-vault encrypt_string 2> /dev/null)
2023-07-11 12:44:03 +02:00
# Decrypt string
elif [[ $CMD == "decrypt-string" ]]; then
2023-07-04 17:43:07 +02:00
host=$2
var=$3
# run ansible msg for variable
# send return as JSON
vaultpw=$(ANSIBLE_LOAD_CALLBACK_PLUGINS=1 ANSIBLE_STDOUT_CALLBACK=json ansible "$host" -m debug -a "msg={{$var}}" 2> /dev/null)
# Parse JSON to just get the "msg"
vaultpw=$(jq -r ".plays[].tasks[].hosts[].msg" <<< "$vaultpw")
2023-07-11 12:44:03 +02:00
# Encrypt file
elif [[ $CMD == "encrypt-file" ]]; then
file=$2
ansible-vault encrypt "$file"
# Decrypt file
elif [[ $CMD == "decrypt-file" ]]; then
file=$2
ansible-vault decrypt "$file"
2023-07-04 17:43:07 +02:00
else
echo "Invalid command"
2023-07-08 09:02:41 +02:00
echo ""
echo "Usage:"
2023-07-11 12:44:03 +02:00
echo "ansible-vault-tools encrypt-string <password>"
echo "ansible-vault-tools decrypt-string <host> <variable>"
echo ""
echo "ansible-vault-tools encrypt-file <file-path>"
echo "ansible-vault-tools decrypt-file <file-path>"
2023-07-04 17:43:07 +02:00
exit 1
fi
echo "$vaultpw"