How could I override some object attributes (eg. Service.enable_active_checks) in an apply rule based on the presence or absence of a variable?
Example, I want to follow the documentation “apply Service for” pattern to define services and I want to customise display names and turn off active checks for a specific one:
object Host "myhost" {
import "generic-host"
address = "127.0.1.4"
vars.interfaces["eth0"] = {
vlan = "internal"
display_name = "LAN interface"
}
vars.interfaces["eth1"] = {
vlan = "remote"
display_name = "WAN interface"
enable_active_checks = false
}
}
The best I came up with is standardising my apply for
rules to use a common config
name and checking its keys for variable presence, then explicitly handling them one by one:
template Service "custom-vars-service" {
if ( "display_name" in keys(config) ) { display_name = config.display_name }
if ( "enable_active_checks" in keys(config) ) { enable_active_checks = config.enable_active_checks }
if ( "enable_passive_checks" in keys(config) ) { enable_passive_checks = config.enable_passive_checks }
if ( "notes" in keys(config) ) { notes = config.notes }
}
apply Service for (my_svc => config in host.vars.my_svc) {
import "custom-vars-service"
# ....
}
Is there any better way?
Is there any downside to my approach that I’m not seeing right now?
thanks for feedback