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

add custom config files #91

Merged
merged 5 commits into from
Aug 1, 2022
Merged
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
2 changes: 2 additions & 0 deletions molecule/default/converge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
vars:
icinga2_confd: false
icinga2_features:
- name: notification
- name: checker
- name: influxdb2
host: 127.0.0.1
Expand All @@ -22,6 +23,7 @@
endpoints:
- "{{ ansible_fqdn }}"
icinga2_config_directories:
- zones.d/main/commands
- zones.d/main/hosts
- zones.d/main/services
collections:
Expand Down
3 changes: 3 additions & 0 deletions molecule/default/files/icinga2_command
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object CheckCommand "test" {
command = [ PluginDir + "/bin/true" ]
}
5 changes: 5 additions & 0 deletions molecule/default/host_vars/icinga-default.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
icinga2_custom_config:
- name: icinga2_command
path: zones.d/main/commands/custom_commands.conf


icinga2_objects:
icinga-default:
- name: eventcommand-test
Expand Down
13 changes: 13 additions & 0 deletions molecule/default/tests/integration/test_custom_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def test_icinga2_custom_files(host):
i2_file = host.file("/etc/icinga2/zones.d/main/commands/custom_commands.conf")
print(i2_file.content_string)
assert i2_file.is_file
assert i2_file.contains('object CheckCommand "test" {')
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
10 changes: 7 additions & 3 deletions roles/icinga2/tasks/configure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
template:
src: icinga2.conf.j2
dest: "{{ icinga2_config_path + '/icinga2.conf' }}"
owner: "{{ icinga2_user }}"
group: "{{ icinga2_group }}"
notify: reload icinga2 service

- name: merge defaults and user specified constants (set_fact icinga2_combined_constants)
Expand All @@ -31,14 +33,16 @@
path: "{{ icinga2_fragments_path }}"
recurse: yes
file_type: file
register: result
register: result_frag

- name: cleanup config files
file:
state: absent
dest: "{{ item.path }}"
loop: "{{ result.files }}"
when: item.path not in icinga2_local_objects
loop: "{{ result_frag.files }}"
when:
- item.path not in icinga2_local_objects
- item.path not in _icinga2_custom_conf_paths

- name: collect empty config dirs
shell: >
Expand Down
23 changes: 23 additions & 0 deletions roles/icinga2/tasks/objects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,26 @@
icinga2_local_objects: "{{ icinga2_local_objects|default([]) + [item.dest] }}"
with_items: "{{ result.results }}"
when: result.results is defined

- set_fact:
_icinga2_custom_conf_paths: "{{ _icinga2_custom_conf_paths|default([]) + [ icinga2_fragments_path + '/' + item.path + '/' + item.order|default('20') + '_' + item.name] }}"
when: icinga2_custom_config is defined
loop: "{{ icinga2_custom_config }}"

- name: prepare custom config paths
file:
state: directory
owner: root
group: root
path: "{{ icinga2_fragments_path }}/{{ item.path }}/"
loop: "{{ icinga2_custom_config }}"


- name: add custom config to assemble
ansible.builtin.copy:
owner: root
group: root
src: "files/{{ item.name }}"
dest: "{{ icinga2_fragments_path }}/{{ item.path }}/{{ item.order|default('20') }}_{{ item.name }}"
loop: "{{ icinga2_custom_config }}"
when: icinga2_custom_config is defined and icinga2_custom_config|length > 0