I’ struggling with importing services from a rest api call. The actual import is of course working fine. There is only three columns: name, color, url. In the sync rule I have chosen object type service. And the sync property mapping is set to name -> object_name, url -> notes_url, color -> vars.color.
I understand that I need to relate these services to a host (or hosts), but I don’t understand how to do that. There is two hosts I want to add these services to.
The error I get is: Exception while syncing Icinga\Module\Director\Objects\IcingaService batch-: Cannot store a Service object without a related host
Okay, I found a way to “hard code” the host property by making one sync rule per host to create all services on. And then set the Source Column to Custom expression and enter the hostname as a variable. Is this the only way to do this? Can I set several hosts in one sync rule for example? With like an apply rule where I wildcard the hostname?
Okay, this got really messy. Since service apply rules are duplicated I destroyed my whole Director config by adding over 2000 apply rules that was wrong. Luckily I was in test! When I finally got it working (by filtering out all but one object) it got duplicated each sync.
In contrast to the documentation I had to format the filter as “host.vars.os=%22Linux%2A%22” not as suggested “host.vars.os = Linux*”. But the issue sustained, every time I synced a new apply rule was added and stopped deployment since the host already had an object.
So typically I see a failure when director is trying to recreate an object and not a duplication, but in most cases this is caused by having a different primary key in the import rule than the object_name in the sync rule. Could this be the case? Because this is the only idea I have at the moment.
I think there’s something special with Service Apply Rules in Icinga2. From the Rest API documentation (that I used to build a script to remove the over 2000 objects that was wrongfully created):
Please note that Service Apply Rule names are not unique in Icinga 2. They are not real objects, they are creating other objects in a loop. This makes it impossible to distinct them by name. Therefore, a dedicated REST API endpoint director/serviceapplyrules ships all Service Apply Rules combined with their internal ID. This ID can then be used to modify or delete a Rule via director/service .