voxpupuli / puppet-icinga2

Puppet module to manage Icinga 2
https://forge.puppet.com/icinga/icinga2
Apache License 2.0
61 stars 94 forks source link

How to add custom variables to a template? #710

Closed Napsty closed 2 years ago

Napsty commented 2 years ago

Context

Icinga2 supports the definition of custom variables in the templates, for example host templates. These custom vars are then automatically applied on all the Hosts using this host template.

template Host "generic-host" {
  check_command = "hostalive_4"
  max_check_attempts = 3
  check_interval = 1m
  retry_interval = 30s
  vars.custom_var = "Feuerstein"
}

But how can this be added with the Puppet module?

Expected Behavior

Adding the custom variable to the host template "generic-host", configured in Puppet:

profile::icinga2master::icinga2_object_host:
  generic-host:
    check_command: hostalive_4
    check_interval: 1m
    max_check_attempts: 3
    retry_interval: 30s
    vars.custom_var: 'Feuerstein'
    target: /etc/icinga2/conf.d/templates.conf
    template: true

Results in the following template in the final config file (/etc/icinga2/conf.d/templates.conf):

template Host "generic-host" {
  check_command = "hostalive_4"
  max_check_attempts = 3
  check_interval = 1m
  retry_interval = 30s
  vars.custom_var = "Feuerstein"
}

Current Behavior

Puppet run falls on its nose, stating that:

Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement, Icinga2::Object::Host[generic-host]: has no parameter named 'vars.custom_var' (file: /etc/puppetlabs/code/environments/production/site/profile/manifests/icinga2master.pp, line: 35) on node icinga2
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

I also tried it with vars.custom_var: '-:Feuerstein' but same error.

Your Environment

Thanks for any hints.

lbetz commented 2 years ago

Hi Claudio,

vars has to be a hash. So your yaml code must look like that:

profile::icinga2master::icinga2_object_host:
  generic-host:
    check_command: hostalive_4
    check_interval: 1m
    max_check_attempts: 3
    retry_interval: 30s
    vars
      custom_var: Feuerstein
    target: /etc/icinga2/conf.d/templates.conf
    template: true
Napsty commented 2 years ago

@lbetz Lennart, as always, you're faster than I documenting the current problem internally ;-). Will try this!

lbetz commented 2 years ago

I am on vacation at home.

On 21. Sep 2022, at 09:37, Claudio Kuenzler @.***> wrote:

@lbetz https://github.com/lbetz Lennart, as always, you're faster than I documenting the current problem internally ;-). Will try this!

— Reply to this email directly, view it on GitHub https://github.com/Icinga/puppet-icinga2/issues/710#issuecomment-1253323270, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAOULIVOZSWYJEN33T4CXDV7K3NBANCNFSM6AAAAAAQRYQOEE. You are receiving this because you were mentioned.

Napsty commented 2 years ago

Working, thanks for the quick solution!