If conditional for apply rule not working

Hey Guys

I was working on the apply rules for my checks and I wanted to create a “If/else” condition. When the variable “host.vars.disk_custom” is defined it should use the custom apply, with the config parameters defined in the host object and if not the default apply rule with the default parameters.
But somehow it is not working, I think it’s a syntax problem.

services.conf:

if ( host.vars.disk_custom" ) {
apply Service for (disk => config in host.vars.disk_custom) {
import “generic-service”
check_command = “disk”
vars += config

assign where host.vars.avec_os == "Linux" 

}
} else {
apply Service “disk” {
import “generic-service”
check_command = “disk”
vars.disk_wfree = “10%”
vars.disk_cfree = “5%”
vars.disk_errors_only = “true”

assign where host.zone == host.vars.avec_os == "Linux"

}
}

host.conf

object Host “xx.xx.xx.xx.xx” {
check_command = “hostalive”
address = “xx.xx.xx.xx.xx”
vars.agent_endpoint = name //follows the convention that host name == endpoint name
vars.avec_linux_distro = “RedHat”
vars.avec_linux_distro_major_version = “7”
vars.avec_linux_distro_version = “7.6”
vars.avec_os = “Linux”
vars.notification[“mail”] = {
groups = [ “icingaadmins” ]
}
vars.disk_custom[“Disk”] = {
disk_errors_only = “false”
disk_wfree = “5%”
}
}

icinga2 daemon -C

[2020-03-18 15:40:58 +0100] critical/config: Error: Error while evaluating expression: Tried to access undefined script variable ‘host’
Location: in /etc/icinga2/zones.d/xxx/services.conf: 16:6-16:9
/etc/icinga2/zones.d/xxx/services.conf(14):
/etc/icinga2/zones.d/xxx/services.conf(15):
/etc/icinga2/zones.d/xxx/services.conf(16): if ( host.vars.disk_custom ) {
^^^^
/etc/icinga2/zones.d/xxx/services.conf(17): apply Service for (disk => config in host.vars.disk_custom) {
/etc/icinga2/zones.d/xxx/services.conf(18): import “generic-service”

[2020-03-18 15:40:58 +0100] critical/cli: Config validation failed. Re-run with ‘icinga2 daemon -C’ after fixing the config.

Do you know what could be wrong?

Thanks for the help
Cheers

apply for does not need assign statements:

apply Service for (disk => config in host.vars.disk_custom) {
   import “generic-service”
   check_command = “disk”
   vars += config
}

The other condition could look like this:

apply Service “disk” {
   import “generic-service”
   check_command = “disk”
   vars.disk_wfree = “10%”
   vars.disk_cfree = “5%”
   vars.disk_errors_only = “true”

   assign where host.vars.avec_os == "Linux"
   ignore where host.vars.disk_custom
}
1 Like

i think you also have an unclosed ( unneeded) quotation mark there.

Thanks this helped me