fboender / ansible-cmdb

Generate host overview from ansible fact gathering output
GNU General Public License v3.0
2.36k stars 468 forks source link

Empty web page when using fact caching #186

Open sc01194 opened 5 years ago

sc01194 commented 5 years ago

Hi,

I am using ansible-cmdb V1.29 with python 2.7.15 (ansible seems to be running on python 2.7.5)

When I generate the web page from data produced by the command "ansible -m setup --tree mydir/ all" everything is fine but If I use the data from fact caching I have the hosts list but they have no data. ansible_cmd fact_cache

The only differences in the json files produced is the formatting. The json files from the ansible command line have no carriage return when the json files from fact caching are formatted for readability with carriage return. Exemple attached json.zip

Any idea of what I do wrong? I lookup in the parameters for fact caching for an option to remove carriage return but I could not find one.

Regards, Sebastien Chanson

sc01194 commented 5 years ago

I found what was wrong the fact caching json file is missing the entry "ansible_facts": at the beginning of the file. If I add it manually then I have the data in the web page. Sorry for missing that in my first post. I do not know if this is due to my ansible setup or if this is ansible-cmdb which is not handling properly fact_cache data?

fboender commented 5 years ago

Hey sc01194!

I do not know if this is due to my ansible setup or if this is ansible-cmdb which is not handling properly fact_cache data?

It's ansible-cmdb's fault. I've noticed before that Ansible itself isn't particularly consistent when dealing with facts. Sometimes they contain the "ansible_facts" key, sometimes they don't (fact caching, dynamic inventory scripts). I've documented that here:

Please note that the --fact-cache option will apply to all fact directories you specify. This means you can't mix fact-cache fact directories and normal setup fact directories. Also, if you wish to manually extend facts (see the Extending chapter), you must omit the ansible_facts key and put items in the root of the JSON.

But that's just laziness on my part because I kinda assumed most people wouldn't be using fact caching to generate the CMDB. I'll look into it again, see if I can come up with a decent fix for this.

Thanks for the report!

vossenjp commented 5 years ago

This one affects me too. I'm using ansible-2.3.2.0 and just pip installed ansible-cmdb.py v1.30 but the problem remains. I've also noticed how inconsistent Ansible is with fact dumps. :-(

I have some shell code that uses jq for some other reporting, and I had to do this stupid hack-around:

...
# WTH?  Deal with maybe/maybe not ".ansible_facts" layer
if grep -q '^{"ansible_facts":' $node; then
    af='.ansible_facts'
else
    af=''
fi
...
jq -r "$af.ansible_distribution                     + \"-\" +
...
Sputnik93 commented 2 years ago

A fix for this issue would be greatly appreciated indeed.