Variable not recognized using object definition

I’m not sure whether this is a bug or feature: test1 will have the variable by_ssh_logname defined while test2 is null

test1.conf:

apply Service "test1" {
   vars.by_ssh_logname = host.vars.ssh_username
   ...
}

icinga2 object list…:

  * vars
    * by_ssh_logname = "nagios"
      % = modified in '/etc/icinga2/conf.d/test/test1.conf', lines 3:4-3:47

test2.conf:

object Service "test2" {
   host_name = "example1"
   vars.by_ssh_logname = host.vars.ssh_username
   ...
}

icinga2 object list…:

  * vars
    * by_ssh_logname = null
      % = modified in '/etc/icinga2/conf.d/test/test2.conf', lines 4:4-4:47

I’m not sure whether this is a bug or feature: test1 will have the variable
by_ssh_logname defined while test2 is null

Are both service checks applied to the same host, or different hosts?

test1.conf:

apply Service "test1" {
   vars.by_ssh_logname = host.vars.ssh_username
   ...
}

icinga2 object list…:

  * vars
    * by_ssh_logname = "nagios"
      % = modified in '/etc/icinga2/conf.d/test/test1.conf', lines 3:4-3:47

test2.conf:

object Service "test2" {
   host_name = "example1"
   vars.by_ssh_logname = host.vars.ssh_username
   ...
}

icinga2 object list…:

  * vars
    * by_ssh_logname = null
      % = modified in '/etc/icinga2/conf.d/test/test2.conf', lines 4:4-4:47

Show us the ssh_username variable definition, and the object list output for
the host/s.

Antony.

Same host:

object Host "example1" {
    vars.ssh_username = "nagios"
   ...
}

icinga2 object list…:

...
  * vars
    * ssh_username = "nagios"
...

Why are you defining host_name in the test2 Service Check?

How are you applying the Service Checks to the Host?

Antony.

I use apply Service in general. But for a test I’ve used object Service.

1 Like

Ah, sorry, I missed that bit of detail in your original posting.

So, test1 gets applied to the (previously defined) Host, at which time Icinga
knows all about the properties of that Host, therefore it picks up the
ssh_username value correctly.

But test2 is simply defined, and not applied to a host, so Icinga doesn’t
know what the properties of Host example1 are when test2 is being defined,
therefore it can’t get a value for ssh_username.

That’s one of the advantages of using apply rules.

Antony.

And that’s exactly the problem here. With apply rules, you’ll virtually get access to the host variable and can use that in this scope. With just creating a service object, no such reference exists yet at this point during config compilation.

Cheers,
Michael