Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different var syntax #310

Open
telution opened this issue Jul 29, 2024 · 4 comments
Open

Different var syntax #310

telution opened this issue Jul 29, 2024 · 4 comments

Comments

@telution
Copy link

telution commented Jul 29, 2024

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

@Donien
Copy link
Collaborator

Donien commented Jul 31, 2024

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
}

@telution
Copy link
Author

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

@Donien
Copy link
Collaborator

Donien commented Jul 31, 2024

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.

@mkayontour
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants