Notification apply using host.templates

Hiya,

I am trying to create a template stacking system that determines the role of the hosts based on which host templates it has applied. For instance, start with a generic base host template, then add a ‘role-mysql-database’ host template which exposes additional fields.

That is working fine, however I am hitting a problem when trying to create a Notification apply rule that uses the hosts.templates = role-database. It seems that Notification apply rules can’t use this variable, though I can use it with service apply rules. Since you can also use Host Groups and other custom variables for service assigns it seems like it would be possible.

Here’s my sample config;

zones.d/master/notification_apply.conf

apply Notification “email ops services test” to Host {
import “service-email-notify-template”

assign where host.templates == "role-mysql-database"
users = [ "mysql-dba" ]

}

If I change the apply where to use host.groups, various host.vars or other custom vars the notification test works fine. So, it seems host.templates cannot be used in this way.

Any thoughts? My goal is to be able to use the existence of a certain host template to create both service apply rules (working) and notification rules (not working). If anyone has an an alternative thought on how to accomplish something like that I’d welcome it.

My versions;
Icingaweb2 2.7.1
Director 1.7.0
Icinga2 2.11.0

Hi,

host.templates is of the array type, so you need to use a different operator than == which is used for raw values like string, number, boolean. In order to check whether a value exists in a given array, use the in operator.

You can test this with the debug console before creating an assign where expression.

$ icinga2 console
Icinga 2 (version: 2.11.2-1)
Type $help to view available commands.
<1> => var templates = [ "generic-host", "linux-host" ]
null
<2> => "linux-host" in templates
true
<3> => "windows-host" in templates
false

Cheers,
Michael

1 Like

Hi there - perfect, thanks for steering me in the right direction. I had noticed that type difference and tried using the ‘in’ operator;

assign where host.templates in [ “role-mysql-database” ]

Turns out, I just needed to use ‘contains’.

assign where “role-mysql-database” in host.templates

Thanks for your quick response and continued constant top notch community support for all us icinga2/web/director users, so consistently over such a long period of time. As I’ve built my implementation, nearly all the questions I’ve had were previously answered by you, either here or on github.

I’ll mark this as solved, if my implementation ends up working the way I like it I will publish it as a small guide that may help someone else.

2 Likes