Skip to content

Commit

Permalink
Add molecule for role icinga_plugins (#18)
Browse files Browse the repository at this point in the history
* added Molecule tests for centos7, rocky linux 8 and rocky linux 8

Co-authored-by: xFuture603 <[email protected]>
Co-authored-by: Christoph <[email protected]>
  • Loading branch information
3 people authored Feb 25, 2022
1 parent ae1d36d commit 5ce7c5e
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 15 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,18 @@ jobs:
python3 -m pip install -r requirements.txt
working-directory: ansible_collections/t_systems_mms/ansible_collection_icinga

- name: Test with molecule
- name: Test role icinga_agent with molecule
run: molecule test
working-directory: ansible_collections/t_systems_mms/ansible_collection_icinga/roles/icinga_agent
env:
PY_COLORS: '1'
ANSIBLE_FORCE_COLOR: '1'
MOLECULE_DISTRO: ${{ matrix.distro }}

- name: Test role icinga_plugins with molecule
run: molecule test
working-directory: ansible_collections/t_systems_mms/ansible_collection_icinga/roles/icinga_plugins
env:
PY_COLORS: '1'
ANSIBLE_FORCE_COLOR: '1'
MOLECULE_DISTRO: ${{ matrix.distro }}
12 changes: 0 additions & 12 deletions roles/icinga_agent/molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,6 @@
- "result_api_conf.group == 'nagios'"
when: ansible_os_family == 'Debian'

# - name: verify that icinga2 package is installed
# yum:
# name: icinga2
# state: present
# when: ansible_os_family == 'RedHat'

# - name: verify that icinga2 package is installed for Debian based systems
# apt:
# name: icinga2
# state: present
# when: ansible_os_family == 'Debian'

- name: verify that icinga2 package is installed
ansible.builtin.package:
name: icinga2
Expand Down
2 changes: 1 addition & 1 deletion roles/icinga_plugins/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
icinga2_plugins_basedir:
Debian: /usr/lib/nagios
Redhat: /usr/lib64/nagios
RedHat: /usr/lib64/nagios

icinga2_plugins_pluginsdir: "{{ icinga2_plugins_basedir[ansible_os_family] }}/plugins"

Expand Down
35 changes: 35 additions & 0 deletions roles/icinga_plugins/molecule/default/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
- name: Converge
hosts: all
collections:
- t_systems_mms.ansible_collection_icinga
pre_tasks:

- name: set name of installation package based on distro
set_fact:
icinga_install_plugins:
- nagios-plugins-all
when:
- ansible_os_family == 'RedHat'
- ansible_facts.distribution_major_version | int is version('8', '<')

- name: set name of installation package based on distro
set_fact:
icinga_install_plugins:
- monitoring-plugins
when: ansible_os_family == 'Debian'

- name: add icinga group
ansible.builtin.group:
name: icinga
state: present
when: ansible_os_family == 'RedHat'

- name: add icinga user with icinga group
ansible.builtin.user:
name: icinga
group: icinga
when: ansible_os_family == 'RedHat'

roles:
- icinga_plugins
15 changes: 15 additions & 0 deletions roles/icinga_plugins/molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: instance
image: "geerlingguy/docker-${MOLECULE_DISTRO}-ansible:latest"
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
privileged: true
pre_build_image: true
command: ""
provisioner:
name: ansible
64 changes: 64 additions & 0 deletions roles/icinga_plugins/molecule/default/prepare.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
- name: Prepare
hosts: all
tasks:
- name: install epel repo
yum:
name: epel-release
state: installed
when: ansible_os_family == 'RedHat'

- name: import icinga2 key
command: "rpm --import https://packages.icinga.com/icinga.key"
when: ansible_os_family == 'RedHat'

- name: install icinga2 repo
command: "yum install https://packages.icinga.com/epel/icinga-rpm-release-7-latest.noarch.rpm -y"
when:
- ansible_os_family == 'RedHat'
- ansible_facts.distribution_major_version | int is version('7', '=')

- name: install icinga2 repo
command: "yum install https://packages.icinga.com/epel/icinga-rpm-release-8-latest.noarch.rpm -y"
when:
- ansible_os_family == 'RedHat'
- ansible_facts.distribution_major_version | int is version('8', '=')

- name: install icinga2 basic packages
apt:
name: gnupg
state: present
update_cache: true
when: ansible_os_family == 'Debian'

- name: add icinga2 repo key
apt_key:
url: "http://packages.icinga.com/icinga.key"
state: present
when: ansible_os_family == 'Debian'

- name: add icinga2 repository on debian 11
apt_repository:
repo: "{{ item }}"
state: present
update_cache: true
with_items:
- deb http://packages.icinga.com/debian icinga-bullseye main
- deb-src http://packages.icinga.com/debian icinga-bullseye main
when:
- ansible_os_family == 'Debian'
- ansible_facts.distribution_major_version | int is version('11', '=')

- name: install icinga2 build dependencies
apt:
pkg: "icinga2"
state: "build-dep"
update_cache: true
when: ansible_os_family == 'Debian'

- name: install icinga2 basic packages
apt:
name: "icinga2"
state: "present"
update_cache: true
when: ansible_os_family == 'Debian'
74 changes: 74 additions & 0 deletions roles/icinga_plugins/molecule/default/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
- name: Verify
hosts: all
tasks:
- name: set name of installation package based on distro
set_fact:
icinga_install_plugins:
- nagios-plugins-all
when:
- ansible_os_family == 'RedHat'
- ansible_facts.distribution_major_version | int is version('8', '<')

- name: set name of installation package based on distro
set_fact:
icinga_install_plugins:
- monitoring-plugins
when: ansible_os_family == 'Debian'

- name: include default vars from icinga_plugins roles
include_vars:
file: ../../defaults/main.yml

- name: verify that plugin dir exists
file:
path: "{{ icinga2_plugins_pluginsdir }}"
state: directory
register: result_plugin_dir

- name: validate plugin dir exists on system
assert:
that:
- "result_plugin_dir.state == 'directory'"
- "result_plugin_dir.mode == '0755'"
- "result_plugin_dir.owner == 'root'"
- "result_plugin_dir.group == 'root'"

- name: search for all files in plugins directory
ansible.builtin.find:
paths: "{{ icinga2_plugins_pluginsdir }}"
recurse: yes
file_type: file
register: files_in_plugins_dir
check_mode: no

- name: validate plugins belongs to the right user for Redhat based systems
assert:
that:
- "item.pw_name == 'icinga'"
- "item.gr_name == 'icinga'"
loop: "{{ files_in_plugins_dir.files }}"
when: ansible_os_family == 'RedHat'

- name: validate plugins belongs to the right user for Debian based systems
assert:
that:
- "item.pw_name == 'nagios'"
- "item.gr_name == 'nagios'"
loop: "{{ files_in_plugins_dir.files }}"
when: ansible_os_family == 'Debian'

- name: verify that package is installed
package:
name: "{{ icinga_install_plugins }}"
state: present
register: result_installation
when:
- icinga_install_plugins is defined

- name: validate package is installed
assert:
that:
- "result_installation is not changed"
when:
- icinga_install_plugins is defined
2 changes: 1 addition & 1 deletion roles/icinga_plugins/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
when:
- icinga_install_plugins is defined
- icinga_install_plugins | length > 0
- ansible_facts.os_family == "RedHat" and ansible_facts.distribution_major_version | int is version('7', "=") or ansible_facts.os_family == "Debian"
- (ansible_facts.os_family == "RedHat" and ansible_facts.distribution_major_version | int is version('7', "=")) or ansible_facts.os_family == "Debian"

- name: copy icinga plugins to target node
ansible.builtin.copy:
Expand Down

0 comments on commit 5ce7c5e

Please sign in to comment.