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

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.:

./ 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:

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": [
    "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, 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

/ GET director/service?name=my_apply_rule


"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:

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


  • 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.

This reply might help:

It basically says, that the apply rules are service generators, so you have to fetch all those single services and act on them instead.