Creating a dynamic service definition with a for loop into an Apply for loop

I’m trying to dynamically create some repetitive service definitions looping a nested dictionary into an apply Service for definition.

My question is quite similar to this one, but I’m trying to implement it in a different way so I’d prefer to open a new thread.

The plugin I’m trying to add is check_mssql_healthcheck_mssql_health, which supports several check modes.

I want to automatically apply some modes with a default configuration for each SQL Server instance I’ve declared into the hostvars:

object Host "sqltest1" {
  import "generic-host"
  address = ""

  vars.client_endpoint = name

  vars.mssql["SQL Server 2019"] = {
    mssql_health_username = "SQLTEST1\\Icinga2"
    mssql_health_password = "password"
    mssql_health_port = 1234
    mssql_health_mode = "auto"

For the declared SQL Server 2019 instance I want to automatically add the checks declared into a constant:

const mssql_health_auto_modes = {
    "Connection Time" = {
        mssql_health_mode = "connection-time"
    "Connected Users" = {
        mssql_health_mode = "connected-users"
        mssql_health_warning = "80"
        mssql_health_critical = "100"
    "Transactions per second" = {
        mssql_health_mode = "transactions"
    "Batch requests per second" = {
        mssql_health_mode = "batch-requests"
        mssql_health_warning = "400"
        mssql_health_critical = "500"

For this I tried to add this piece of code:

# Automatic service definition WITHOUT specific mode configures (through 'mssql' host.var and host.var.mssql_health_mode == "auto" or undefined)
apply Service for (service_name => service_config in host.vars.mssql) {
    for (check_name => check_config in mssql_health_auto_modes) {
        import "generic-service"
        name = service_name + " - " + check_name
        check_command = "_check_mssql_health"
        vars.mssql_health_mode = check_config.mssql_health_mode
        vars += service_config
        assign where !service_config.mssql_health_mode || service_config.mssql_health_mode == "auto"

This does not end with any errors, but only one service is added to the host:

And this is nor the first nor the latest check declared into the mssql_health_auto_modes constant, so I cannot understand what’s going on.

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

Thank you very much!

this is quite a bit late, but might be helpful for others. vars.mssql just has one entry on the host.
This would be "SQL Server 2019". There for the loop only runs once and generates only one service.


This is an interesting approach. Were you able to succeed in its implementation?


Hello @d83!

You need a for loop around the apply-for, not inside.