summaryrefslogtreecommitdiff
path: root/common/utils.sh
diff options
context:
space:
mode:
Diffstat (limited to 'common/utils.sh')
-rw-r--r--common/utils.sh21
1 files changed, 21 insertions, 0 deletions
diff --git a/common/utils.sh b/common/utils.sh
new file mode 100644
index 00000000..2749eda6
--- /dev/null
+++ b/common/utils.sh
@@ -0,0 +1,21 @@
+## this contains several helper functions
+
+get_ansible_variable() {
+ local _var_name="$1"
+ local _hosts="$2"
+
+ local _result=$(env ANSIBLE_STDOUT_CALLBACK="json" ansible "$_hosts" -m debug -a "var=$_var_name" | \
+ jq -r ".plays[].tasks[].hosts[].$_var_name" | sort | uniq)
+ if [ $? -ne 0 ] || [ -z "$_result" ]; then
+ return 1
+ fi
+
+ local _num_results=$(echo "$_result" | wc -l)
+ if [ $_num_results -ne 1 ]; then
+ echo "ERROR: hosts may only belong to one environment group but got: $(echo $_result | xargs | sed 's/ /, /g')"
+ return 1
+ fi
+
+ eval "$_var_name"='$(echo "$_result")'
+ return 0
+}