## 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 }