diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/.yamllint b/.yamllint new file mode 100644 index 0000000..5465b58 --- /dev/null +++ b/.yamllint @@ -0,0 +1,12 @@ +--- +extends: default + +ignore: | + .kitchen/* + vendor/ + +rules: + line-length: + max: 120 + level: warning + truthy: false diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..016b6d1 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +# CHANGELOG + +This project adheres to [Semantic Versioning](http://semver.org/). +Which is based on [Keep A Changelog](http://keepachangelog.com/) + +## [Unreleased] + +- first version diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..afe21bd --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2019 Adrien Waksberg + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md new file mode 100644 index 0000000..cf8a77f --- /dev/null +++ b/README.md @@ -0,0 +1,222 @@ +# Ansible role: Icinga2 + +Install and configure Icinga2 + +## Requirements + +* Ansible >= 2.9 +* Debian Buster + +## Role variables + +- `icinga2_db_type` - set the database type mysql or postgresql (default: `mysql`) +- `icinga2_ido_host` - set the database host (default: `127.0.0.1`) +- `icinga2_ido_database` - set the database name (default: `icinga2`) +- `icinga2_ido_user` - set the database user (default: `icinga2`) +- `icinga2_ido_password` - set the database password +- `icinga2_master` - set if the server is a master node (default: `false`) +- `icinga2_endpoints` - hash with the endpoint name and address + +``` + local.loc: 127.0.0.1 + satellite.loc: 10.121.30.20 +``` + +- `icinga2_zones` - hash with zone configuration + +``` + master: + endpoints: + - loca.loc + satellite: + parent: master + endpoints: + - satellite.loc +``` + +- `icinga2_templates` - hash with template configuration + +``` + generic-host: + type: host + vars: + os: Linux + options: + check_command: hostalive + check_interval: 1m + retry_interval: 30s + max_check_attempts: 3 + generic-service: + type: service + options: + max_check_attempts: 5 + check_interval: 1m + retry_interval: 30s +``` + +- `icinga2_service_groups` - hash with service group configuration + +``` + smnp: + display_name: SMNP + match: 'snmp-*' +``` + +- `icinga2_services` - hash with service configuration + +``` + ping: + check_command: ping + assign: host.address + ssh: + check_command: ssh + assign: (host.address || host.address6) && host.vars.os == "Linux" + vars: + timeout: 5 +``` + +- `icinga2_host_groups` - hash with host group configuration + +``` + tomcat: + assign: host.vars.tomcat_port + influxdb: + display_name: InfluxDB + assign: match("influx-*", host.name) +``` + +- `icinga2_hosts` - hash with host configuration + +``` + localhost: + template: generic-host + address: 127.0.0.1 + vars: + dist: Debian +``` + +- `icinga2_user_groups` - hash with user group configuration + +``` + admin: + display_name: Administrateur +``` + +- `icinga2_users` - hash with the user configuration + +``` + johndoe: + display_name: John Doe + email: john@doe.loc + groups: + - admin +``` + +- `icinga2_api_users` - hash with the user configuration for the API access + +``` + icingaweb2: + password: secret + permissions: + - '*' +``` + +- `icinga2_timeperiods` - hash with timeperiods configuration + +``` + 24x7: + ranges: + monday: 00:00-24:00 + tuesday: 00:00-24:00 + wednesday: 00:00-24:00 + thursday: 00:00-24:00 + friday: 00:00-24:00 + saturday: 00:00-24:00 + sunday: 00:00-24:00 + excludes: + monday: 00:05-00:10 + default_exclude: + prefer_includes: false + ranges: + monday: 00:00-01:00 + includes: + monday: 00:05-00:10 +``` + +- `icinga2_notifications` - hash with notifications configuration + +``` + mail-icingaadmin-host: + type: Host + import: mail-host-notification + assign: host.vars.notification.mail + user_groups: host.vars.notification.mail.groups + users: host.vars.notification.mail.users + mail-icingaadmin-service: + type: Service + import: mail-service-notification + assign: host.vars.notification.mail + user_groups: host.vars.notification.mail.groups + users: host.vars.notification.mail.users + options: + period: $service.vars.period$ +``` + +- `icinga2_check_commands` - hash with check command configuration + +``` + ping-ha: + command: '"/bin/ping"' + arguments: + packet: + value: 10 + required: true + vars: + timeout: 10 +``` + +- `icinga2_event_commands` - hash with event command configuration (see check command config) +- `icinga2_features` - hash with the features to enable and thier configuration + +``` + api: + object: ApiListener + config: + accept_config: false + accept_commands: true +``` + +## How to use + +``` +- hosts: server + roles: + - icinga2 +``` + +## Development + +### Test with molecule and docker + +* install [docker](https://docs.docker.com/engine/installation/) +* install `python3` and `python3-pip` +* install molecule and dependencies `pip3 install molecule molecule-docker docker ansible-lint testinfra yamllint` +* run `molecule test` + +## License + +``` +Copyright (c) 2019 Adrien Waksberg + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..56eedb8 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,78 @@ +--- +icinga2_db_type: mysql +icinga2_ido_host: 127.0.0.1 +icinga2_ido_database: icinga2 +icinga2_ido_user: icinga2 +icinga2_master: false +icinga2_endpoints: + localhost: 127.0.0.1 +icinga2_zones: + master: + endpoints: + - localhost +icinga2_templates: + generic-host: + type: host + vars: + os: Linux + options: + check_command: hostalive + check_interval: 1m + retry_interval: 30s + max_check_attempts: 3 + generic-service: + type: service + options: + max_check_attempts: 5 + check_interval: 1m + retry_interval: 30s +icinga2_services: + ping: + check_command: ping + assign: host.address + ssh: + check_command: ssh + assign: (host.address || host.address6) && host.vars.os == "Linux" +icinga2_hosts: + localhost: + template: generic-host + address: 127.0.0.1 +icinga2_timeperiods: + 24x7: + ranges: + monday: 00:00-24:00 + tuesday: 00:00-24:00 + wednesday: 00:00-24:00 + thursday: 00:00-24:00 + friday: 00:00-24:00 + saturday: 00:00-24:00 + sunday: 00:00-24:00 + 9to5: + ranges: + monday: 09:00-17:00 + tuesday: 09:00-17:00 + wednesday: 09:00-17:00 + thursday: 09:00-17:00 + friday: 09:00-17:00 + nevers: +icinga2_notifications: + mail-icingaadmin-host: + type: Host + import: mail-host-notification + assign: host.vars.notification.mail + groups: host.vars.notification.mail.groups + users: host.vars.notification.mail.users + mail-icingaadmin-service: + type: Service + import: mail-service-notification + assign: host.vars.notification.mail + groups: host.vars.notification.mail.groups + users: host.vars.notification.mail.users +icinga2_features: {} +icinga2_user_groups: {} +icinga2_host_groups: {} +icinga2_service_groups: {} +icinga2_users: {} +icinga2_api_users: {} +icinga2_check_commands: {} +icinga2_event_commands: {} diff --git a/handlers/main.yml b/handlers/main.yml new file mode 100644 index 0000000..781e22c --- /dev/null +++ b/handlers/main.yml @@ -0,0 +1,10 @@ +--- +- name: reload icinga2 + service: + name: icinga2 + state: reloaded + +- name: restart icinga2 + service: + name: icinga2 + state: restarted diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..5ed276f --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,19 @@ +--- +galaxy_info: + author: Adrien Waksberg + company: Adrien Waksberg + description: Install and configure Icinga2 + license: Apache2 + min_ansible_version: 2.9 + + platforms: + - name: Debian + versions: + - buster + + galaxy_tags: + - monitoring + - nagios + - icinga + +dependencies: [] diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml new file mode 100644 index 0000000..a6333bd --- /dev/null +++ b/molecule/default/converge.yml @@ -0,0 +1,45 @@ +--- +- name: Converge + hosts: all + roles: + - ansible-role-icinga2 + pre_tasks: + - name: update apt cache + apt: + update_cache: true + vars: + icinga2_ido_host: 127.0.0.1 + icinga2_ido_user: icinga + icinga2_ido_password: test + icinga2_ido_database: icinga + icinga2_services: + ping: + check_command: ping + assign: host.address + ssh: + check_command: ssh + assign: (host.address || host.address6) && host.vars.os == "Linux" + options: + event_command: ping_event + icinga2_hosts: + localhost: + template: generic-host + address: 127.0.0.1 + vars: + os: Linux + http_vhosts: + http: + http_vhosts: "/" + icinga: + http_vhosts: /icingaweb2 + icinga2_check_commands: + https: + command: '[PluginDir + "/check_http"]' + arguments: + - name: -H + value: $http_vhost$ + description: "Host name argument for servers using host headers (virtual host)" + required: true + icinga2_event_commands: + ping_event: + command: '"/usr/bin/ping -c 1 -t 1 127.0.0.1"' diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..cd99616 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,18 @@ +--- +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 +lint: | + set -e + yamllint . + ansible-lint +verifier: + name: testinfra diff --git a/molecule/default/tests/test_default.py b/molecule/default/tests/test_default.py new file mode 100644 index 0000000..a0d19fb --- /dev/null +++ b/molecule/default/tests/test_default.py @@ -0,0 +1,76 @@ +import os, re +import testinfra.utils.ansible_runner + +def test_packages(host): + for package_name in ['icinga2', 'icinga2-ido-mysql', 'monitoring-plugins']: + package = host.package(package_name) + assert package.is_installed + +def test_config_hosts(host): + config = host.file('/etc/icinga2/conf.d/hosts.conf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'nagios' + assert config.mode == 0o640 + assert config.contains('address = "127.0.0.1"') + +def test_config_services(host): + config = host.file('/etc/icinga2/conf.d/services.conf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'nagios' + assert config.mode == 0o640 + assert config.contains('check_command = "ping"') + +def test_config_templates(host): + config = host.file('/etc/icinga2/conf.d/templates.conf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'nagios' + assert config.mode == 0o640 + assert config.contains('os = "Linux"') + +def test_config_commands(host): + config = host.file('/etc/icinga2/conf.d/check_commands.conf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'nagios' + assert config.mode == 0o640 + assert config.contains('value = "$http_vhost$"') + +def test_config_commands(host): + config = host.file('/etc/icinga2/conf.d/ido-db.conf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'nagios' + assert config.mode == 0o640 + assert config.contains('password = "test"') + +def test_config_commands(host): + config = host.file('/etc/icinga2/conf.d/timeperiods.conf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'nagios' + assert config.mode == 0o640 + assert config.contains('"sunday" = "00:00-24:00"') + +def test_config_notifications(host): + config = host.file('/etc/icinga2/conf.d/notifications.conf') + assert config.exists + assert config.is_file + assert config.user == 'root' + assert config.group == 'nagios' + assert config.mode == 0o640 + assert config.contains('user_groups = host.vars.notification.mail.groups') + + +def test_service(host): + service = host.service('icinga2') + assert service.is_running + assert service.is_enabled diff --git a/tasks/base.yml b/tasks/base.yml new file mode 100644 index 0000000..67a6977 --- /dev/null +++ b/tasks/base.yml @@ -0,0 +1,45 @@ +--- +- name: install dependencies packages + package: + name: apt-transport-https + retries: 2 + register: result + until: result is succeeded + tags: icinga2 + +- name: add key repository + apt_key: + url: https://packages.icinga.com/icinga.key + retries: 2 + register: result + until: result is succeeded + tags: icinga2 + +- name: add repository + apt_repository: + repo: 'deb https://packages.icinga.com/debian icinga-{{ ansible_distribution_release }} main' + retries: 2 + register: result + until: result is succeeded + tags: icinga2 + +- name: install packages + package: + name: + - icinga2 + - 'icinga2-ido-{{ icinga2_db_type }}' + - icingacli + - monitoring-plugins + retries: 2 + register: result + until: result is succeeded + tags: icinga2 + +- name: create certs directory + file: + path: /var/lib/icinga2/certs + owner: root + group: nagios + mode: 0750 + state: directory + tags: icinga2 diff --git a/tasks/config.yml b/tasks/config.yml new file mode 100644 index 0000000..75fb4ad --- /dev/null +++ b/tasks/config.yml @@ -0,0 +1,116 @@ +--- +- name: remove old config files + file: + path: /etc/icinga2/conf.d/apt.conf + state: absent + notify: reload icinga2 + tags: icinga2 + +- name: copy config files + template: + src: '{{ item }}.conf.j2' + dest: '/etc/icinga2/conf.d/{{ item }}.conf' + owner: root + group: nagios + mode: 0640 + loop: + - check_commands + - groups + - hosts + - notifications + - services + - templates + - timeperiods + - users + notify: reload icinga2 + tags: icinga2 + +- name: copy zones config files + template: + src: zones.conf.j2 + dest: /etc/icinga2/zones.conf + owner: root + group: nagios + mode: 0640 + notify: reload icinga2 + tags: icinga2 + +- name: copy ido config file + template: + src: ido-db.conf.j2 + dest: /etc/icinga2/conf.d/ido-db.conf + owner: root + group: nagios + mode: 0640 + notify: restart icinga2 + tags: icinga2 + +- name: copy api SSL key + copy: + content: '{{ icinga2_api_ssl_node_key }}' + dest: '/var/lib/icinga2/certs/{{ inventory_hostname }}.key' + owner: root + group: nagios + mode: 0640 + when: '"api" in icinga2_features' + notify: restart icinga2 + tags: icinga2 + +- name: copy api SSL certificate + copy: + content: '{{ icinga2_api_ssl_node_crt }}' + dest: '/var/lib/icinga2/certs/{{ inventory_hostname }}.crt' + owner: root + group: nagios + mode: 0640 + when: '"api" in icinga2_features' + notify: restart icinga2 + tags: icinga2 + +- name: copy api SSL ca certifiacte + copy: + content: '{{ icinga2_api_ssl_ca_crt }}' + dest: '{{ item }}/ca.crt' + owner: root + group: nagios + mode: 0640 + when: '"api" in icinga2_features' + loop: + - /var/lib/icinga2/certs + - /etc/icinga2/pki + notify: restart icinga2 + tags: icinga2 + +- name: copy api SSL ca key + copy: + content: '{{ icinga2_api_ssl_ca_key }}' + dest: /etc/icinga2/pki/ca.key + owner: root + group: nagios + mode: 0640 + when: '"api" in icinga2_features and icinga2_master' + notify: restart icinga2 + tags: icinga2 + +- name: copy feature config files + template: + src: feature.conf.j2 + dest: '/etc/icinga2/features-available/{{ item.key }}.conf' + owner: root + group: nagios + mode: 0640 + loop: '{{ icinga2_features|dict2items }}' + loop_control: + label: '{{ item.key }}' + notify: restart icinga2 + tags: icinga2 + +- name: manage features + icinga2_feature: + name: '{{ item.key }}' + state: '{{ item.state|default("present") }}' + loop: '{{ icinga2_features|dict2items }}' + loop_control: + label: '{{ item.key }}' + notify: restart icinga2 + tags: icinga2 diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..500dac9 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,4 @@ +--- +- import_tasks: base.yml +- import_tasks: config.yml +- import_tasks: service.yml diff --git a/tasks/service.yml b/tasks/service.yml new file mode 100644 index 0000000..e05d29b --- /dev/null +++ b/tasks/service.yml @@ -0,0 +1,7 @@ +--- +- name: enable and start service + service: + name: icinga2 + enabled: true + state: started + tags: icinga2 diff --git a/templates/check_commands.conf.j2 b/templates/check_commands.conf.j2 new file mode 100644 index 0000000..c21047e --- /dev/null +++ b/templates/check_commands.conf.j2 @@ -0,0 +1,73 @@ +# {{ ansible_managed }} +{% for name, command in icinga2_check_commands.items() %} + +object CheckCommand "{{ name }}" { + command = {{ command.command }} + +{% if command.arguments is defined %} + + arguments = { +{% for argument in command.arguments %} + "{{ argument.name }}" = { +{% if argument.value is defined %} + value = "{{ argument.value }}" +{% else %} + set_if = "{{ argument.set_if }}" +{% endif %} +{% if argument.description is defined %} + description = "{{ argument.description }}" +{% endif %} +{% if argument.required is defined and argument.required %} + required = true +{% endif %} + } +{% endfor %} + } +{% endif %} +{% if command.vars is defined %} + + var = { +{% for var, value in command.vars.items() %} + {{ var }} = {% if value is number %}{{ value }}{% else %}"{{ value }}"{% endif %} + +{% endfor %} + } +{% endif %} +} +{% endfor %} +{% for name, command in icinga2_event_commands.items() %} + +object EventCommand "{{ name }}" { + command = {{ command.command }} + +{% if command.arguments is defined %} + + arguments = { +{% for argument in command.arguments %} + "{{ argument.name }}" = { +{% if argument.value is defined %} + value = "{{ argument.value }}" +{% else %} + set_if = "{{ argument.set_if }}" +{% endif %} +{% if argument.description is defined %} + description = "{{ argument.description }}" +{% endif %} +{% if argument.required is defined and argument.required %} + required = true +{% endif %} + } +{% endfor %} + } +{% endif %} +{% if command.vars is defined %} + + var = { +{% for var, value in command.vars.items() %} + {{ var }} = {% if value is number %}{{ value }}{% else %}"{{ value }}"{% endif %} + +{% endfor %} + } +{% endif %} +} +{% endfor %} diff --git a/templates/feature.conf.j2 b/templates/feature.conf.j2 new file mode 100644 index 0000000..ac036c0 --- /dev/null +++ b/templates/feature.conf.j2 @@ -0,0 +1,32 @@ +// {{ ansible_managed }} + +object {{ item.value.object }} "{{ item.key }}" { +{% if item.value.config is defined %} +{% for option, value in item.value.config.items() %} +{% if value is sameas true %} + {{ option }} = true +{% elif value is sameas false %} + {{ option }} = false +{% elif value is string %} + {{ option }} = "{{ value }}" +{% elif value is number or value|regex_search('^[0-9]+(s|m|h|d)$') %} + {{ option }} = {{ value }} +{% elif value is mapping %} + {{ option }} = { +{% for k, v in value.iteritems() %} +{% if v is sameas true %} + {{ k }} = true +{% elif v is sameas false %} + {{ k }} = false +{% elif v is string %} + {{ k }} = "{{ v }}" +{% elif v is number or v|regex_search('^[0-9]+(s|m|h|d)$') %} + {{ k }} = {{ v }} +{% endif %} +{% endfor %} + } +{% endif %} +{% endfor %} +{% endif %} +} + diff --git a/templates/groups.conf.j2 b/templates/groups.conf.j2 new file mode 100644 index 0000000..92df3eb --- /dev/null +++ b/templates/groups.conf.j2 @@ -0,0 +1,15 @@ +# {{ ansible_managed }} +{% for name, group in icinga2_host_groups.items() %} + +object HostGroup "{{ name }}" { + display_name = "{{ group.display_name|default(name) }}" + assign where {{ group.assign }} +} +{% endfor %} +{% for name, group in icinga2_service_groups.items() %} + +object ServiceGroup "{{ name }}" { + display_name = "{{ group.display_name|default(name) }}" + assign where match("{{ group.match }}", service.name) +} +{% endfor %} diff --git a/templates/hosts.conf.j2 b/templates/hosts.conf.j2 new file mode 100644 index 0000000..40da015 --- /dev/null +++ b/templates/hosts.conf.j2 @@ -0,0 +1,41 @@ +# {{ ansible_managed }} +{% for name, host in icinga2_hosts.items() %} + +object Host "{{ name }}" { + import "{{ host.template }}" +{% if host.address is defined %} + address = "{{ host.address }}" +{% endif %} +{% if host.address6 is defined %} + address6 = "{{ host.address6 }}" +{% endif %} +{% if host.zone is defined %} + zone = "{{ host.zone }}" +{% endif %} +{% if host.vars is defined %} + +{% for var, value in host.vars.items() %} +{% if value is sameas True %} + vars.{{ var }} = true +{% elif value is sameas False %} + vars.{{ var }} = false +{% elif value is mapping %} +{% for name, config in value.items() %} + vars.{{ var }}["{{ name }}"] = { +{% for option, v in config.items() %} + {{ option }} = {% if v is number %}{{ v }}{% else %}"{{ v }}"{% endif %} + +{% endfor %} + } +{% endfor %} +{% elif value is iterable and not value is string %} + vars.{{ var }} = [ "{{ value|join(', "') }}" ] +{% elif value is number or value|regex_search('^[0-9]+(s|m|h|d)$') %} + vars.{{ var }} = {{ value }} +{% else %} + vars.{{ var }} = "{{ value }}" +{% endif %} +{% endfor %} +{% endif %} +} +{% endfor %} diff --git a/templates/ido-db.conf.j2 b/templates/ido-db.conf.j2 new file mode 100644 index 0000000..d827e79 --- /dev/null +++ b/templates/ido-db.conf.j2 @@ -0,0 +1,10 @@ +# {{ ansible_managed }} + +library "db_ido_{{ icinga2_db_type }}" + +object Ido{{ icinga2_db_type|title }}Connection "ido-{{ icinga2_db_type }}" { + user = "{{ icinga2_ido_user }}", + password = "{{ icinga2_ido_password }}", + host = "{{ icinga2_ido_host }}", + database = "{{ icinga2_ido_database }}" +} diff --git a/templates/notifications.conf.j2 b/templates/notifications.conf.j2 new file mode 100644 index 0000000..1237ec7 --- /dev/null +++ b/templates/notifications.conf.j2 @@ -0,0 +1,37 @@ +# {{ ansible_managed }} +{% for name, notification in icinga2_notifications.items() %} + +apply Notification "{{ name }}" to {{ notification.type }} { + import "{{ notification.import }}" + assign where {{ notification.assign }} + user_groups = {{ notification.groups }} + users = {{ notification.users }} +{% if notification.options is defined %} +{% for option, value in notification.options.items() %} +{% if value is sameas True %} + {{ option }} = true +{% elif value is sameas False %} + {{ option }} = false +{% elif value is number or value|regex_search('^[0-9]+(s|m|h|d)$') or option in ['period', 'zone'] %} + {{ option }} = {{ value }} +{% else %} + {{ option }} = "{{ value }}" +{% endif %} +{% endfor %} +{% endif %} +{% if notification.vars is defined %} + +{% for var, value in notification.vars.items() %} +{% if value is sameas True %} + vars.{{ var }} = true +{% elif value is sameas False %} + vars.{{ var }} = false +{% elif value is number or value|regex_search('^[0-9]+(s|m|h|d)$') %} + vars.{{ var }} = {{ value }} +{% else %} + vars.{{ var }} = "{{ value }}" +{% endif %} +{% endfor %} +{% endif %} +} +{% endfor %} diff --git a/templates/services.conf.j2 b/templates/services.conf.j2 new file mode 100644 index 0000000..c2c4f68 --- /dev/null +++ b/templates/services.conf.j2 @@ -0,0 +1,29 @@ +# {{ ansible_managed }} +{% for name, service in icinga2_services.items() %} + +apply Service "{{ name }}" { + import "{{ service.import|default('generic-service') }}" + check_command = "{{ service.check_command }}" + assign where {{ service.assign }} +{% if service.options is defined %} +{% for option, value in service.options.items() %} +{% if value is sameas True %} + {{ option }} = true +{% elif value is sameas False %} + {{ option }} = false +{% elif value is number or value|regex_search('^[0-9]+(s|m|h|d)$') %} + {{ option }} = {{ value }} +{% else %} + {{ option }} = "{{ value }}" +{% endif %} +{% endfor %} +{% endif %} +{% if service.vars is defined %} + +{% for var_name, value in service.vars.items() %} + vars.{{ var_name }} = {% if value is number %}{{ value }}{% else %}"{{ value|replace('\n', ' ') }}"{% endif %} + +{% endfor %} +{% endif %} +} +{% endfor %} diff --git a/templates/templates.conf.j2 b/templates/templates.conf.j2 new file mode 100644 index 0000000..8317384 --- /dev/null +++ b/templates/templates.conf.j2 @@ -0,0 +1,44 @@ +# {{ ansible_managed }} +{% for name, template in icinga2_templates.items() %} + +template {{ template.type|title }} "{{ name }}" { +{% if template.import is defined %} + import "{{ template.import }}" +{% endif %} +{% if template.options is defined %} +{% for option, value in template.options.items() %} +{% if value is iterable and not value is string %} +{% if option in ['states', 'types'] %} + {{ option }} = [ {{ value|join(', ') }} ] +{% else %} + {{ option }} = [ "{{ value|join(', "') }}" ] +{% endif %} +{% elif value is number or value|regex_search('^[0-9]+(s|m|h|d)$') %} + {{ option }} = {{ value }} +{% else %} + {{ option }} = "{{ value }}" +{% endif %} +{% endfor %} +{% endif %} +{% if template.vars is defined %} + +{% for var, value in template.vars.items() %} +{% if value is sameas True %} + vars.{{ var }} = true +{% elif value is sameas False %} + vars.{{ var }} = false +{% elif value is iterable and not value is string %} +{% if var in ['states', 'types'] %} + vars.{{ var }} = [ {{ value|join(', ') }} ] +{% else %} + vars.{{ var }} = [ "{{ value|join(', "') }}" ] +{% endif %} +{% elif value is number or value|regex_search('^[0-9]+(s|m|h|d)$') %} + vars.{{ var }} = {{ value }} +{% else %} + vars.{{ var }} = "{{ value }}" +{% endif %} +{% endfor %} +{% endif %} +} +{% endfor %} diff --git a/templates/timeperiods.conf.j2 b/templates/timeperiods.conf.j2 new file mode 100644 index 0000000..b2d6249 --- /dev/null +++ b/templates/timeperiods.conf.j2 @@ -0,0 +1,31 @@ +# {{ ansible_managed }} +{% for time_name, time in icinga2_timeperiods.items() %} +object TimePeriod "{{ time_name }}" { +{% if time.display_name is defined %} + display_name = "{{ time.display_name }}" +{% endif %} + prefer_includes = {% if time.prefer_includes is sameas false %}false{% else %}true +{% endif %} + ranges = { +{% if time.ranges is defined %} +{% for timeperiod, range in time.ranges.items() %} + "{{ timeperiod }}" = "{{ range }}" +{% endfor %} +{% endif %} + } +{% if time.includes is defined %} + includes = { +{% for timeperiod, range in time.includes.items() %} + "{{ timeperiod }}" = "{{ range }}" +{% endfor %} + } +{% endif %} +{% if time.excludes is defined %} + excludes = { +{% for timeperiod, range in time.excludes.items() %} + "{{ timeperiod }}" = "{{ range }}" +{% endfor %} + } +{% endif %} +} +{% endfor %} diff --git a/templates/users.conf.j2 b/templates/users.conf.j2 new file mode 100644 index 0000000..c00edca --- /dev/null +++ b/templates/users.conf.j2 @@ -0,0 +1,22 @@ +# {{ ansible_managed }} +{% for name, group in icinga2_user_groups.items() %} + +object UserGroup "{{ name }}" { + display_name = "{{ group.display_name|default(name) }}" +} +{% endfor %} +{% for name, user in icinga2_users.items() %} + +object User "{{ name }}" { + display_name = "{{ user.display_name|default(name) }}" + groups = [ "{{ user.groups|join(', "') }}" ] + email = "{{ user.email }}" +} +{% endfor %} +{% for name, user in icinga2_api_users.items() %} + +object ApiUser "{{ name }}" { + password = "{{ user.password }}" + permissions = [ "{{ user.permissions|join(', "') }}" ] +} +{% endfor %} diff --git a/templates/zones.conf.j2 b/templates/zones.conf.j2 new file mode 100644 index 0000000..a946f2a --- /dev/null +++ b/templates/zones.conf.j2 @@ -0,0 +1,24 @@ +# {{ ansible_managed }} + +object Zone "global-templates" { + global = true +} + +object Zone "director-global" { + global = true +} +{% for endpoint, host in icinga2_endpoints.items() %} +object Endpoint "{{ endpoint }}" { + host = "{{ host }}" +} + +{% endfor %} +{% for zone_name, zone in icinga2_zones.items() %} +object Zone "{{ zone_name }}" { +{% if zone.parent is defined %} + parent = "{{ zone.parent }}" +{% endif %} + endpoints = [ "{{ zone.endpoints|join('", "')}}" ] +} + +{% endfor %}