Addition assignment for variables in arrays

Hello Folks,
I try to approach something like this in notifications:

Apply:

apply Notification "mail-service" to Service {
  import "mail-service-notification-template"

  user_groups += [ service.vars.notification.mail_contacts.groups ]

  assign where service.vars.notification.mail_contacts.groups
}

Template notication 1:

template Service "notification-mail-departmentA-service" {

enable_flapping = true

        vars.notification["mail_contacts"] = {
        groups = [ "departmentA" ]
        }
}

Template notication 2:

template Service "notification-mail-departmentB-service" {

enable_flapping = true

        vars.notification["mail_contacts"] = {
        groups = [ "departmentB" ]
        }
}

Template service check:

template Service "check_A_template" {
import "notification-mail-departmentA-service"
check_command = "..."
...
}

template Service "check_B_template" {
import "notification-mail-departmentB-service"
check_command = "..."
...
}

template Service "check_AB_template" {
import "notification-mail-departmentA-service"
import "notification-mail-departmentB-service"
check_command = "..."
...
}

How could I solve it, without using too many apply rules? Many thanks in advance!

Best regards
David

Hi @parad1se

I think that the groups will be overwritten if you do it like this. Maybe this works:

// for template Service departmentB
vars.notification["mail_contacts"].groups += "departmentB"

Kind regards

Hi @ritzgu
thanks for the suggestion, this is the result which I get with:

// for template Service departmentB
vars.notification["mail_contacts"].groups += "departmentB"

Result:
[2021-03-23 10:09:41 +0100] critical/config: Error: Error while evaluating expression: Operator + cannot be applied to values of type 'Array' and 'String'

Also tried:

// for template Service departmentA
vars.notification["mail_contacts"].groups += "departmentA"

// for template Service departmentB
vars.notification["mail_contacts"].groups += "departmentB"

Result:
[2021-03-23 10:12:12 +0100] critical/config: Error: Validation failed for object ‘xxx!mail-Service’ of type ‘Notification’; Attribute ‘user_groups’: Object ‘departmentAdepartmentB’ of type ‘UserGroup’ does not exist.

As it seems nothing can be additional added to a array from a Host/Service var-Variable / Array. Or is there syntax for this case?

Many thanks in advance!

Best regards
David

Whoops, my bad.

vars.notification["mail_contacts"].groups += [ "departmentB" ]
1 Like

Its working with:
vars.notification["mail_contacts"].groups += [ "departmentB" ]

And changed apply rule to:

apply Notification "mail-service" to Service {
  import "mail-service-notification-template"

  user_groups += service.vars.notification.mail_contacts.groups

  assign where service.vars.notification.mail_contacts.groups
}

Result on IcingaWeb2:

Custom Variables

  • mail_contacts => {
    • groups =>
      • departmentA
      • departmentB}|

Thank you ritzgu! This is the solution.

1 Like