From 99095ffd582a3fc4ac5fd02fb8e1f6c570a0ca8a Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 11:05:44 +0100 Subject: [PATCH 01/29] added integration tests within molecule --- molecule/default/molecule.yml | 3 ++- molecule/default/tests/integration/test_icinga2.py | 7 +++++++ molecule/default/tests/integration/test_service.py | 4 ++++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 molecule/default/tests/integration/test_icinga2.py create mode 100644 molecule/default/tests/integration/test_service.py diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 5f0621f9..582d442c 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -14,7 +14,8 @@ platforms: provisioner: name: ansible verifier: - name: ansible + name: testinfra + directory: tests/integration/ lint: | set -e yamllint . diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py new file mode 100644 index 00000000..2366b446 --- /dev/null +++ b/molecule/default/tests/integration/test_icinga2.py @@ -0,0 +1,7 @@ +def test_icinga2_package(host): + icinga2_pkg = host.package("icinga2") + assert icinga2_pkg.is_installed + +def test_icinga2_dir(host): + icinga2_dir = host.file("/etc/icinga2") + assert icinga2_dir.is_directory diff --git a/molecule/default/tests/integration/test_service.py b/molecule/default/tests/integration/test_service.py new file mode 100644 index 00000000..bfcd0cdc --- /dev/null +++ b/molecule/default/tests/integration/test_service.py @@ -0,0 +1,4 @@ +def test_icinga2_service(host): + service = host.service("icinga2") + assert service.is_running + assert service.is_enabled From 6cb13e6edd05eba5c4697da44b5d8fa58e662e28 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 11:17:08 +0100 Subject: [PATCH 02/29] added pytest-testinfra to requirements --- requirements-test.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements-test.txt b/requirements-test.txt index 5a5565fc..8d073762 100644 --- a/requirements-test.txt +++ b/requirements-test.txt @@ -2,3 +2,4 @@ ansible ansible-lint molecule molecule-docker +pytest-testinfra From 0e836895ce80310ed7c29372c179e926de817a7f Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 11:32:52 +0100 Subject: [PATCH 03/29] added more integration tests for features --- molecule/default/tests/integration/test_icinga2.py | 4 ++++ molecule/default/tests/integration/test_repos.py | 9 +++++++++ 2 files changed, 13 insertions(+) create mode 100644 molecule/default/tests/integration/test_repos.py diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 2366b446..c8bdef2b 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -5,3 +5,7 @@ def test_icinga2_package(host): def test_icinga2_dir(host): icinga2_dir = host.file("/etc/icinga2") assert icinga2_dir.is_directory + +def test_icinga2_features(host): + f_checker = host.file("/etc/icinga2/feature-enabled/checker.conf") + assert f_checker.linked_to("/etc/icinga2/feature-available/checker.conf") diff --git a/molecule/default/tests/integration/test_repos.py b/molecule/default/tests/integration/test_repos.py new file mode 100644 index 00000000..3bed8788 --- /dev/null +++ b/molecule/default/tests/integration/test_repos.py @@ -0,0 +1,9 @@ +def test_repos(host): + if host.system_info.distribution == 'debian': + repofile = '/etc/apt/sources.list.d/icinga' + if host.system_info.distribution == 'centos': + repofile = '/etc/yum.repos.d/ICINGA-release' + + + rf = host.file(repofile) + assert rf.is_file From 08457932f589ebce551cf3e93179dcaf513e1f13 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 11:45:06 +0100 Subject: [PATCH 04/29] fixed test_icinga2 typo and change test_repos --- molecule/default/tests/integration/test_icinga2.py | 4 ++-- molecule/default/tests/integration/test_repos.py | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index c8bdef2b..00bea46e 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -7,5 +7,5 @@ def test_icinga2_dir(host): assert icinga2_dir.is_directory def test_icinga2_features(host): - f_checker = host.file("/etc/icinga2/feature-enabled/checker.conf") - assert f_checker.linked_to("/etc/icinga2/feature-available/checker.conf") + f_checker = host.file("/etc/icinga2/features-enabled/checker.conf") + assert f_checker.linked_to("/etc/icinga2/features-available/checker.conf") diff --git a/molecule/default/tests/integration/test_repos.py b/molecule/default/tests/integration/test_repos.py index 3bed8788..df221997 100644 --- a/molecule/default/tests/integration/test_repos.py +++ b/molecule/default/tests/integration/test_repos.py @@ -1,9 +1,7 @@ def test_repos(host): if host.system_info.distribution == 'debian': - repofile = '/etc/apt/sources.list.d/icinga' + repofile = host.file("etc/apt/sources.list.d/icinga") if host.system_info.distribution == 'centos': - repofile = '/etc/yum.repos.d/ICINGA-release' + repofile = host.file("/etc/yum.repos.d/ICINGA-release") - - rf = host.file(repofile) - assert rf.is_file + assert repofile.is_file From 0cb9d4cf8bfdc234cd75eb66c6a1a1a659559e9c Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 12:18:20 +0100 Subject: [PATCH 05/29] update tests --- molecule/default/tests/integration/test_icinga2.py | 2 +- molecule/default/tests/integration/test_repos.py | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 00bea46e..59cb592c 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -8,4 +8,4 @@ def test_icinga2_dir(host): def test_icinga2_features(host): f_checker = host.file("/etc/icinga2/features-enabled/checker.conf") - assert f_checker.linked_to("/etc/icinga2/features-available/checker.conf") + assert f_checker.exists diff --git a/molecule/default/tests/integration/test_repos.py b/molecule/default/tests/integration/test_repos.py index df221997..d381c725 100644 --- a/molecule/default/tests/integration/test_repos.py +++ b/molecule/default/tests/integration/test_repos.py @@ -1,7 +1,9 @@ def test_repos(host): if host.system_info.distribution == 'debian': - repofile = host.file("etc/apt/sources.list.d/icinga") + repofile = host.file("/etc/apt/sources.list.d/icinga") + assert repofile.is_file + assert repofile.user("root") if host.system_info.distribution == 'centos': repofile = host.file("/etc/yum.repos.d/ICINGA-release") - - assert repofile.is_file + assert repofile.is_file + assert repofile.user("root") From 46773e166dd7aa6d43e9eb597b67510ff6f90859 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 12:55:31 +0100 Subject: [PATCH 06/29] fixed ending of files in repos --- molecule/default/tests/integration/test_repos.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/molecule/default/tests/integration/test_repos.py b/molecule/default/tests/integration/test_repos.py index d381c725..cf9d0c83 100644 --- a/molecule/default/tests/integration/test_repos.py +++ b/molecule/default/tests/integration/test_repos.py @@ -1,9 +1,9 @@ def test_repos(host): if host.system_info.distribution == 'debian': - repofile = host.file("/etc/apt/sources.list.d/icinga") + repofile = host.file("/etc/apt/sources.list.d/icinga.list") assert repofile.is_file assert repofile.user("root") if host.system_info.distribution == 'centos': - repofile = host.file("/etc/yum.repos.d/ICINGA-release") + repofile = host.file("/etc/yum.repos.d/ICINGA-release.repo") assert repofile.is_file assert repofile.user("root") From 227294a242fbd2623d9df64587db807ac2d5d8a8 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 13:09:47 +0100 Subject: [PATCH 07/29] updated repos and feature tests --- molecule/default/tests/integration/test_icinga2.py | 1 + molecule/default/tests/integration/test_repos.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 59cb592c..0fd744fc 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -9,3 +9,4 @@ def test_icinga2_dir(host): def test_icinga2_features(host): f_checker = host.file("/etc/icinga2/features-enabled/checker.conf") assert f_checker.exists + assert f_checker.linked_to == "/etc/icinga2/features-available/checker.conf" diff --git a/molecule/default/tests/integration/test_repos.py b/molecule/default/tests/integration/test_repos.py index cf9d0c83..692954c2 100644 --- a/molecule/default/tests/integration/test_repos.py +++ b/molecule/default/tests/integration/test_repos.py @@ -2,8 +2,8 @@ def test_repos(host): if host.system_info.distribution == 'debian': repofile = host.file("/etc/apt/sources.list.d/icinga.list") assert repofile.is_file - assert repofile.user("root") + assert repofile.user == "root" if host.system_info.distribution == 'centos': repofile = host.file("/etc/yum.repos.d/ICINGA-release.repo") assert repofile.is_file - assert repofile.user("root") + assert repofile.user == "root" From 2224a670e67b43aaf1c4f29999aa9aa285bdde6b Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 13:49:35 +0100 Subject: [PATCH 08/29] added test for feature mainlog --- molecule/default/tests/integration/test_icinga2.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 0fd744fc..9f869803 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -8,5 +8,8 @@ def test_icinga2_dir(host): def test_icinga2_features(host): f_checker = host.file("/etc/icinga2/features-enabled/checker.conf") + f_mainlog = host.file("/etc/icinga2/features-enabled/mainlog.conf") assert f_checker.exists assert f_checker.linked_to == "/etc/icinga2/features-available/checker.conf" + assert f_mainlog.exists + assert f_mainlog.linked_to == "/etc/icinga2/features-available/mainlog.conf" From d32d7cd99924d46b7ef7e43222a84c6914eb764c Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 14:25:45 +0100 Subject: [PATCH 09/29] extended tests with some variables to get away from standard --- molecule/default/converge.yml | 12 ++++++++++++ molecule/default/tests/integration/test_icinga2.py | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index a2fe613f..9fda0f0e 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -2,6 +2,18 @@ - name: Converge hosts: all + vars: + icinga2_features: + - name: api + ca_host: none + endpoints: + - name: "{{ ansible_fqdn }}" + zones: + - name: "main" + endpoints: + - "{{ ansible_fqdn }}" + icinga2_config_directories: + - zones.d/main collections: icinga.icinga tasks: diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 9f869803..4dc837eb 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -2,6 +2,16 @@ def test_icinga2_package(host): icinga2_pkg = host.package("icinga2") assert icinga2_pkg.is_installed +def test_icinga2_configdir(host): + icinga2_cdir = host.file("/etc/icinga2/zones.d/main") + assert icinga2_cdir.is_directory + if host.system_info.distribution == 'centos': + assert icinga2_cdir.owner == "icinga" + assert icinga2_cdir.group == "icinga" + if host.system_info.distribution == 'debian': + assert icinga2_cdir.owner == "nagios" + assert icinga2_cdir.group == "nagios" + def test_icinga2_dir(host): icinga2_dir = host.file("/etc/icinga2") assert icinga2_dir.is_directory @@ -9,7 +19,10 @@ def test_icinga2_dir(host): def test_icinga2_features(host): f_checker = host.file("/etc/icinga2/features-enabled/checker.conf") f_mainlog = host.file("/etc/icinga2/features-enabled/mainlog.conf") + f_api = host.file("/etc/icinga2/features-enabled/api.conf") assert f_checker.exists assert f_checker.linked_to == "/etc/icinga2/features-available/checker.conf" assert f_mainlog.exists assert f_mainlog.linked_to == "/etc/icinga2/features-available/mainlog.conf" + assert f_api.exists + assert f_api.linked_to == "/etc/icinga2/features-available/api.conf" From 2be33150906ddd8c21fc1eb839f729ab42f6d50a Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 14:44:28 +0100 Subject: [PATCH 10/29] updated tests and enabled checker and mainlog --- molecule/default/converge.yml | 2 ++ molecule/default/tests/integration/test_icinga2.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 9fda0f0e..ece88986 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -4,6 +4,8 @@ hosts: all vars: icinga2_features: + - name: checker + - name: mainlog - name: api ca_host: none endpoints: diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 4dc837eb..207e71d8 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -6,10 +6,10 @@ def test_icinga2_configdir(host): icinga2_cdir = host.file("/etc/icinga2/zones.d/main") assert icinga2_cdir.is_directory if host.system_info.distribution == 'centos': - assert icinga2_cdir.owner == "icinga" + assert icinga2_cdir.user == "icinga" assert icinga2_cdir.group == "icinga" if host.system_info.distribution == 'debian': - assert icinga2_cdir.owner == "nagios" + assert icinga2_cdir.user == "nagios" assert icinga2_cdir.group == "nagios" def test_icinga2_dir(host): From d262d6e1150927fef3997b0322be0b0a9ca0f61c Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 16:54:47 +0100 Subject: [PATCH 11/29] added host vars for integration tests --- molecule/default/converge.yml | 2 +- molecule/default/host_vars/icinga-default.yaml | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 molecule/default/host_vars/icinga-default.yaml diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index ece88986..a55b453e 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -15,7 +15,7 @@ endpoints: - "{{ ansible_fqdn }}" icinga2_config_directories: - - zones.d/main + - zones.d/main/hosts collections: icinga.icinga tasks: diff --git a/molecule/default/host_vars/icinga-default.yaml b/molecule/default/host_vars/icinga-default.yaml new file mode 100644 index 00000000..f1db2696 --- /dev/null +++ b/molecule/default/host_vars/icinga-default.yaml @@ -0,0 +1,11 @@ +icinga2_objects: + icinga-master.localdomain: + - name: agent.localdomain + type: Endpoint + file: zones.d/main/hosts/agent.localdomain.conf + - name: agent.localdomain + type: Host + file: zones.d/main/hosts/agent.localdomain.conf + check_command: hostalive + address: 127.0.0.1 + check_interval: 3m From 75d0e2e43552f5a8c2ffdf14f4c5e38b1f59da6a Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 17:29:46 +0100 Subject: [PATCH 12/29] removed host_vars folder and added it to molecule.yml --- molecule/default/host_vars/icinga-default.yaml | 11 ----------- molecule/default/molecule.yml | 13 +++++++++++++ molecule/default/tests/integration/test_icinga2.py | 6 ++++++ 3 files changed, 19 insertions(+), 11 deletions(-) delete mode 100644 molecule/default/host_vars/icinga-default.yaml diff --git a/molecule/default/host_vars/icinga-default.yaml b/molecule/default/host_vars/icinga-default.yaml deleted file mode 100644 index f1db2696..00000000 --- a/molecule/default/host_vars/icinga-default.yaml +++ /dev/null @@ -1,11 +0,0 @@ -icinga2_objects: - icinga-master.localdomain: - - name: agent.localdomain - type: Endpoint - file: zones.d/main/hosts/agent.localdomain.conf - - name: agent.localdomain - type: Host - file: zones.d/main/hosts/agent.localdomain.conf - check_command: hostalive - address: 127.0.0.1 - check_interval: 3m diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 582d442c..305bf10d 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -13,6 +13,19 @@ platforms: pre_build_image: true provisioner: name: ansible + inventory: + host_vars: + icinga2_objects: + icinga-master.localdomain: + - name: agent.localdomain + type: Endpoint + file: zones.d/main/hosts/agent.localdomain.conf + - name: agent.localdomain + type: Host + file: zones.d/main/hosts/agent.localdomain.conf + check_command: hostalive + address: 127.0.0.1 + check_interval: 3m verifier: name: testinfra directory: tests/integration/ diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 207e71d8..d11b6c7d 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -12,6 +12,12 @@ def test_icinga2_configdir(host): assert icinga2_cdir.user == "nagios" assert icinga2_cdir.group == "nagios" +def test_icinga2_objects(host): + i2_oh = host.file("/etc/icinga2/zones.d/main/hosts") + assert i2_oh.is_directory + + + def test_icinga2_dir(host): icinga2_dir = host.file("/etc/icinga2") assert icinga2_dir.is_directory From e1d8296ce3232d6a8eac4dd9cfa7008b1f52a4a6 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 17:44:33 +0100 Subject: [PATCH 13/29] added icinga2 objects for icinga-default --- molecule/default/converge.yml | 3 +++ molecule/default/host_vars/icinga-default.yaml | 11 +++++++++++ molecule/default/molecule.yml | 14 ++------------ 3 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 molecule/default/host_vars/icinga-default.yaml diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index a55b453e..e35bd7ec 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -19,6 +19,9 @@ collections: icinga.icinga tasks: + - debug: + var: ansible_fqdn + - name: "Include Icinga Repos" include_role: name: repos diff --git a/molecule/default/host_vars/icinga-default.yaml b/molecule/default/host_vars/icinga-default.yaml new file mode 100644 index 00000000..864b9af3 --- /dev/null +++ b/molecule/default/host_vars/icinga-default.yaml @@ -0,0 +1,11 @@ +icinga2_objects: + icinga-default: + - name: agent.localdomain + type: Endpoint + file: zones.d/main/hosts/agent.localdomain.conf + - name: agent.localdomain + type: Host + file: zones.d/main/hosts/agent.localdomain.conf + check_command: hostalive + address: 127.0.0.1 + check_interval: 3m diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 305bf10d..42363430 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -14,18 +14,8 @@ platforms: provisioner: name: ansible inventory: - host_vars: - icinga2_objects: - icinga-master.localdomain: - - name: agent.localdomain - type: Endpoint - file: zones.d/main/hosts/agent.localdomain.conf - - name: agent.localdomain - type: Host - file: zones.d/main/hosts/agent.localdomain.conf - check_command: hostalive - address: 127.0.0.1 - check_interval: 3m + link: + host_vars: host_vars/ verifier: name: testinfra directory: tests/integration/ From af623ac70ca8c7cc8a437768f096afca06eb7e45 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 18:05:17 +0100 Subject: [PATCH 14/29] update molecule tests --- molecule/default/converge.yml | 1 - molecule/default/molecule.yml | 4 ++-- molecule/default/prepare.yml | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index e35bd7ec..6d05ed5a 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -21,7 +21,6 @@ tasks: - debug: var: ansible_fqdn - - name: "Include Icinga Repos" include_role: name: repos diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index 42363430..bf18f05f 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -21,5 +21,5 @@ verifier: directory: tests/integration/ lint: | set -e - yamllint . - ansible-lint . + yamllint --no-warnings roles/ + ansible-lint roles/ diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index 24953e61..cc5bbc56 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -5,7 +5,7 @@ - name: Install requirements for Debian apt: name: - - gpg - - apt-transport-https + - gpg + - apt-transport-https update_cache: yes when: ansible_os_family == "Debian" From a4e6e7ab3bac3ecee73e067fe9ba215a6eda32e2 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 18:25:25 +0100 Subject: [PATCH 15/29] update ansible-lint settings --- .ansible-lint | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.ansible-lint b/.ansible-lint index 03c0023e..ac3698ad 100644 --- a/.ansible-lint +++ b/.ansible-lint @@ -1,2 +1,9 @@ skip_list: - 'yaml' + - unnamed-task +warn_list: + - command-instead-of-shell + - risky-file-permissions + - literal-compare + - meta-incorrect + - meta-no-info From 698aa039eb137fb5e44edfb8416ddd4761d85edc Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 18:33:00 +0100 Subject: [PATCH 16/29] added zone for endpoint --- molecule/default/host_vars/icinga-default.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/molecule/default/host_vars/icinga-default.yaml b/molecule/default/host_vars/icinga-default.yaml index 864b9af3..46027432 100644 --- a/molecule/default/host_vars/icinga-default.yaml +++ b/molecule/default/host_vars/icinga-default.yaml @@ -3,6 +3,12 @@ icinga2_objects: - name: agent.localdomain type: Endpoint file: zones.d/main/hosts/agent.localdomain.conf + - name: agent.localdomain + type: Zone + file: zones.d/main/hosts/agent.localdomain.conf + parent: main + endpoints: + - agent.localdomain - name: agent.localdomain type: Host file: zones.d/main/hosts/agent.localdomain.conf From 354ce5316df5e12d527e7e420b0380b57eef9152 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Thu, 11 Nov 2021 18:35:42 +0100 Subject: [PATCH 17/29] added tests for hostobject --- molecule/default/tests/integration/test_icinga2.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index d11b6c7d..20a70014 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -15,7 +15,8 @@ def test_icinga2_configdir(host): def test_icinga2_objects(host): i2_oh = host.file("/etc/icinga2/zones.d/main/hosts") assert i2_oh.is_directory - + i2_oagent = host.file("/etc/icinga2/zones.d/main/hosts/agent.localdomain.conf") + assert i2_oagent.is_file def test_icinga2_dir(host): From c038a17518dbed1a4ad15fee1556f38f4c324e9e Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 07:23:02 +0100 Subject: [PATCH 18/29] updated tests with services --- molecule/default/converge.yml | 3 +-- molecule/default/host_vars/icinga-default.yaml | 16 ++++++++++++++++ .../default/tests/integration/test_icinga2.py | 5 ++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 6d05ed5a..9f608752 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -16,11 +16,10 @@ - "{{ ansible_fqdn }}" icinga2_config_directories: - zones.d/main/hosts + - zones.d/main/services collections: icinga.icinga tasks: - - debug: - var: ansible_fqdn - name: "Include Icinga Repos" include_role: name: repos diff --git a/molecule/default/host_vars/icinga-default.yaml b/molecule/default/host_vars/icinga-default.yaml index 46027432..78b6a6cd 100644 --- a/molecule/default/host_vars/icinga-default.yaml +++ b/molecule/default/host_vars/icinga-default.yaml @@ -15,3 +15,19 @@ icinga2_objects: check_command: hostalive address: 127.0.0.1 check_interval: 3m + - name: generic-service + type: Service + template: true + check_interval: 300s + retry_interval: 30s + order: 1 + - name: ping + type: Service + file: zones.d/main/services/services.conf + imports: + - generic-service + check_command: ping4 + assign: + - host.address + ignore: + - match("no*", host.name) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 20a70014..4995df44 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -17,7 +17,10 @@ def test_icinga2_objects(host): assert i2_oh.is_directory i2_oagent = host.file("/etc/icinga2/zones.d/main/hosts/agent.localdomain.conf") assert i2_oagent.is_file - + print i2_oservice.content_string + i2_oservice = host.file("/etc/icinga2/zones.d/main/services/services.conf") + assert i2_oservice.is_file + print i2_oservice.content_string def test_icinga2_dir(host): icinga2_dir = host.file("/etc/icinga2") From c4d690ec42dbffabfeb6388f8c4925d3e39118ba Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 07:27:47 +0100 Subject: [PATCH 19/29] added missing requirement file --- molecule/default/host_vars/icinga-default.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/molecule/default/host_vars/icinga-default.yaml b/molecule/default/host_vars/icinga-default.yaml index 78b6a6cd..bb26fd67 100644 --- a/molecule/default/host_vars/icinga-default.yaml +++ b/molecule/default/host_vars/icinga-default.yaml @@ -17,6 +17,7 @@ icinga2_objects: check_interval: 3m - name: generic-service type: Service + file: zones.d/main/services/services.conf template: true check_interval: 300s retry_interval: 30s From b5b6db6211149b2da66a1a6f8f2b13d05cd26457 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 08:12:51 +0100 Subject: [PATCH 20/29] changed assign rules --- molecule/default/host_vars/icinga-default.yaml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/molecule/default/host_vars/icinga-default.yaml b/molecule/default/host_vars/icinga-default.yaml index bb26fd67..1ee4edf2 100644 --- a/molecule/default/host_vars/icinga-default.yaml +++ b/molecule/default/host_vars/icinga-default.yaml @@ -31,4 +31,16 @@ icinga2_objects: assign: - host.address ignore: - - match("no*", host.name) + - match(no*, host.name) + - name: group-ping + type: ServiceGroup + file: zones.d/main/servicegroups.conf + display_name: "Ping Services" + assign: + - match(ping*, service.check_command) + - name: group-linux + type: HostGroup + file: zones.d/main/hostgroups.conf + display_name: Linux Hosts + assign: + - match(linux, host.vars.os) From 0fdef036a24de6d38bcb542065ee97e309a2c955 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 08:29:03 +0100 Subject: [PATCH 21/29] added check config --- roles/icinga2/tasks/configure.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/roles/icinga2/tasks/configure.yml b/roles/icinga2/tasks/configure.yml index 01ae2fed..e597de11 100644 --- a/roles/icinga2/tasks/configure.yml +++ b/roles/icinga2/tasks/configure.yml @@ -102,3 +102,11 @@ when: item.split('/')[3] == 'conf.d' or item.split('/')[3] == 'zones.d' loop: "{{ _empty_result.stdout_lines }}" notify: reload icinga2 service + +- name: check config + ansible.builtin.command: icinga2 daemon -C + register: da + ignore_errors: true + +- debug: + var: da From 842ab3da6ef7e7744b1b6cb1ed323642f61d9186 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 08:36:25 +0100 Subject: [PATCH 22/29] added command never change --- roles/icinga2/tasks/configure.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/roles/icinga2/tasks/configure.yml b/roles/icinga2/tasks/configure.yml index e597de11..3cbaf200 100644 --- a/roles/icinga2/tasks/configure.yml +++ b/roles/icinga2/tasks/configure.yml @@ -105,6 +105,7 @@ - name: check config ansible.builtin.command: icinga2 daemon -C + changed_when: false register: da ignore_errors: true From 00fa012c813746c9c870749b4a4b522f31d004de Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 08:44:59 +0100 Subject: [PATCH 23/29] updated service apply --- molecule/default/host_vars/icinga-default.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/molecule/default/host_vars/icinga-default.yaml b/molecule/default/host_vars/icinga-default.yaml index 1ee4edf2..09a4cbb3 100644 --- a/molecule/default/host_vars/icinga-default.yaml +++ b/molecule/default/host_vars/icinga-default.yaml @@ -24,6 +24,7 @@ icinga2_objects: order: 1 - name: ping type: Service + apply: true file: zones.d/main/services/services.conf imports: - generic-service From 5e3552248501a44536a0df6daddc2d492de27422 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 09:07:18 +0100 Subject: [PATCH 24/29] removed conf.d folder from icinga2.conf --- molecule/default/converge.yml | 1 + roles/icinga2/tasks/configure.yml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 9f608752..b82dbc71 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -3,6 +3,7 @@ - name: Converge hosts: all vars: + icinga2_confd: false icinga2_features: - name: checker - name: mainlog diff --git a/roles/icinga2/tasks/configure.yml b/roles/icinga2/tasks/configure.yml index 3cbaf200..440a2976 100644 --- a/roles/icinga2/tasks/configure.yml +++ b/roles/icinga2/tasks/configure.yml @@ -110,4 +110,4 @@ ignore_errors: true - debug: - var: da + var: da.stdout From 83e7873ed22dc0863925c367f6571efe4a50114e Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 09:13:44 +0100 Subject: [PATCH 25/29] fixed print syntax --- molecule/default/tests/integration/test_icinga2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 4995df44..4eda2fc8 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -17,10 +17,10 @@ def test_icinga2_objects(host): assert i2_oh.is_directory i2_oagent = host.file("/etc/icinga2/zones.d/main/hosts/agent.localdomain.conf") assert i2_oagent.is_file - print i2_oservice.content_string + print(i2_oservice.content_string) i2_oservice = host.file("/etc/icinga2/zones.d/main/services/services.conf") assert i2_oservice.is_file - print i2_oservice.content_string + print(i2_oservice.content_string) def test_icinga2_dir(host): icinga2_dir = host.file("/etc/icinga2") From 134ff5f4378449ec61aa520e28987a86ee133e55 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 09:20:50 +0100 Subject: [PATCH 26/29] fixed referenced var before declared --- molecule/default/tests/integration/test_icinga2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index 4eda2fc8..b0dd0b19 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -17,7 +17,7 @@ def test_icinga2_objects(host): assert i2_oh.is_directory i2_oagent = host.file("/etc/icinga2/zones.d/main/hosts/agent.localdomain.conf") assert i2_oagent.is_file - print(i2_oservice.content_string) + print(i2_oagent.content_string) i2_oservice = host.file("/etc/icinga2/zones.d/main/services/services.conf") assert i2_oservice.is_file print(i2_oservice.content_string) From f5f3730ce1c3c7770452ba1e01ebbb38bb76c87d Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 10:30:48 +0100 Subject: [PATCH 27/29] added user and updated tests --- .../default/tests/integration/test_icinga2.py | 116 +++++++++++++++--- roles/icinga2/tasks/configure.yml | 2 + roles/icinga2/tasks/features/api.yml | 2 + 3 files changed, 101 insertions(+), 19 deletions(-) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index b0dd0b19..f6434488 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -12,27 +12,105 @@ def test_icinga2_configdir(host): assert icinga2_cdir.user == "nagios" assert icinga2_cdir.group == "nagios" -def test_icinga2_objects(host): - i2_oh = host.file("/etc/icinga2/zones.d/main/hosts") - assert i2_oh.is_directory - i2_oagent = host.file("/etc/icinga2/zones.d/main/hosts/agent.localdomain.conf") - assert i2_oagent.is_file - print(i2_oagent.content_string) - i2_oservice = host.file("/etc/icinga2/zones.d/main/services/services.conf") - assert i2_oservice.is_file - print(i2_oservice.content_string) +def test_icinga2_zones_dir(host): + i2_zones_dir = host.file("/etc/icinga2/zones.d/main/hosts") + if host.system_info.distribution == 'centos': + assert i2_zones_dir.is_directory + assert i2_zones_dir.user == "icinga" + assert i2_zones_dir.group == "icinga" + if host.system_info.distribution == 'debian': + assert i2_zones_dir.is_directory + assert i2_zones_dir.user == "nagios" + assert i2_zones_dir.group == "nagios" + +def test_icinga2_object_host(host): + i2_file = host.file("/etc/icinga2/zones.d/main/hosts/agent.localdomain.conf") + print(i2_file.content_string) + assert i2_file.is_file + assert i2_file.contains('object Zone "agent.localdomain" {') + assert i2_file.contains('object Endpoint "agent.localdomain" {') + assert i2_file.contains('object Host "agent.localdomain" {') + if host.system_info.distribution == 'centos': + assert i2_file.user == "icinga" + assert i2_file.group == "icinga" + assert i2_file.mode == 0o644 + if host.system_info.distribution == 'debian': + assert i2_file.user == "nagios" + assert i2_file.group == "nagios" + assert i2_file.mode == 0o644 + +def test_icinga2_object_service(host): + i2_file = host.file("/etc/icinga2/zones.d/main/services/services.conf") + print(i2_file.content_string) + assert i2_file.is_file + assert i2_file.contains('apply Service "ping" {') + assert i2_file.contains('template Service "generic-service" {') + if host.system_info.distribution == 'centos': + assert i2_file.user == "icinga" + assert i2_file.group == "icinga" + assert i2_file.mode == 0o644 + if host.system_info.distribution == 'debian': + assert i2_file.user == "nagios" + assert i2_file.group == "nagios" + assert i2_file.mode == 0o644 + +def test_icinga2_certificate(host): + i2_file = host.file("/var/lib/icinga2/certs/icinga-default.crt") + assert i2_file.is_file + if host.system_info.distribution == 'centos': + assert i2_file.user == "icinga" + assert i2_file.group == "icinga" + assert i2_file.mode == 0o644 + if host.system_info.distribution == 'debian': + assert i2_file.user == "nagios" + assert i2_file.group == "nagios" + assert i2_file.mode == 0o644 + +def test_icinga2_crt_key(host): + i2_file = host.file("/var/lib/icinga2/certs/icinga-default.key") + assert i2_file.is_file + if host.system_info.distribution == 'centos': + assert i2_file.user == "icinga" + assert i2_file.group == "icinga" + assert i2_file.mode == 0o600 + if host.system_info.distribution == 'debian': + assert i2_file.user == "nagios" + assert i2_file.group == "nagios" + assert i2_file.mode == 0o600 def test_icinga2_dir(host): icinga2_dir = host.file("/etc/icinga2") assert icinga2_dir.is_directory -def test_icinga2_features(host): - f_checker = host.file("/etc/icinga2/features-enabled/checker.conf") - f_mainlog = host.file("/etc/icinga2/features-enabled/mainlog.conf") - f_api = host.file("/etc/icinga2/features-enabled/api.conf") - assert f_checker.exists - assert f_checker.linked_to == "/etc/icinga2/features-available/checker.conf" - assert f_mainlog.exists - assert f_mainlog.linked_to == "/etc/icinga2/features-available/mainlog.conf" - assert f_api.exists - assert f_api.linked_to == "/etc/icinga2/features-available/api.conf" +def test_icinga2_feature_checker(host): + i2_file = host.file("/etc/icinga2/features-enabled/checker.conf") + assert i2_file.exists + assert i2_file.linked_to == "/etc/icinga2/features-available/checker.conf" + if host.system_info.distribution == 'centos': + assert i2_file.user == "root" + assert i2_file.group == "root" + if host.system_info.distribution == 'debian': + assert i2_file.user == "root" + assert i2_file.group == "root" + +def test_icinga2_feature_mainlog(host): + i2_file = host.file("/etc/icinga2/features-enabled/mainlog.conf") + assert i2_file.exists + assert i2_file.linked_to == "/etc/icinga2/features-available/mainlog.conf" + if host.system_info.distribution == 'centos': + assert i2_file.user == "root" + assert i2_file.group == "root" + if host.system_info.distribution == 'debian': + assert i2_file.user == "root" + assert i2_file.group == "root" + +def test_icinga2_feature_api(host): + i2_file = host.file("/etc/icinga2/features-enabled/api.conf") + assert i2_file.exists + assert i2_file.linked_to == "/etc/icinga2/features-available/api.conf" + if host.system_info.distribution == 'centos': + assert i2_file.user == "root" + assert i2_file.group == "root" + if host.system_info.distribution == 'debian': + assert i2_file.user == "root" + assert i2_file.group == "root" diff --git a/roles/icinga2/tasks/configure.yml b/roles/icinga2/tasks/configure.yml index 440a2976..3347abdf 100644 --- a/roles/icinga2/tasks/configure.yml +++ b/roles/icinga2/tasks/configure.yml @@ -84,6 +84,8 @@ src: "{{ item.path }}" dest: "{{ item.path |regex_replace('^'+icinga2_fragments_path, '/etc/icinga2') }}" delimiter: ' ' + owner: "{{ icinga2_user }}" + group: "{{ icinga2_group }}" loop: "{{ result.files }}" notify: reload icinga2 service diff --git a/roles/icinga2/tasks/features/api.yml b/roles/icinga2/tasks/features/api.yml index 9029a1db..c4fb4582 100644 --- a/roles/icinga2/tasks/features/api.yml +++ b/roles/icinga2/tasks/features/api.yml @@ -139,6 +139,8 @@ copy: src: "{{ icinga2_ca_path }}/ca.crt" dest: "{{ icinga2_cert_path }}/ca.crt" + owner: "{{ icinga2_user }}" + group: "{{ icinga2_group }}" remote_src: yes when: icinga2_ca_host == 'none' when: (icinga2_ssl_cert_path.stat.exists == false or icinga2_ssl_key_path.stat.exists == false or icinga2_force_newcert) and icinga2_ssl_cacert is not defined From 7d99d98e8b53292c38ca734001b36244e900f635 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Fri, 12 Nov 2021 12:02:16 +0100 Subject: [PATCH 28/29] changed checked files --- .../default/tests/integration/test_icinga2.py | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/molecule/default/tests/integration/test_icinga2.py b/molecule/default/tests/integration/test_icinga2.py index f6434488..9ef97451 100644 --- a/molecule/default/tests/integration/test_icinga2.py +++ b/molecule/default/tests/integration/test_icinga2.py @@ -83,34 +83,37 @@ def test_icinga2_dir(host): assert icinga2_dir.is_directory def test_icinga2_feature_checker(host): - i2_file = host.file("/etc/icinga2/features-enabled/checker.conf") + i2_file = host.file("/etc/icinga2/features-available/checker.conf") + i2_link = host.file("/etc/icinga2/features-enabled/checker.conf") assert i2_file.exists - assert i2_file.linked_to == "/etc/icinga2/features-available/checker.conf" + assert i2_link.linked_to == "/etc/icinga2/features-available/checker.conf" if host.system_info.distribution == 'centos': - assert i2_file.user == "root" - assert i2_file.group == "root" + assert i2_file.user == "icinga" + assert i2_file.group == "icinga" if host.system_info.distribution == 'debian': - assert i2_file.user == "root" - assert i2_file.group == "root" + assert i2_file.user == "nagios" + assert i2_file.group == "nagios" def test_icinga2_feature_mainlog(host): - i2_file = host.file("/etc/icinga2/features-enabled/mainlog.conf") + i2_file = host.file("/etc/icinga2/features-available/mainlog.conf") + i2_link = host.file("/etc/icinga2/features-enabled/mainlog.conf") assert i2_file.exists - assert i2_file.linked_to == "/etc/icinga2/features-available/mainlog.conf" + assert i2_link.linked_to == "/etc/icinga2/features-available/mainlog.conf" if host.system_info.distribution == 'centos': - assert i2_file.user == "root" - assert i2_file.group == "root" + assert i2_file.user == "icinga" + assert i2_file.group == "icinga" if host.system_info.distribution == 'debian': - assert i2_file.user == "root" - assert i2_file.group == "root" + assert i2_file.user == "nagios" + assert i2_file.group == "nagios" def test_icinga2_feature_api(host): - i2_file = host.file("/etc/icinga2/features-enabled/api.conf") + i2_file = host.file("/etc/icinga2/features-available/api.conf") + i2_link = host.file("/etc/icinga2/features-enabled/api.conf") assert i2_file.exists - assert i2_file.linked_to == "/etc/icinga2/features-available/api.conf" + assert i2_link.linked_to == "/etc/icinga2/features-available/api.conf" if host.system_info.distribution == 'centos': - assert i2_file.user == "root" - assert i2_file.group == "root" + assert i2_file.user == "icinga" + assert i2_file.group == "icinga" if host.system_info.distribution == 'debian': - assert i2_file.user == "root" - assert i2_file.group == "root" + assert i2_file.user == "nagios" + assert i2_file.group == "nagios" From 186e0c6c46f7ee1c1141d421263e092a9adce241 Mon Sep 17 00:00:00 2001 From: Thilo W Date: Mon, 15 Nov 2021 08:43:57 +0100 Subject: [PATCH 29/29] removed check config for testing --- roles/icinga2/tasks/configure.yml | 9 --------- 1 file changed, 9 deletions(-) diff --git a/roles/icinga2/tasks/configure.yml b/roles/icinga2/tasks/configure.yml index 3347abdf..e21c99aa 100644 --- a/roles/icinga2/tasks/configure.yml +++ b/roles/icinga2/tasks/configure.yml @@ -104,12 +104,3 @@ when: item.split('/')[3] == 'conf.d' or item.split('/')[3] == 'zones.d' loop: "{{ _empty_result.stdout_lines }}" notify: reload icinga2 service - -- name: check config - ansible.builtin.command: icinga2 daemon -C - changed_when: false - register: da - ignore_errors: true - -- debug: - var: da.stdout