Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor task names for clarity and consistency across Icinga2 role #364

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/role-icinga2/features/feature-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Example if connection and ticket creation should be on the satellite:
icinga2_features:
- name: api
ca_host: icinga-satellite.localdomain
ticket_salt: "{{ icinga2_constants.ticket_salt }}"
ticket_salt: "{{ icinga2_constants.TicketSalt }}"
[...]
icinga2_delegate_host: icinga-satellite.localdomain
```
Expand All @@ -83,7 +83,7 @@ master host.
icinga2_features:
- name: api
ca_host: icinga-satellite.localdomain
ticket_salt: "{{ icinga2_constants.ticket_salt }}"
ticket_salt: "{{ icinga2_constants.TicketSalt }}"
[...]
icinga2_delegate_host: icinga-master.localdomain
```
Expand Down
6 changes: 3 additions & 3 deletions plugins/action/icinga2_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def run(self, tmp=None, task_vars=None):
args = merge_hash(args.pop('args', {}), args)
object_type = args.pop('type', None)

if object_type not in task_vars['icinga2_object_types']:
if object_type not in task_vars['__icinga2_object_types']:
raise AnsibleError('unknown Icinga object type: %s' % object_type)

#
Expand Down Expand Up @@ -60,7 +60,7 @@ def run(self, tmp=None, task_vars=None):
#
# quoting of object name?
#
if obj['name'] not in task_vars['icinga2_combined_constants']:
if obj['name'] not in task_vars['__icinga2_combined_constants']:
object_name = '"' + obj['name'] + '"'
else:
object_name = obj['name']
Expand Down Expand Up @@ -111,7 +111,7 @@ def run(self, tmp=None, task_vars=None):
#
object_content += Icinga2Parser().parse(
obj['args'],
list(task_vars['icinga2_combined_constants'].keys()) + task_vars['icinga2_reserved'] + varlist + list(obj['args'].keys()),
list(task_vars['__icinga2_combined_constants'].keys()) + task_vars['__icinga2_reserved'] + varlist + list(obj['args'].keys()),
2
) + '}\n'
copy_action = self._task.copy()
Expand Down
10 changes: 9 additions & 1 deletion roles/icinga2/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
icinga2_packages: ["icinga2"]
icinga2_fragments_path: /var/tmp/icinga
icinga2_config_path: /etc/icinga2
icinga2_state: started
icinga2_enabled: true
icinga2_confd: true
icinga2_ca_path: /var/lib/icinga2/ca
icinga2_cert_path: /var/lib/icinga2/certs
icinga2_plugins:
- plugins
- plugins-contrib
Expand All @@ -14,6 +18,10 @@ icinga2_features:
- name: checker
- name: notification
- name: mainlog
icinga2_remote_objects: []
_icinga2_custom_conf_paths: []
icinga2_config_host: "{{ ansible_fqdn }}"

icinga2_config_directories:
- zones.d/main/commands
- zones.d/main/hosts
- zones.d/main/services
149 changes: 149 additions & 0 deletions roles/icinga2/meta/argument_specs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
---
argument_specs:
main:
short_description: configure or manage Icinga 2 server and agents
description:
- Role to install, configure or manage Icinga 2 server and agents.
author: |
- Lennart Betz <[email protected]>
- Thilo Wening <[email protected]>
- Thomas Widhalm <[email protected]>
options:
icinga2_packages:
description: list of packages to be installed
type: list
elements: str
default: ["icinga2"]
icinga2_packages_dependencies:
description:
- list of packages dependancies to be installed in addition to packages
- OS Specific
type: list
elements: str
ansible_selinux: # naming is wrong, should be prefixed with the role name, e.g. icinga2_selinux
description:
- The Icinga 2 role will automatically detect via Ansible facts if SELinux is enabled on the system.
- If this is the case the package icinga2-selinux will be automatically installed.
- If the package should be installed, even if SELinux is not enabled or somehow wrongly disabled in Ansible use the following variable.
type: dict
options:
status:
description: Force installation of package `icinga2-selinux`
type: str
choices:
- enabled
- disabled
icinga2_fragments_path:
description: Base installation folder of all icinga content
default: /var/tmp/icinga
icinga2_config_path:
description: folderpath for icinga configuration
default: /etc/icinga2
icinga2_user:
description: local icinga2 user
icinga2_group:
description: local group for O(icinga2_user)
icinga2_constants:
description:
- Define constants in **constants.conf**.
- usefull to define the O(icinga2_constants.NodeName) and O(icinga2_constants.ZoneName)
- usefull to define the salt
type: dict
options:
PluginDir:
description: the plugin folderpath
default: /usr/lib/nagios/plugins
ManubulonPluginDir:
description: manabulon plugin folderpath
default: /usr/lib/nagios/plugins
PluginContribDir:
description: contrib plugins folderpath
default: /usr/lib/nagios/plugins
NodeName:
description: >
- node name
- ex: satellite.localdomain
default: ansible_fqdn
ZoneName:
description: >
- NodeName
- ex: zone-satellite-d1
default: NodeName
TicketSalt:
description: salt
default: ''
icinga2_features:
description:
- List of features and their configuration settings to be set
- Each feature has its own attributes
- Check L(Documentation Icinga 2 Features,https://icinga.com/docs/icinga-2/latest/doc/09-object-types/#features)
type: list
elements: dict
default: >-
- name: checker
- name: notification
- name: mainlog
icinga2_config_host:
description: hostname of the configuration
default: ansible_fqdn
icinga2_confd:
description:
- If the local **conf.d** directory shouldn't be recursively included then the var O(icinga2_confd) should be set to `false`.
- >-
Otherwise you can use a directory name to set the include to a different folder
than **conf.d**. The folder needs to exist below /etc/icinga2. If it should be created by the role use the variable O(icinga2_config_directories) in addition.
type: str

icinga2_config_directories:
description:
- List of configuration directories to be created
- Those directories are only managed when they are part of `zones.d`, `conf.d` or the variable O(icinga2_confd).
type: list
elements: str
default:
- zones.d/main/commands
- zones.d/main/hosts
- zones.d/main/services
icinga2_ca_path:
description: CA folderpath, used for the API
type: str
default: /var/lib/icinga2/ca
icinga2_cert_path:
description: path for certificate to be used by API
type: str
default: /var/lib/icinga2/certs
icinga2_state:
description: expected state of the icinga2 service
choices: [ reloaded, restarted, started, stopped ]
default: started
icinga2_enabled:
description: expected status of the icinga2 service
type: bool
default: true
icinga2_plugins:
description: list of plugins to be included in the icinga2.conf
type: list
elements: str
default:
- plugins
- plugins-contrib
- windows-plugins
- nscp
icinga2_purge_features:
description: >-
Decides whether the unmanaged features should be purged or not. Default: true
type: bool
default: true
icinga2_delegate_host:
description: >-
The role primarily delegates the ticket creation to the Icinga ca host.
If the host is not listed with the same name in Ansible, you can set the name of the host in Ansible with this variable.
example: `icinga2_delegate_host: icinga-master`
type: str
# TODO: change logic to make it more separated since not possible to validate as item.split
# icinga2_objects:
# description: >-
# List of objects for which configuration file will generated.
# COMPLEX object, can be either dict containing list of dict (host vars), or list of dict...
# type: list
# elements: dict
70 changes: 35 additions & 35 deletions roles/icinga2/tasks/configure.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
---
- name: populate features (icinga2_dict_features)
set_fact:
icinga2_dict_features: "{{ icinga2_dict_features|default({}) | combine({ item.name: item }) }}"
- name: configure | Populate features (__icinga2_dict_features)
ansible.builtin.set_fact:
__icinga2_dict_features: "{{ __icinga2_dict_features | default({}) | combine({item.name: item}) }}"
with_items: "{{ icinga2_features }}"

- name: main config file {{ icinga2_config_path + '/icinga2.conf' }}
template:
- name: configure | Main config file {{ icinga2_config_path + '/icinga2.conf' }}
ansible.builtin.template:
src: icinga2.conf.j2
dest: "{{ icinga2_config_path + '/icinga2.conf' }}"
owner: "{{ icinga2_user }}"
group: "{{ icinga2_group }}"
notify: check-and-reload-icinga2-service

- name: merge defaults and user specified constants (set_fact icinga2_combined_constants)
set_fact:
icinga2_combined_constants: "{{ icinga2_default_constants | combine(icinga2_constants) }}"
- name: configure | Merge defaults and user specified constants (set_fact __icinga2_combined_constants)
ansible.builtin.set_fact:
__icinga2_combined_constants: "{{ __icinga2_default_constants | combine(icinga2_constants) }}"

- name: set constants in {{ icinga2_config_path + '/constants.conf' }}
- name: configure | Set constants in {{ icinga2_config_path + '/constants.conf' }}
ansible.builtin.template:
src: constants.conf.j2
dest: "{{ icinga2_config_path + '/constants.conf' }}"
owner: "{{ icinga2_user }}"
group: "{{ icinga2_group }}"
notify: check-and-reload-icinga2-service

- name: features
include_tasks: features.yml
- name: configure | Features
ansible.builtin.include_tasks: features.yml

- name: objects
include_tasks: objects.yml
- name: configure | Objects
ansible.builtin.include_tasks: objects.yml

- name: ensure monitoring config directories
- name: configure | Ensure monitoring config directories
ansible.builtin.file:
path: "{{ icinga2_config_path }}/{{ item }}"
state: directory
owner: "{{ item.owner | default(icinga2_user) }}"
group: "{{ item.group | default(icinga2_group) }}"
owner: "{{ icinga2_user }}"
group: "{{ icinga2_group }}"
loop: "{{ icinga2_config_directories }}"
when:
- icinga2_config_directories is defined
Expand All @@ -48,66 +48,66 @@
# - my_own_config.d
#- item.split('/')[0] in icinga2_local_config or item.split('/')[0] == 'zones.d'

- name: collect config fragments
find:
- name: configure | Collect config fragments
ansible.builtin.find:
path: "{{ icinga2_fragments_path }}"
recurse: yes
file_type: file
register: result_frag

- name: cleanup config files
file:
- name: configure | Cleanup config files
ansible.builtin.file:
state: absent
dest: "{{ item.path }}"
loop: "{{ result_frag.files }}"
when:
- item.path not in icinga2_local_objects
- item.path not in __icinga2_local_objects
- item.path not in _icinga2_custom_conf_paths

- name: collect empty config dirs
shell: >-
- name: configure | Collect empty config dirs
ansible.builtin.shell: >-
find {{ icinga2_fragments_path }} -mindepth 1 -type d -empty
register: _empty_result
check_mode: false
changed_when: _empty_result.stdout_lines |length > 0

- name: remove empty config dirs
file:
- name: configure | Remove empty config dirs
ansible.builtin.file:
state: absent
path: "{{ item }}"
loop: "{{ _empty_result.stdout_lines }}"

- name: collect config files
find:
- name: configure | Collect config files
ansible.builtin.find:
path: "{{ icinga2_fragments_path }}"
recurse: yes
file_type: directory
pattern: '*.conf'
register: result

- name: assemble config files
- name: configure | Assemble config files
ansible.builtin.assemble:
src: "{{ item.path }}"
dest: "{{ item.path |regex_replace('^'+icinga2_fragments_path, '/etc/icinga2') }}"
dest: "{{ item.path | regex_replace('^' + icinga2_fragments_path, '/etc/icinga2') }}"
delimiter: ' '
owner: "{{ icinga2_user }}"
group: "{{ icinga2_group }}"
mode: 0644
loop: "{{ result.files }}"
notify: check-and-reload-icinga2-service

- name: enable features
file:
- name: configure | Enable features
ansible.builtin.file:
state: "{{ 'link' if (item.state is undefined or item.state != 'absent') else 'absent' }}"
path: "{{ '/etc/icinga2/features-enabled/' + icinga2_feature_realname[item.name]|default(item.name) + '.conf' }}"
src: "{{ '../features-available/' + icinga2_feature_realname[item.name]|default(item.name) + '.conf' if (item.state is undefined or item.state != 'absent') else omit }}"
path: "{{ '/etc/icinga2/features-enabled/' + __icinga2_feature_realname[item.name] | default(item.name) + '.conf' }}"
src: "{{ '../features-available/' + __icinga2_feature_realname[item.name] | default(item.name) + '.conf' if (item.state is undefined or item.state != 'absent') else omit }}"
loop: "{{ icinga2_features }}"
notify: check-and-reload-icinga2-service

- name: remove empty config files
- name: configure | Remove empty config files
ansible.builtin.file:
state: absent
path: "{{ item |regex_replace('^'+icinga2_fragments_path, '/etc/icinga2') }}"
path: "{{ item | regex_replace('^' + icinga2_fragments_path, '/etc/icinga2') }}"
when: item.split('/')[icinga2_fragments_path.split('/')|length] == 'conf.d' or item.split('/')[icinga2_fragments_path.split('/')|length] == 'zones.d'
loop: "{{ _empty_result.stdout_lines }}"
notify: check-and-reload-icinga2-service
Loading