Adding groups through templates

Hello,

i found out that groups assinged via templates overwrite each other.

For example: I have 3 templates which assinging hostgroups to a host.
only the 3rd group will be assigned to the host.
all other groups will be overwridden.

How can i fix this?
Do i understand the concept of hostgroups wrong?
Viper

There are several (long lasting) bug reports for this issue on the Icinga Director issue tracker, one example:

I’m not really sure whether the underlying problem is the Icinga 2 core itself or just the Icinga Director.
For now you have to work around this by assigning the host groups based on, e.g., variables, host names, ip addresses/subnets or add them manually.

The problem is with the handling of the additive assignments. I’m not sure how this can be solved in the Director, but the DSL requires certain tricks as well.

Here’s an example which just overrides everything.

template Host "t1" {
  groups = [ "hg1" ]
}

template Host "t2" {
  groups = [ "hg2" ]
}

template Host "t3" {
  groups = [ "hg3" ]
}

object Host "h" {
  import "t1"
  import "t2"
  import "t3"

  check_command = "dummy"
}

If you want to solve this by using array#add, this works just for groups in this specific case. This is because of the config compiler already knowing that groups is of the array type.

This method does not work with custom variables of the array type. This needs a manual initialization with an empty array, e.g. vars.services = [].

template Host "t1" {
  groups.add("hg1")
}

template Host "t2" {
  groups.add("hg2")
}

template Host "t3" {
  groups.add("hg3")
}

object Host "h" {
  import "t1"
  import "t2"
  import "t3"

  check_command = "dummy"
}

With using the trick of additive assignments solving

  • uninitialized array being initialized and adding the new value
  • given array, just adding the new element

this may work. But still, I’m not sure how the Director logic is implemented and how complicated changes could be incorporated to render this differently.

template Host "t1" {
  groups += [ "hg1" ]
}

template Host "t2" {
  groups += [ "hg2" ]
}

template Host "t3" {
  groups += [ "hg3" ]
}

object Host "h" {
  import "t1"
  import "t2"
  import "t3"

  check_command = "dummy"
}

You might link this topic into the GitHub URLs and discuss further over there.

Cheers,
Michael

@log1c: not really a working workaround. i run now into this issue:

@dnsmichi: i think it’s similar to this:

sorry for not looking for existing bugs.

Thank you for your answers.

Viper

Add your comments (and this topic) to the GitHub issue to let developers know about your concern, please.

Cheers,
Michael

@dnsmichi always at your service, master
verbeugend die bühne verlassend

1 Like