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
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
I think this is not possible 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
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.
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",host.name) ) {
vars.myvar = host.vars.myvar
}
}