How to employ applyrules using director via CLI or REST API?

api

(Dmitry) #1

Hello everyone.
I need to create a fully automate deployment of icinga including the connection of agents, creation of host templates, service templates and apply rules. So, that after deployment all necessary hosts are already monitored using specified services.
In general, Icinga2 Director module fits well for this goal, however, I faced some issues, which I wasn’t able to resolve.
One of them is applyrules. It turned out that I am able to create applyrule using e.g.:

./director-curl.sh POST director/service '{"object_name":"my_apply_rule", "object_type":"apply","assign_filter":"host.vars.os=%22Debian%22","imports":"apt_service_t"}'

note that for this example I used script kindly provided in icinga director github: https://github.com/Icinga/icingaweb2-module-director/blob/master/doc/70-REST-API.md#CURL-helper-script

the command creates necessary applyrule and works fine. If to fire this command one more time - the same result will be returned:

  HTTP/1.1 201 Created
Date: Wed, 09 Jan 2019 05:29:53 GMT
Server: Apache/2.4.29 (Ubuntu)
X-Powered-By: PHP/7.2.10-0ubuntu0.18.04.1
Content-Length: 165
Content-Type: application/json

{
    "assign_filter": "host.vars.os=%22Debian%22",
    "imports": [
        "apt_service_t"
    ],
    "object_name": "my_apply_rule",
    "object_type": "apply"
}

The system now has two applyrules with completely the same content including the name. Only inside of director database these two items have different id.
I would expect that system returns anything like 422; “error”: “Trying to recreate”, as if I would attempt to recreate service / service template, host/host template, hostgroup etc. But taking into account comment of @Thomas-Gelf https://github.com/Icinga/icingaweb2-module-director/issues/1238, it is clear that there is some issue with names of applyrules. Under the previous link, it is mentioned that it is impossible to get the list of applyrules. As the issue have been removed from release 1.5 and current stable release now is 1.6 looks like there is no way to get the entire list of applyrules as well as retrieve a single applyrule by name.
Attempt to do so

/director-curl.sh GET director/service?name=my_apply_rule

gives:

"error": "Failed to load icinga_service for host_id IS NULL AND service_set_id IS NULL AND object_name = 'my_apply_rule' AND object_type = 'template'"

This issue is decribed here: https://github.com/Icinga/icingaweb2-module-director/issues/1228

note that it does not matter whether you fire it using REST API or CLI

Summary:

  • It is possible to create applyrule over API
  • It is not possible to retrieve, edit and even delete applyrule over API.
  • It is possible to create many applyrules with the same content.

My question to the community:
How are you dealing with this? I believe that I’m not alone who need automatization on the level of services and rules.
How do you manage applyrules over API?

At the result I want to achieve idempotent ansible playbook, which may be applied even many times without harming the system.

Thanks in advance.


Access dependency via API
How to assign service (template) to host (template) using Icinga director CLI or REST API
List the template objects via API
What do we need to automate for Icinga?