# Use vars in host display\_name

**URL:** <https://community.icinga.com/t/use-vars-in-host-display-name/7405>\
**Category:** Icinga 2\
**Created:** [May 22, 2021, 4:46pm UTC](https://community.icinga.com/t/use-vars-in-host-display-name/7405 "2021-05-22T16:46:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdhgdr32](https://community.icinga.com/letter_avatar_proxy/v4/letter/g/6bbea6/32.png) [@gdhgdr32](https://community.icinga.com/u/gdhgdr32)\
**Post date:** [May 22, 2021, 4:46pm UTC](https://community.icinga.com/t/use-vars-in-host-display-name/7405/1 "2021-05-22T16:46:27Z")

</div>

I’m trying to dynamically generate the display\_name value for Hosts in my environment. Ideally I’d like to do this using the vars that I have set on that host. I’ve attempted to accomplish this in a handful of ways, but nothing seems to work (though I wouldn’t be surprised if I’m doing it wrong). I’ve included a few of the attempts (but there were many more) in my example host below. My latest attempt was the uncommented bit, which resulted in a pass through the config checker, but the display\_name value shows up as " **Object of type ‘Function’**" in the Icinga GUI

I’ve seen examples for Services where you can do this very simply (E.G., display\_name = $host.vars.customer$ + $host.vars.host\_type$) so I’m lost as to why that doesn’t work for Hosts. Is this just not possible or is there a different way to accomplish this?

**Example host:**  
object Host “dcn\_dcnproddeploy01” {  
import “dcn-hdos-splunk-host”  
vars.splunk\_roles = [“Deployment Server”, “License Master”]  
vars.aliases = [“ccnproddeploy01.bla”]  
vars.hl\_splunk\_gui\_url = “[https://ccnproddeploy01.bla](https://ccnproddeploy01.bla):$vars.splunk\_port$”  
vars.client\_splunk\_gui\_url = “[https://ccnproddeploy01.bla](https://ccnproddeploy01.bla):$vars.splunk\_port$”  
vars.site\_location\_identifier = “NA”  
vars.cluster\_number\_identifier = “1”  
display\_name = {{  
var test;  
var r = macro("$host.vars.customer$");  
test = r.to\_string();  
return test  
}}  
address = “1.2.3.4”  
#display\_name = $host.vars.customer$ + $host.vars.host\_type$  
#display\_name = “$host.vars.customer$” + “$host.vars.host\_type$”  
#display\_name = $host.vars.customer$ $host.vars.site\_location\_identifier$ $host.vars.host\_type$ $host.vars.splunk\_roles$ $host.vars.cluster\_number\_identifier$  
#display\_name = {{ $host.vars.aliases$ }}  
}

**The import:**  
template Host “dcn-hdos-splunk-host” {  
import “generic-host”  
vars.os\_type = “hdos”  
vars.host\_type = “Splunk”  
vars.customer = “DCN”  
vars.org\_name = “dcn”  
vars.has\_nrpe = true  
vars.team = “splunk”  
}

- root@:~# icinga2 --version  
icinga2 - The Icinga 2 network monitoring daemon (version: r2.12.3-1)  
Platform: Ubuntu  
Platform version: 18.04.5 LTS (Bionic Beaver)
- Enabled features: api-users api command compatlog ido-mysql mainlog notification statusdata

---

<div class="post-metadata">

**Author:** ![yhabteab](https://community.icinga.com/letter_avatar_proxy/v4/letter/y/ac91a4/32.png) [@yhabteab](https://community.icinga.com/u/yhabteab)\
**Post date:** [May 22, 2021, 7:17pm UTC](https://community.icinga.com/t/use-vars-in-host-display-name/7405/2 "2021-05-22T19:17:21Z")

</div>

Hi @gdhgdr32 , welcome 🤗 to the community.

Have you also tried the following approach? I have tried it on my system and it works quite well 😌 I think.

```bash
  display_name = this.vars.customer + " " + this.vars.host_type

```

Best,  
Yonas

---

<div class="post-metadata">

**Author:** ![gdhgdr32](https://community.icinga.com/letter_avatar_proxy/v4/letter/g/6bbea6/32.png) [@gdhgdr32](https://community.icinga.com/u/gdhgdr32)\
**Post date:** [May 23, 2021, 2:27pm UTC](https://community.icinga.com/t/use-vars-in-host-display-name/7405/3 "2021-05-23T14:27:13Z")

</div>

You’re a life saver Yonas, thank you so much, that’s exactly what I needed. I couldn’t even find the existence of “this.” in the docs.

---

<div class="post-metadata">

**Author:** ![Pooh](https://community.icinga.com/letter_avatar_proxy/v4/letter/p/f05b48/32.png) [@Pooh](https://community.icinga.com/u/Pooh)\
**Post date:** [May 23, 2021, 3:01pm UTC](https://community.icinga.com/t/use-vars-in-host-display-name/7405/4 "2021-05-23T15:01:58Z")

</div>

Indeed - that’s a nice way of getting at the object’s variables.

I, too, had never noticed the use of “this” in the documentation, but after  
having a pretty thorough search around, I’ve found a reference to “the ‘this’  
scope” for variable names:

[Language Reference - Icinga 2](https://icinga.com/docs/icinga-2/latest/doc/17-language-reference/)  
#variable-scopes

I can’t help feeling that this should be repeated and emphasised, for example  
in the Advanced Topics section, possibly alongside Apply Rules.

Antony.

---

<div class="post-metadata">

**Author:** ![yhabteab](https://community.icinga.com/letter_avatar_proxy/v4/letter/y/ac91a4/32.png) [@yhabteab](https://community.icinga.com/u/yhabteab)\
**Post date:** [May 26, 2021, 5:32pm UTC](https://community.icinga.com/t/use-vars-in-host-display-name/7405/5 "2021-05-26T17:32:11Z")

</div>

Hii @Pooh,

If you don’t want to explicitly call the `this` keyword, you can also do it that way `vars.customer + vars.host_type`, because it’s exactly the same. Therefore, it would be superfluous to document the `this` keyword, whereas it can be done without it.
