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.

1 Like

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.

Iā€™ve created a simple inventory script based on Ravi Bhureā€™s omd.py that supports multisite and ssl

The nagios-livestatus.py wouldnā€™t run in my environment, and some of the functionality I needed I had already added to omd.py. I also eschewed the check-mk web api because it is missing some critical functionality that I require.

The repo is here- GitHub - wezelboy/simple-ansible-inventory-from-check-mk: Provides dynamic inventory host info from check-mk to ansible.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.