Macro is "not a callable object"

This one has me scratching my head:

object Host "somehost" {
  check_command = "dummy"
  vars.dummy_text = {{
    var host_name = macro("$$")
    var critical_services = ["api", "cockpit", "home", "console", "shell"]
    var crit_names = []
    var warn_names = []
    for (svc in critical_services) {
      svc_state = get_service(host_name, svc).state
      if (svc_state == 2) {
      } else if (svc_state == 1) {
    var ok_names = critical_services - (crit_names + warn_names)
    var ok_text = "OK Services: " + ok_names.join(", ")
    var warn_text = "Warning Services: " + warn_names.join(", ")
    var crit_text = "CRITICAL Services: " + crit_names.join(", ")
    var status_text = ok_text + "\n" + warn_text + "\n" + crit_text + "\n" + host_name
    return status_text

And if I then call this on the cli:

Location: in /etc/icinga2/zones.d/somehost/somehost.conf: 31:21-31:40
/etc/icinga2/zones.d/somehost/somehost.conf(30):   vars.dummy_text = {{
/etc/icinga2/zones.d/somehost/somehost.conf(31):     var host_name = macro("$$")
/etc/icinga2/zones.d/somehost/somehost.conf(32):     var critical_services = ["api", "cockpit", "home", "console", "shell"]
/etc/icinga2/zones.d/somehost/somehost.conf(33):     var crit_names = []
Argument is not a callable object.

If I refactor it using regular lambda syntax with use, it works, eg.

vars.dummy_text = () use(name) => { var host_name = name....}

I’ve triple-checked the docs on macro to make sure that I can call it from an abbreviated lambda that’s used for assigning to a custom variable. I’ve used macro in a Service’s custom variable lambda without issue, and the docs show plenty of examples of this being done on a Host… But somehow, this one is not callable?

Am I missing something?

what for the “var” before each variable definition?

this works fine for me:

  vars = {
    state_severity = {{ return macro("$state_" + macro("$state$") + "_severity$") }}

I’m seeing that same problem. For me it manifests itself by erring out in the script debugger, then also the first time icinga tries to execute it, but then after a time it works.

Almost as if the first time around an import of the library that provides the macro() function is missing.

Your refactoring to an ordinary lambda saved the day, now I have a workaround that works every time.

1 Like