summaryrefslogtreecommitdiff
path: root/common/utils.sh
blob: 2749eda6a482faf309c715409a7bfbca0951199a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
}