Hello.
I have an issue with with dynamically adding host via API.
I have several service applies like this:
apply Service “load” {
import “load-service”
check_command = “load”
command_endpoint = host.vars.client_endpoint
assign where “linux-servers” in host.groups
}
And I tried to add host via API:
curl -k -s -u apiuser:apipass -H 'Accept: application/json' -X PUT 'https://master.somedomain.com:5665/v1/objects/hosts/test-node01' -d '{ "templates": [ "generic-host" ], "attrs": { "address": "10.10.10.12", "check_command": "hostalive", "vars.os" : "Linux", "vars.client_endpoint": "test-node01", "vars.location": "loc1", "vars.role": "lb" , "zone": "ams1" }, "pretty": true }'
But I get the error:
…
{
“results”: [
{
“code”: 500.0,
“errors”: [
“Error: Validation failed for object ‘test-node01!load’ of type ‘Service’; Attribute ‘command_endpoint’: Object ‘test-node01’ of type ‘Endpoint’ does not exist.\nLocation: in /etc/icinga2/zones.d/global-templates/services.conf: 81:3-81:46\n/etc/icinga2/zones.d/global-templates/services.conf(79): import “load-service”\n/etc/icinga2/zones.d/global-templates/services.conf(80): check_command = “load”\n/etc/icinga2/zones.d/global-templates/services.conf(81): command_endpoint = host.vars.client_endpoint\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n/etc/icinga2/zones.d/global-templates/services.conf(82): assign where “linux-servers” in host.groups\n/etc/icinga2/zones.d/global-templates/services.conf(83): }\n”,
],
“status”: “Object could not be created.”
}
]
}
…
So I can’t add a host since it doesn’t pass the validation and it doesn’t pass the validation because the host doesn’t exist.
As a workaround i see adding a host in two steps:
- Adding host without Endpoint variable.
- Modifying previously added hosts to adding endpoint variable.
Nevertheless, I belive there is smarter way to resolve the issue.
I would really appreciate a for your advices.