Accessing service var from service template

Hello everyone,
I created a service template which I want to use to customize services check interval, icon, notifications and command endpoint for a specific service if needed:

# Custom service template: this template is used from every custom plugins
template Service "custom-service" {
    import "generic-service"

    # Custom check interval
    if (vars.check_interval) {
        check_interval = vars.check_interval
    }

    # Custom icon
    if (vars.icon_image) {
        icon_image = vars.icon_image
    }

    # Enable notifications
    if (vars.enable_notifications) {
        enable_notifications = vars.enable_notifications
    }

    # Enable force remote command
    if (vars.command_endpoint) {
        command_endpoint = vars.command_endpoint
    }
}

One example service is using this template:

apply Service for (tcp => config in host.vars.tcp) {
  import "custom-service"

  check_command = "tcp"

  vars += config
}

These variables are added to the host:

vars.tcp["TCP Service"] = {
  tcp_port = 4022
  command_endpoint = "my.command.endpoint"
}

What I expect is this command to be executed from the endpoint my.command.endpoint, but it is executed from the main Icinga Server.

I am able to enable the remote execution if I add the conditional statement into the Service object:

apply Service for (tcp => config in host.vars.tcp) {
  import "custom-service"

  check_command = "tcp"

  vars += config

  if (vars.command_endpoint) {
    command_endpoint = vars.command_endpoint
  }
}

I would like to add these optional settings into the template so that I don’t need to add them for every service I have.

Could you help me to understand where I’m wrong, please?

Thank you very much!

Hello M!

No custom vars are set at the time you import the template.

As your own command endpoint example shows, the template must be imported after the custom vars.

Best,
A/K