From bcb98b9e9a01806373207f61cc1181704aacf0e6 Mon Sep 17 00:00:00 2001 From: binaryDiv Date: Mon, 31 May 2021 14:25:10 +0200 Subject: [PATCH] Add two ansible helpers --- bin/ansible-skeleton | 81 +++++++++++++++++++++++++++++++++++ bin/ansible-vault-show-inline | 28 ++++++++++++ 2 files changed, 109 insertions(+) create mode 100755 bin/ansible-skeleton create mode 100755 bin/ansible-vault-show-inline diff --git a/bin/ansible-skeleton b/bin/ansible-skeleton new file mode 100755 index 0000000..577e309 --- /dev/null +++ b/bin/ansible-skeleton @@ -0,0 +1,81 @@ +#!/bin/bash + +# Strict mode +set -euo pipefail +IFS=$'\n\t' + +BASENAME=$(basename $0) + +# Define role subdirectories +VALID_SUBDIRS=(defaults files handlers meta tasks templates vars) +DEFAULT_SUBDIRS=(defaults tasks) + +function usage { + cat << END_OF_HELPTEXT +Usage: $BASENAME ROLENAME [SUBDIRS]... + +Creates a skeleton for an Ansible role under ./roles/ROLENAME with +subdirectories and main.yml files. + +SUBDIRS is a subset of the following sub directories: + ${VALID_SUBDIRS[@]} + +If SUBDIRS is not specified, it defaults to: + ${DEFAULT_SUBDIRS[@]} +END_OF_HELPTEXT +} + +function inarray { + local match="$1"; shift + local el + for el; do + [[ "$el" == "$match" ]] && return 0 + done + return 1 +} + +# Show help if no arguments are given or one of them is --help +[[ $# -eq 0 ]] && usage && exit 0 + +for arg in $@; do + [[ $arg == "--help" ]] && usage && exit 0 +done + +# Parse arguments +ROLENAME=$1 + +if [[ $# -eq 2 && $2 == all ]]; then + # Special value all: create all subdirectories + SUBDIRS=("${VALID_SUBDIRS[@]}") +elif [[ $# -gt 1 ]]; then + SUBDIRS=() + + for arg in "${@:2}"; do + if ! inarray $arg "${VALID_SUBDIRS[@]}"; then + echo "Unknown role subdirectory: $arg" >&2 + exit 1 + fi + SUBDIRS+=($arg) + done +else + SUBDIRS=("${DEFAULT_SUBDIRS[@]}") +fi + +ROLEDIR="roles/$ROLENAME" + +# Create base directory for role +mkdir -p "$ROLEDIR" + +# Create skeleton +for subdir in "${SUBDIRS[@]}"; do + echo "Creating $ROLEDIR/$subdir" + mkdir -p "$ROLEDIR/$subdir" + + case "$subdir" in + defaults|handlers|meta|tasks|vars) + filename="$ROLEDIR/$subdir/main.yml" + echo "Creating $filename" + touch "$filename" + ;; + esac +done diff --git a/bin/ansible-vault-show-inline b/bin/ansible-vault-show-inline new file mode 100755 index 0000000..b74d7c3 --- /dev/null +++ b/bin/ansible-vault-show-inline @@ -0,0 +1,28 @@ +#!/bin/bash + +# Strict mode +set -euo pipefail +IFS=$'\n\t' + +if [[ $# -eq 0 ]] || [[ $1 == "--help" ]]; then + echo "Usage: $(basename $0) FILE VARIABLE" + echo + echo "Decrypts ansible-vault encrypted inline variable VARIABLE from FILE." + echo + echo "Example: $(basename $0) roles/common/defaults.main.yml user_root_password" + + exit 1 +elif [[ $# -lt 2 ]]; then + echo "Error: Missing arguments." >&2 + exit 1 +fi + +VAR_FILE=$1 +VAR_NAME=$2 + +if [[ ! -f $VAR_FILE ]]; then + echo "Error: Variable file '$VAR_FILE' does not exist!" >&2 + exit 1 +fi + +ansible localhost -m debug -a "var=$VAR_NAME" -e "@$VAR_FILE"