feat: add dependencies

This commit is contained in:
Adrien Waksberg 2022-11-09 14:11:22 +01:00
parent 32911bac43
commit e2ed6fb9b5
8 changed files with 65 additions and 12 deletions

View file

@ -5,6 +5,10 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/)
## [Unreleased]
### Added
- add dependencies
### Fixed
- bug in user template

View file

@ -148,6 +148,19 @@ Install and configure Icinga2
monday: 00:05-00:10
```
- `icinga2_dependencies` - hash with dependencies configuration ([view all options](https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#dependency))
```
postgresql-port:
parent_host_name: host.name
parent_service_name: postgresql-service
child_service_name: postgresql-port
disable_notifications: true
states:
- DOWN
assign: '"postgresql" in host.groups'
```
- `icinga2_notifications` - hash with notifications configuration
```

View file

@ -107,4 +107,5 @@ icinga2_users: {}
icinga2_api_users: {}
icinga2_check_commands: {}
icinga2_event_commands: {}
icinga2_dependencies: {}
icinga2_scripts: {}

View file

@ -50,3 +50,8 @@
icinga2_event_commands:
ping_event:
command: '"/usr/bin/ping -c 1 -t 1 127.0.0.1"'
icinga2_dependencies:
test:
parent_host_name: host.name
disable_notifications: false
assign: "true"

View file

@ -2,14 +2,6 @@
driver:
name: docker
platforms:
- name: debian10
image: nishiki/debian10:molecule
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
command: /bin/systemd
capabilities:
- SYS_ADMIN
- name: debian11
image: nishiki/debian11:molecule
privileged: true

View file

@ -77,6 +77,15 @@ def test_config_notifications(host):
assert config.mode == 0o640
assert config.contains('user_groups = host.vars.notification.mail.groups')
def test_config_dependencies(host):
config = host.file('/etc/icinga2/conf.d/dependencies.conf')
assert config.exists
assert config.is_file
assert config.user == 'root'
assert config.group == 'nagios'
assert config.mode == 0o640
assert config.contains('parent_host_name = host.name')
def test_script(host):
path = host.file('/etc/icinga2/scripts/test-notification.sh')
assert path.exists

View file

@ -13,7 +13,7 @@
owner: root
group: nagios
mode: 0750
loop: '{{ icinga2_scripts|dict2items }}'
loop: '{{ icinga2_scripts | dict2items }}'
loop_control:
label: '{{ item.key }}'
tags: icinga2
@ -28,6 +28,7 @@
loop:
- check_commands
- commands
- dependencies
- groups
- hosts
- notifications
@ -112,7 +113,7 @@
owner: root
group: nagios
mode: 0640
loop: '{{ icinga2_features|dict2items }}'
loop: '{{ icinga2_features | dict2items }}'
loop_control:
label: '{{ item.key }}'
notify: restart icinga2
@ -121,8 +122,8 @@
- name: manage features
community.general.icinga2_feature:
name: '{{ item.key }}'
state: '{{ item.state|default("present") }}'
loop: '{{ icinga2_features|dict2items }}'
state: '{{ item.state | default("present") }}'
loop: '{{ icinga2_features | dict2items }}'
loop_control:
label: '{{ item.key }}'
notify: restart icinga2

View file

@ -0,0 +1,28 @@
# {{ ansible_managed }}
{% for name, dependency in icinga2_dependencies.items() %}
apply Dependency "{{ name }}" to Service {
{% if dependency.parent_host_name is defined %}
{% if dependency.parent_host_name == "host.name" %}
parent_host_name = host.name
{% else %}
parent_host_name = {{ dependency.parent_host_name }}
{% endif %}
{% endif %}
{% for option in ['parent_service_name', 'child_host_name', 'child_service_name' 'period'] %}
{% if dependency[option] is defined %}
{{ option }} = "{{ dependency[option] }}"
{% endif %}
{% endfor %}
{% for option in ['disable_checks', 'disable_notifications', 'ignore_soft_states'] %}
{% if dependency[option] is defined %}
{{ option }} = {{ dependency[option] | ternary("true", "false") }}
{% endif %}
{% endfor %}
{% if dependency.states is defined %}
states = [ {{ value | join(', ') }} ]
{% endif %}
assign where {{ dependency.assign }}
}
{% endfor %}