Parsing dictionary variables in conf file

In services.conf, the “core_dump_notification” has been set as shown in below.

apply Service "coredump.tps.tfs.production" {
  import "search_coredump-service"
  vars.alt_erp = "liuyana"
  vars.alt_token = "df46e2281f8dc9fa9eb396b4962cc"
  vars.jmq_group = "Coredump.searertest"
  vars.check_module = "nana"
  vars.core_dump_notification.mail.users += [ "liuyana" ]
  vars.core_dump_notification.timline.users += [ "liuyana" ]
  assign where host.name == "coredump-host"
}

“vars.core_dump_notification” dictionary containing custom variables that are specific to this service. I need get all custom variables of “vars.core_dump_notification” which are mail and timline in notification conf.
Such as service.vars.core_dump_notification.keys or for key in service.vars.core_dump_notification
i set notification conf as below.

apply Notification "mq-coredump-notification" to Service {
  command = "mq-service-notification"
  users = ["liuyana"]
  vars.involved_users = {{
    var res = {}
    if (service.vars.core_dump_notification.first){
      for (key in service.vars.core_dump_notification.first){
          res[key] = service.vars.core_dump_notification.first[key]["users"]
      }
    }
    return res
  }}
  states = [ Critical ]
  types = [ Problem ]
  interval = 0m
  period = "24x7"
  assign where service.vars.core_dump_notification.first.mail
}

however, i got the error message:

#warning/Notification: Exception occured during notification for checkable 'coredump-host!coredump.tps.tfs.production': Error: Cannot use array iterator for dictionary.

Do you have any suggestions?
Thanks a lot.

service is not a scoped variable in the anonymous lambda function, it is not defined. Therefore the loop iteration fails.

Instead of the anonymous lambda function, you could look into closures where you’ll bind the service variable with use into the functions context.

https://icinga.com/docs/icinga2/latest/doc/17-language-reference/#closures

Cheers,
Michael

Thanks for your quick response. I found the solution in this link,https://icinga.com/docs/icinga2/latest/doc/17-language-reference/#for-loops.
Meanwhile, i found the example in ‘for-loops’ is wrong as shown in below.

critical/config: Error: syntax error, unexpected var (T_VAR), expecting T_STRING or T_IDENTIFIER
Location: in /etc/icinga2/zones.d/master/coredump-mq/notifications.conf: 29:12-29:14
/etc/icinga2/zones.d/master/coredump-mq/notifications.conf(27):     var res = {}
/etc/icinga2/zones.d/master/coredump-mq/notifications.conf(28):     if (service.vars.core_dump_notification.first){
/etc/icinga2/zones.d/master/coredump-mq/notifications.conf(29):       for (var key => var value in service.vars.core_dump_notification.first){
                                                                           ^^^
/etc/icinga2/zones.d/master/coredump-mq/notifications.conf(30):         res[key] = []
/etc/icinga2/zones.d/master/coredump-mq/notifications.conf(31):         for (name in value["users"]){

The correct format is this which no ‘var’.

    var res = {}
    if (service.vars.core_dump_notification.first){
      for (key => value in service.vars.core_dump_notification.first){
        if (value["users"]){
          res[key] = []
        }
        for (name in value["users"]){
          res[key] += [name]
        }
      }
    }
    return res
  }}

the icinga2 version is v2.5.4.
To summarise, i have resolved my questions through the above method.
thanks a lot.

The example documents the current 2.11 version. 2.5 doesn’t have that, and also misses many other DSL features.