Skip to content

Instantly share code, notes, and snippets.

@TimoKramer
Last active March 22, 2016 08:42
Show Gist options
  • Select an option

  • Save TimoKramer/d948c8f03c2e76519f91 to your computer and use it in GitHub Desktop.

Select an option

Save TimoKramer/d948c8f03c2e76519f91 to your computer and use it in GitHub Desktop.
a custom facts script that returns a long cluttered string instead of valid json
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment