Skip to content

Commit ac7e05d

Browse files
widhalmtDonien
andauthored
Add a bit more explanation on how to create host objects via collection (#415)
Add more explanation on how to create host objects via collection --------- Co-authored-by: Donien <[email protected]>
1 parent 0e257ee commit ac7e05d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

doc/role-icinga2/objects.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ The `type` will be the original Icinga 2 object types, a list of all can be foun
2222
When defining `icinga2_objects` as a host specific variable (hostvars/groupvars) you can define the variable as a dictionary. Each dictionary key represents the host on which the key's value will be deployed as configuration.<br>
2323
Alternatively you can define `icinga2_objects` as a list which results in the configuration being deployed on just the host for which the variable is defined.
2424

25+
Because it's easily overlooked, here are few more words of clarification: The first example shows variables of the host `webserver.example.org` but the `icinga2_objects` variable includes `host.example.org` and an extra level of indentation. This means that the following configuration will not be deployed on `webserver.example.org` but on `host.example.org`. This is especially useful when defining host objects on agents but "sending" them to the central nodes.
26+
2527
Example defining the variable within hostvars as a dictionary (inventory entry):
2628

2729
```yaml
@@ -40,6 +42,45 @@ webserver.example.org:
4042
4143
This way you can use some host's variables (like `ansible_host`) to deploy configuration on another host (in this case `host.example.org`).
4244

45+
You can use this variant within a playbook where `host.example.org` is your central Icinga 2 instance. Run it on all agents. They will collect their local facts and the central system `host.example.org` will receive host objects for each agent. This approach will only work if you run the play simultanously on your agents and on your central system.
46+
47+
```
48+
- hosts: icinga2_main:icinga2_agents
49+
collections:
50+
- netways.icinga
51+
pre_tasks:
52+
# This assumes that agents don't have 'icinga2_objects' defined at hostvars level
53+
# 'set_fact' will override them!
54+
- name: Add agent hosts to Icinga 2 objects
55+
when: inventory_hostname in groups["icinga2_agents"]
56+
set_fact:
57+
icinga2_objects:
58+
# Name of the central system on which to deploy the given objects
59+
host.example.org:
60+
- name: "{{ inventory_hostname }}"
61+
type: Zone
62+
file: "zones.d/{{ monitoring_zone }}/hosts/{{ inventory_hostname }}.conf"
63+
endpoints:
64+
- "{{ inventory_hostname }}"
65+
parent: "{{ monitoring_zone }}"
66+
- name: "{{ inventory_hostname }}"
67+
type: Endpoint
68+
file: "zones.d/{{ monitoring_zone }}/hosts/{{ inventory_hostname }}.conf"
69+
- name: "{{ inventory_hostname }}"
70+
type: Host
71+
file: "zones.d/{{ monitoring_zone }}/hosts/{{ inventory_hostname }}.conf"
72+
imports:
73+
- linux-host
74+
vars:
75+
distro: "{{ ansible_distribution }}"
76+
distroversion: "{{ ansible_distribution_version }}"
77+
arch: "{{ ansible_architecture }}"
78+
address: "{{ ansible_default_ipv4.address }}"
79+
roles:
80+
- netways.icinga.icinga2
81+
```
82+
83+
> You could also add `icinga2_objects` to your agents's host variables within your inventory instead.
4384
Example defining the variable within hostvars as a list (inventory entry):
4485
4586
```yaml

0 commit comments

Comments
 (0)