Dynamically setting check_interval fails

If in my object host I have:

vars.check_interval = "5m"

and in my apply service I have:

  if (host.vars.check_interval) {
    check_interval = host.vars.check_interval
  }

But I get a config validation error upon reload.

However hardcoding the value in the apply service works, eg:

  if (host.vars.check_interval) {
        check_interval = 5m
  }

Can you try this?

check_interval = "$host.vars.check_interval$"

hmm, still get an error, safe-reload fails.

I figured it must be some delimiting of the variable as you suggest though.

It may help if you share the error you are getting.

1 Like

Haven’t tried this myself, but my guess is that setting the variable as the value will result in check_interval = "variable_value". But the value for the interval has to be given without the ".
The only way that comes to my mind to work around that is setting the value like in your second code snippet and have multiple if-conditions to check for the value of the var.

But that is only a working solution if you don’t have many different interval options, I guess.

Is this an apply rule or single object? For some strange reasons you can directly access the host object and its attributes in an apply but not an object. To access it from an object you need some code like this one borrowed from the Director:

    if (! host) {
      var host = get_host(host_name)
    }

It’s in an apply (top of section is apply Service for (nrpe_check in host.vars.nrpe_checks))

error looks like this:


Oct 18 09:42:10 jserv safe-reload[1337683]: /etc/icinga2/conf.d/jp/svc-tmpl.conf(180):   if (host.vars.check_interval) {
Oct 18 09:42:10 jserv safe-reload[1337683]: /etc/icinga2/conf.d/jp/svc-tmpl.conf(181):  // check_interval = 5m
Oct 18 09:42:10 jserv safe-reload[1337683]: /etc/icinga2/conf.d/jp/svc-tmpl.conf(182):     check_interval = "$host.vars.check_interval$"
Oct 18 09:42:10 jserv safe-reload[1337683]:                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Oct 18 09:42:10 jserv safe-reload[1337683]: /etc/icinga2/conf.d/jp/svc-tmpl.conf(183):   }
Oct 18 09:42:10 jserv safe-reload[1337683]: [2021-10-18 09:42:10 +0000] critical/config: 1 error
Oct 18 09:42:10 jserv safe-reload[1337683]: [2021-10-18 09:42:10 +0000] critical/cli: Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config.
Oct 18 09:42:10 jserv systemd[1]: icinga2.service: Control process exited, code=exited, status=1/FAILURE
Oct 18 09:42:10 jserv systemd[1]: Reload failed for Icinga host/service/network monitoring system.