I have a custom fact for my servers to retrieve customer data. This works fine on the machine and returning valid json. I can write the output to another facts-file and this works perfectly fine too. But when I do ansible setup the script returns all the data as one string in a key and an empty value like so:
"custom_fact": {
"{data...}": {}
}
This is my facts-script:
#!/bin/bash
#{{ ansible_managed }}
### extracting information from moodle instances
cd /home/vhosts/moodle
function getinstances() {
MDL_INSTANCES=`find * -maxdepth 0 -not -name "_*" |xargs echo`
}
getinstanceinfo() {
INSTANCE_ARRAY=()
for i in $MDL_INSTANCES
do
source /home/vhosts/moodle/$i/_update.conf
firstname="$(mysql -u $UM_DBUSER -p$UM_DBPASS -h mysql -e 'SELECT firstname FROM mdl_user WHERE id=2;' $UM_DBNAME)"
firstname=${firstname#firstname}
lastname="$(mysql -u $UM_DBUSER -p$UM_DBPASS -h mysql -e 'SELECT lastname FROM mdl_user WHERE id=2;' $UM_DBNAME)"
lastname=${lastname#lastname}
email="$(mysql -u $UM_DBUSER -p$UM_DBPASS -h mysql -e 'SELECT email FROM mdl_user WHERE id=2;' $UM_DBNAME)"
email=${email#email}
supportemail="$(mysql -u $UM_DBUSER -p$UM_DBPASS -h mysql -e "SELECT value FROM mdl_config WHERE name='supportemail';" $UM_DBNAME)"
supportemail=${supportemail#value}
string="\"domain\":\"$i\",\"firstname\":\"$firstname\",\"lastname\":\"$lastname\",\"email\":\"$email\",\"supportemail\":\"$supportemail\""
string=$(echo $string | tr -d '\n')
INSTANCE_ARRAY+=("$string")
done
}
# creating json for ansible extraction
writetostdout() {
printf '['
for (( i=0; i<${#INSTANCE_ARRAY[@]}-1; i++ ))
do
printf "{ ${INSTANCE_ARRAY[$i]} },"
done
printf "{ ${INSTANCE_ARRAY[-1]} }"
printf ']'
}
getinstances
getinstanceinfo
writetostdout
Has anyone an idea why this does not work? I am really stuck. Thanks for help.