Different var syntax
Hi,
I want to create an object file like this:
apply Service for (hdisk => config in host.vars.disks) {
import "generic-service"
check_command = "disk"
command_endpoint = host.name
**vars += config**
vars.disk_wfree = "10%"
vars.disk_cfree = "5%"
assign where "config.disk"
}
I think its not possible, or does anybody knows a way?
icinga2_var:
domain.de:
- name: disk
type: Service
imports:
- generic-service
apply: true
apply_for: hdisk => config in host.vars.disks
file: 'zones.d/global-templates/services.conf'
check_command: disk
command_endpoint: host.name
vars:
- disk_wfree: "10%"
- disk_cfree: "5%"
assign: config.disk
Edit: Better readability
Hi,
could you explain what exactly you try to achieve? I'm not sure whether the vars += config part is the relevant one.
Also, how does host.vars.disks look like at your host? Is it an actual array, a dictionary, etc.?
Please try to use code blocks and proper indentation next time to make everything more readable for people who'd like to help :)
Small example achieving vars += config:
vars:
icinga2_objects:
- name: host1
type: Host
file: "local.d/testing.conf"
address: "localhost"
check_command: "hostalive4"
vars:
disks:
- "disk1"
- "disk2"
- "disk3"
- name: Disk
type: Service
file: "local.d/testing.conf"
apply: true
apply_for: hdisk => config in host.vars.disks
check_command: disk
vars: + config
assign:
- host.vars.disks
The above results in the following
object Host "host1" {
address = "localhost"
check_command = "hostalive4"
vars.disks = [ "disk1", "disk2", "disk3", ]
}
apply Service for (hdisk => config in host.vars.disks) {
check_command = "disk"
vars += config
assign where host.vars.disks
}
As you can probably tell, you can't have both vars += config and vars.foo = "bar" at the same time (as far as I can tell).
Example achieving vars.foo = "bar"
vars:
icinga2_objects:
- name: host1
type: Host
file: "local.d/testing.conf"
address: "localhost"
check_command: "hostalive4"
vars:
disks:
- "disk1"
- "disk2"
- "disk3"
- name: Disk
type: Service
file: "local.d/testing.conf"
apply: true
apply_for: config in host.vars.disks
check_command: disk
vars:
foo: "bar"
disk_partitions: config
disk_wfree: "10%"
assign:
- host.vars.disks
->
object Host "host1" {
address = "localhost"
check_command = "hostalive4"
vars.disks = [ "disk1", "disk2", "disk3", ]
}
apply Service for (config in host.vars.disks) {
check_command = "disk"
vars.foo = "bar"
vars.disk_partitions = config
vars.disk_wfree = "10%"
assign where host.vars.disks
}
Hi. Thank you for your answer.
What I want to achieve is that we use the configuration from host.
vars += config
and overwrite 2 values by default:
vars.disk_wfree = "10%"
vars.disk_cfree = "5%"
With Icinga it is working. But there is no way with the collection I think
Hm, this sounds tricky or even 'impossible'.
If I define a dictionary key twice, the last one overwrites the previous ones.
my_dict:
varname: "first value"
varname: "second value"
my_dict.varname = "second value"
To implement vars += config followed by vars.some_key = "some value" we'd need to define
vars: + config
vars:
some_key: "some value"
This is not possible AFAIK.
You could define your defaults in a template an import it to your assign rule. Then the Icinga mechanics will solve the problem for you.
Just use
imports:
- mydefault-template