Creating an Apply For rule across multiple variables

Maybe this could be a starting point to achieve what you need. In my sample I’ve created two hosts with a simplified dictionary from your initial post. The const SomeChecks is an array with two elements (as your const OracleChecks).

Using for loops can be real fun :slight_smile:

image

Summary
object Host "test-01" {
  import "generic-host"

  address = "127.0.0.1"

  vars.db_instances = {
    "inst01" = {
      "username" = "username"
    }
  }
}

object Host "test-02" {
  import "generic-host"

  address = "127.0.0.1"

  vars.db_instances = {
    "inst01" = {
      "username" = "username"
    },
    "inst02" = {
      "username" = "username"
    }
  }
}

const SomeChecks = [ "check1", "check2" ]

for ( checkname in SomeChecks ) {

  apply Service checkname + "-" for (env => instance in host.vars.db_instances) use(checkname) {
    import "generic-service"

    check_command = "ping4"
    display_name = env + "-" + checkname

    assign where host.address && match("test-*", host.name)
  }

}

Please note, the service’s object name starts with the check name from the const array. The display name is overwritten inside the apply rule, so you get your desired result in Icingaweb2.

For example, inst01-check1 is the object name of the service and (flipped parts) it’s check1-inst01 for the display name.
image

2 Likes