#!/bin/sh
# -*- mode: sh; tab-width: 8; encoding: utf-8-unix -*-

# on stdout - messages on stderr

prog=`basename $0 .bash`
PREFIX=/usr/local
ROLE=base
base=AnsI

# quiet
[ "$#" -eq 0 ] && exit 1
VARIABLE=$1

[ -f $PREFIX/etc/testforge/testforge.bash ] && . $PREFIX/etc/testforge/testforge.bash

[ -n "$TESTFORGE_ANSIBLE_SRC" ] || TESTFORGE_ANSIBLE_SRC=/g/TestForge/src/ansible

name=`hostname`

if [ -d "$TESTFORGE_ANSIBLE_SRC" ] && [ -f $TESTFORGE_ANSIBLE_SRC/hosts.yml ] ; then
    base=$name
    ansible-inventory -i $TESTFORGE_ANSIBLE_SRC/hosts.yml \
		      --playbook-dir=$TESTFORGE_ANSIBLE_SRC \
		      --host=$base >> /tmp/${AnsI}$$.json 2> /tmp/${AnsI}$$.err
    if [ $? -eq 0 -a -f /tmp/${AnsI}$$.json ] ; then
	#!? export
	VALUE=`jq .$VARIABLE </tmp/${AnsI}$$.json | sed -e  's/,//'|xargs echo`
	# [ -n "$DEBUG" ] && echo >&2 "DEBUG: $prog base=$base VALUE=$VALUE"
	[ "$VALUE" = "null" ] && VALUE=""
	echo -n "$VALUE"
    fi
    rm -f /tmp/${AnsI}$$.json
fi

exit 0
