# Dynamic assign/ignore where clauses?

**URL:** <https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217>\
**Category:** Icinga 2\
**Created:** [February 5, 2024, 3:31pm UTC](https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217 "2024-02-05T15:31:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bofhdk](https://community.icinga.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@bofhdk](https://community.icinga.com/u/bofhdk)\
**Post date:** [February 5, 2024, 3:31pm UTC](https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217/1 "2024-02-05T15:31:18Z")

</div>

I’m trying to convince Icinga to handle things slightly more intelligently, however this has me stumped:

```auto
object Host "some.host.name" {
    // other config lines here
    groups += ["windows"]
    vars.winsvcs += [
        "A Windows Service",
        "Another Windows Service",
    ]
}

```

```auto
// 7 lines of header comments

apply Service "winsvc" for (winsvc in host.vars.winsvcs) {
    import "generic-service"
    check_command ="snmp-service"
    display_name = winsvc
    vars.snmp_service_name = winsvc
    vars.snmp_retries = 2
    vars.snmp_timeout = 15

    var knownServices = get_services(host.name).map(X=>X.name)
    assign where match("*"+winsvc, this.knownServices, MatchAny) == false
    ignore where match("*"+winsvc, this.knownServices, MatchAny)
}

```

the result:

```auto
[2024-02-05 15:35:44 +0100] critical/config: Error: An object with type 'Service' and name 'some.host.name!winsvcA Windows Service' already exists (in /etc/icinga2/zones.d/global-templates/Services/winsvcs.conf: 9:1-9:56), new declaration: in /etc/icinga2/zones.d/global-templates/Services/winsvcs.conf: 9:1-9:56
Location: in /etc/icinga2/zones.d/global-templates/Services/winsvcs.conf: 9:1-9:56
/etc/icinga2/zones.d/global-templates/Services/winsvcs.conf(7): #=- ---------------------------------------------------------------------------
/etc/icinga2/zones.d/global-templates/Services/winsvcs.conf(8):
/etc/icinga2/zones.d/global-templates/Services/winsvcs.conf(9): apply Service "winsvc" for (winsvc in host.vars.winsvcs) {
                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/etc/icinga2/zones.d/global-templates/Services/winsvcs.conf(10): import "generic-service"
/etc/icinga2/zones.d/global-templates/Services/winsvcs.conf(11): check_command ="snmp-service"

```

Expected result: no complaints about services already being defined as the assign/ignore clauses were expected to take care of that issue.

I pulled the get\_services(…) call into a separate var as I was unsure whether or not match(…) could handle it.

Using the console I’ve verified that the get\_services(…) call indeed returns an array, which I’d expect was fine:

```auto
<1> => match("*A Windows Service",get_services("some.host.name").map(X=>X.__name),MatchAny)
true
<2> =>

```

and

```auto
<1> => winsvc="A Windows Service"
null
<2> => match("*"+winsvc,get_services("some.host.name").map(X=>X.__name),MatchAny)
true

```

Have I missed something or am I simply expecting too much of DSL?

---

<div class="post-metadata">

**Author:** ![Dirk](https://community.icinga.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Dirk](https://community.icinga.com/u/Dirk)\
**Post date:** [February 12, 2024, 7:07am UTC](https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217/2 "2024-02-12T07:07:03Z")

</div>

Hi bofhdk,  
personally I don’t use that assign stuff, we follow another aproach. But maybe some thoughts:

- Are you sure the service isn’t assigned via another rule already?
- If “assign where” has the rule “== false”, shouldn’t the “ignore where” part have the rule “!= false”?

Regards,  
Dirk

---

<div class="post-metadata">

**Author:** ![bofhdk](https://community.icinga.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@bofhdk](https://community.icinga.com/u/bofhdk)\
**Post date:** [February 12, 2024, 7:38am UTC](https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217/3 "2024-02-12T07:38:40Z")

</div>

Hi Dirk,

I’m quite sure that this is the only place that this gets assigned. A recursive grep of the configuration seems to support this as well 🙂

As I understand the (slightly cryptic) documentation, match() returns a boolean so the ‘== false’ is merely a substitute for ‘assign where !match(…)’ as I wasn’t sure that this oparticular notation would work.

/b

---

<div class="post-metadata">

**Author:** ![log1c](https://community.icinga.com/user_avatar/community.icinga.com/log1c/32/2071_2.png) [@log1c](https://community.icinga.com/u/log1c)\
**Post date:** [February 20, 2024, 3:07pm UTC](https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217/4 "2024-02-20T15:07:00Z")

</div>

May I ask what you hope to achieve with this approach?  
Because I can’t see any benefit.

Are you trying to circumvent Icingas “redfined” detection, in case someone defines the the same service name twice?

---

<div class="post-metadata">

**Author:** ![bofhdk](https://community.icinga.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@bofhdk](https://community.icinga.com/u/bofhdk)\
**Post date:** [February 20, 2024, 3:35pm UTC](https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217/5 "2024-02-20T15:35:20Z")

</div>

@log1c : Not quite. I’m trying to get Icinga to only apply the service once for any given host. It seems that it might be attempting to assign the same definition twice and that is causing it to break.

Since the error message is rather unhelpful (no surprise!) I’d like for the second attempt to merely fail (possibly with a warning) but for Icinga to continue.

---

<div class="post-metadata">

**Author:** ![log1c](https://community.icinga.com/user_avatar/community.icinga.com/log1c/32/2071_2.png) [@log1c](https://community.icinga.com/u/log1c)\
**Post date:** [February 21, 2024, 7:05am UTC](https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217/6 "2024-02-21T07:05:51Z")

</div>

> [@bofhdk](#):
>
> I’m trying to get Icinga to only apply the service once for any given host

That is what the apply for does anyway. It deploys one check for each entry in an array.  
Why your config tries to define it twice, I don’t really get, tbh.

Example (from a Director config) that works

```auto
apply Service for (config in host.vars.m365_avepoint_booked_backupservices) {
    name = "avepoint-backup-health_" + config
    import "m365-avepoint-backup-health"

    assign where "m365-baas-dummy-hosts" in host.templates
    vars.m365_avepoint_jobmodule = config

    import DirectorOverrideTemplate
}

```

---

<div class="post-metadata">

**Author:** ![Al2Klimov](https://community.icinga.com/user_avatar/community.icinga.com/al2klimov/32/959_2.png) [@Al2Klimov](https://community.icinga.com/u/Al2Klimov)\
**Post date:** [March 12, 2024, 11:28am UTC](https://community.icinga.com/t/dynamic-assign-ignore-where-clauses/13217/7 "2024-03-12T11:28:50Z")

</div>

Hello @bofhdk!

> [@bofhdk](#):
>
> I pulled the get\_services(…) call into a separate var as I was unsure whether or not match(…) could handle it.

I’m afraid you have to do exactly the opposite – to inline the get\_services call.

Best,  
A/K
