2

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.

Timo
  • 143
  • 7
  • opened a bug report: https://github.com/ansible/ansible/issues/15089 – Timo Mar 22 '16 at 08:51
  • Now I know it is a problem with encoding. As soon as there is a umlaut in a name ansible interprets the output as a long string. – Timo Mar 22 '16 at 12:17
  • I guess bash is not returning well formatted. I wrote a python script that has same functionality and returns e.g. \u00f6 as ö. I don't know how to achieve that in bash. – Timo Mar 24 '16 at 14:27

1 Answers1

2

If I am not mistaken your JSON output looks something like this:

[{
  "domain": "foo",
  ...
},{
  "domain": "foo",
  ...
},{
  "domain": "foo",
  ...
}]

So that's a list, and it sure is valid JSON, but Ansible would not know what to do with this list. You need to give it a key so Ansible knows to which fact the list should be assigned to:

{
  "myList": [{
    "domain": "foo",
    ...
  },{
    "domain": "foo",
    ...
  },{
    "domain": "foo",
    ...
  }]
} 
udondan
  • 2,061
  • 15
  • 18
  • hey. thanks for your reply. this is something I tried before and it works fine as a plain json but still, in my bash-script it does return the same cluttered string and no json. I think there might be a bug considering my long search and the strange behaviour. – Timo Mar 22 '16 at 08:22