Dynamic service name from host variables

I’m used to declare some services in this way:

object Host "foo.bar.baz" {
  [...]

  vars.bar["My Beautiful Service"] = {
    [...]
  }
}

apply Service "Foo " for (name => config in host.vars.bar) {
  [...]
}

This works and creates a service named Foo My Beautiful Service.

If I want the service to be named as My Beautiful Service Foo Then I tried to create the same service with this apply rule:

apply Service for (name => config in host.vars.bar) " Foo " {
  [...]
}

But this not works:

[2020-01-23 18:37:21 +0000] critical/config: Error: syntax error, unexpected T_STRING, expecting '{'
Location: in /etc/icinga2/services.d/custom-services.conf: 1:57-1:66
/etc/icinga2/services.d/custom-services.conf(1): apply Service for (name => config in host.vars.bar) " Foo" {
                                                                                                    ^^^^^^^^^^
/etc/icinga2/services.d/custom-services.conf(2):   import "generic-service"
/etc/icinga2/services.d/custom-services.conf(3): 

[2020-01-23 18:37:21 +0000] critical/cli: Config validation failed. Re-run with 'icinga2 daemon -C' after fixing the config.

Is there a way to do what I want?

Thank you very much!

I have recently set up my first service like this (via Icinga Director though).
Config looks like this:

apply Service for (config in host.vars.partitionen) {
    name = "disk-" + config
    check_command = "disk-windows"
    max_check_attempts = "5"
    check_interval = 5m
    retry_interval = 1m
    assign where "windows-servers" in host.groups
    command_endpoint = host_name
    vars.disk_win_crit = "90%"
    vars.disk_win_path = config + ":"
    vars.disk_win_show_used = true
    vars.disk_win_warn = "80%"

    import DirectorOverrideTemplate
}

Service name inside the Director looks like:
image

My guess would be that a line like name = config + "-disk" would also work and switch the name around.

1 Like