Host Custom Variable value to populate in Service Template Custom Variable


I have a host template populated with Environment variable while onboarding.
So for example a server can be classified u_Environment = “Prod”

Now I want to populate the same value on all the single service deployed in the same server

Something like this

object Host "ABCDEFG-Host" {
import "DtmplHost-AG-NT"

display_name = "ABCDEFG-Host"
address = "XX.XX.11.X"
groups = [ "DWindows_All" ]
vars["_override_servicevars"] += {
    "DP_NT_CPUBusy" = {
        u_Environment = "Prod"
vars.u_Environment = "Prod"
vars.u_OperatingSystem = "Windows"


My host template and service template has same value
Can we do something like service.u_Environment = host.$u_Environment$
Hard code it at service template itself and automatically the value will be picked? Or is there a way I can do it on 1000s of service looking at 1000s of servers. Note using director

(Not sure if I understood you correctly) You can use a host variable in your service definition(s) in this way:

vars.anyvariable = $host.vars.u_Environment$


assign where host.vars.u_OperatingSystem == “Windows”

All right - let me try to explain

A host is onboarded using director sync method and a custom variable is also populated during onboarding about the Environment (CustomEnvironment -> u_Environment)
Now Because it is part of host attribute I can push this information on hostalive alert that the server is lets say Prod

Now I want to push the same information on all the services distributed on the same server. So lets say CPU Monitoring on same host. I want to automatically pupulate the information of in custom variable that it is Prod

So As you said I did ->

but I got this :stuck_out_tongue:

I think this is not possible :frowning: it would have been great. If we can get and populate a host variable during on-boarding and same can be populated on services on the same server - could have made our life easy

HI @radioactive9,

host.vars.u_Environment without the dollar signs should work. But note that this is only possible if you use apply rules for your services and not single service objects.


ohh!! Thanks for your response.

Host Object

Has environment as Prod

But Single Service

Service Template as below assigned

:frowning: I am on all single service But really it would have been a great feature to have


I’m doing something similar, but not based on templates.

From what i’v understood so far, templates for Host and Service Object are evaluated and resolved by the compiler in a way they cant directly interact with each others, so it would make it hard to replicate vars from one to another (maybe using reference would work ? didn’t tried so far).

However, you can replicate vars from host to service object in this way :

apply Service "myservice" {
    vars.myvar = host.vars.myvar

So, to sum it up, if you set your variable in your host template and add this to the service you are interested in, it should work as intended.
However, if you need more flexibility for some reason, you can affect the var dynamically based on conditions, example :

apply Service "myservice" {
    if ( regex("pattern", ) {
    vars.myvar = host.vars.myvar