Ansible inventory from Check_Mk

Has anyone written a script to gather a server inventory out of Check_Mk using Livestatus then formatting the output so that it can be used by Ansible dynamic inventory?

Why you want to use Livestatus. Better use the WebAPI to get the inventory data as JSON.

Sure, that works too… Livestatus will also let you output in JSON. What I was hoping is that someone already wrote something that created JSON that Ansible expects (See Inventory Script Conventions here: https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#inventory-script-conventions). I’m not very good with JSON but I will attempt to fumble through it if nobody else has.

With the WebAPI, I don’t see an example of how to return a list of all servers. All of the examples are requesting info for a specific host or hosts. How do I return info for all servers or all servers matching some filter (wildcard, matching tag, etc)?

Hi,
to get all host for the inventory request per host, you can use get_all_host from the WebAPI.
Cheers,
Christian

…something like that ?
Regards,
Simon

Yes, exactly like this… but my ansible did not seem to come with it? And I’m a github newbie so not having any luck finding the actual script ‘contrib/omd.py’.

Edit: Sorry this probably isn’t clear… towards the bottom of the page you link, it linked to this: https://github.com/ansible/ansible/pull/11870 which seems really promising… but again… my lack of github skills is getting in the way.

Replying to myself… I now see the notes that the OMD specific one was abandoned in favor of this one you linked to… nagios_livestatus.py. I’m trying it out, but unfortunately it does not support encrypted livestatus which I’m using… If anyone better than me with Python wants to offer suggestions for adding encryption support, that would be great.

From the monitor host itself, so you can just tie directly to the livestatus socket (no SSL, etc), assuming a Linux with nc (netcat):

echo -e 'GET hosts\nColumns: name\nOutputFormat: json\n' | nc -U /opt/omd/sites/monit
or/tmp/run/live

Optionally, you can pretty print that JSON by piping through python -mjson.tool

Edit: the name of my site is “monitor” in the above path to the “live” socket.

Assuming you have an “automation” user setup on your cmk and secret, something like this could work:

curl -s "https://your-ssl-hostname.example.com/monitor/check_mk/webapi.py?action
=get_all_hosts&_username=automation&_secret=my-very-long-secret&request_format=pytho
n&output_format=json" 

And again, you can pretty print that to easily see how that looks by piping through python -mjson.tool

Hi @lance
I wanted to ask if you have any progress in your tries? If not I would try to create something with the webapi way.

@marass, I have not yet… I have sort of put this to the back burner for the past month. I need to start investigating it again soon.

@lance so I have now a working PoC via WebAPI. I will push it on github if it has a structure that you can customize it.

Sounds great! Can you tell me where to find it on github?

Are you able to share a link to your github?

I’ve been using the omd.py for a few years now, but I added some functionality to list all the hostgroups and their members- which is really helpful with some of my playbooks.

I haven’t seen anything in the web API that can help with this. get_all_hostgroups just returns the hostgroup names.