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
2023-07-04 17:45:20 +02:00

31 lines
691 B
Bash
Executable File

#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2023 Max Mehl <https://mehl.mx>
#
# SPDX-License-Identifier: Apache-2.0
CMD=$1
# Encrypt
if [[ $CMD == "encrypt" ]]; then
pass=$2
vaultpw=$(echo -n "$pass" | ansible-vault encrypt_string 2> /dev/null)
# Decrypt
elif [[ $CMD == "decrypt" ]]; then
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")
else
echo "Invalid command"
exit 1
fi
echo "$vaultpw"