REST API how to add/delete hosts to/from a host group

Well, you won’t find a POST in the rulesets - because you can’t create rulesets, only rules.
The “internal” name you can use with the API for Assignment of Hosts to host groups is host_groups.

An python example would be:

#!/usr/bin/env python3
import pprint
import requests

HOST_NAME = "localhost"
SITE_NAME = "cmk"
API_URL = f"http://{HOST_NAME}/{SITE_NAME}/check_mk/api/1.0"

USERNAME = "automation"
PASSWORD = "test123"

session = requests.session()
session.headers['Authorization'] = f"Bearer {USERNAME} {PASSWORD}"
session.headers['Accept'] = 'application/json'

resp = session.post(
    f"{API_URL}/domain-types/rule/collections/all",
    headers={
        "Content-Type": 'application/json',  # (required) A header specifying which type of content is in the request/response body.
    },
    json={
        'ruleset': 'host_groups',
        'folder': '~some_folder',
        'properties': {
            'disabled': False
        },
        'value_raw': "'the_host_group'",
        'conditions': {}
    },
)
if resp.status_code == 200:
    pprint.pprint(resp.json())
elif resp.status_code == 204:
    print("Done")
else:
    raise RuntimeError(pprint.pformat(resp.json()))
1 Like