diff --git a/CHANGELOG.md b/CHANGELOG.md index f819905..c5f61fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/) ## [Unreleased] +### Added + +- add dependencies + ### Fixed - bug in user template diff --git a/README.md b/README.md index 9dd06c4..c863e33 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/defaults/main.yml b/defaults/main.yml index 483563e..d15d498 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -107,4 +107,5 @@ icinga2_users: {} icinga2_api_users: {} icinga2_check_commands: {} icinga2_event_commands: {} +icinga2_dependencies: {} icinga2_scripts: {} diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 5d55e65..ccb064c 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -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" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 0507c53..58a453d 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -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 diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py index 748c11f..bbc2322 100644 --- a/molecule/default/tests/test_default.py +++ b/molecule/default/tests/test_default.py @@ -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 diff --git a/tasks/config.yml b/tasks/config.yml index 2bf4f12..56e5f63 100644 --- a/tasks/config.yml +++ b/tasks/config.yml @@ -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 diff --git a/templates/dependencies.conf.j2 b/templates/dependencies.conf.j2 new file mode 100644 index 0000000..a63f8ed --- /dev/null +++ b/templates/dependencies.conf.j2 @@ -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 %}