Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
i2_constants refactor (#13)
Browse files Browse the repository at this point in the history
* Add ability to manage icinga2.conf

* Enable management of constants.conf

closes #9

* Update README.md

* Cleanup test.yml

* Refactor i2_const_* vars to i2_constants dict

Using the combine filter, we can remove the `i2_const_*` variables and
have a generic `i2_constants` dictionary from which we can read the
user-specified constants (`i2_custom_constants`) and our defaults
(`i2_default_constants`). We lose the comments in the process however.

* Copy constants file before overriding OS defaults
  • Loading branch information
RossBarnie authored and aflatto committed Jun 13, 2018
1 parent 59b9fde commit 0fd8afe
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 46 deletions.
30 changes: 18 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,7 @@ You may choose to use your own or the systems default repositories. Repository m
- [Variable: i2_i2_yum_url](#variable-i2_yum_url)
- [Variable: i2_confd](#variable-i2_confd)
- [Variable: i2_include_plugins](#variable-i2_include_plugins)
- [Variable: i2_const_plugindir](#variable-i2_const_plugindir)
- [Variable: i2_const_manubulonplugindir](#variable-i2_const_manubulonplugindir)
- [Variable: i2_const_plugincontribdir](#variable-i2_const_plugincontribdir)
- [Variable: i2_const_nodename](#variable-i2_const_nodename)
- [Variable: i2_const_zonename](#variable-i2_const_zonename)
- [Variable: i2_const_ticketsalt](#variable-i2_const_ticketsalt)
- [Variable: i2_constants](#variable-i2_constants)
- [Variable: i2_custom_constants](#variable-i2_custom_constants)
- [**Variables system specific](#variables-os-specific)
- [Variable: i2_conf_dir](#variable-i2_conf_dir)
- [Variable: i2_user](#variable-i2_user)
Expand Down Expand Up @@ -131,17 +125,29 @@ Set `ZoneName` constant. Defaults to `{{ ansible_fqdn }}`.
#### Variable: `i2_const_ticketsalt`
Set `TicketSalt` constant. Empty by default.

#### Variable: `i2_constants`
Add custom constants to `constants.conf`. Must be a dictionary.
#### Variable: `i2_custom_constants`
Add custom constants to `constants.conf`. Must be a dictionary. Defaults to: `{}`

Example:
Some default required values are specified in `i2_default_constants` and merged with this variable. Use this variable to override these default values, or add your own constants.

Default values of `i2_default_constants`:
```yaml
PluginDir: "{{ i2_lib_dir }}/nagios/plugins"
ManubulonPluginDir: "{{ i2_lib_dir }}/nagios/plugins"
PluginContribDir: "{{ i2_lib_dir }}/nagios/plugins"
NodeName: "{{ ansible_fqdn }}"
ZoneName: "{{ ansible_fqdn }}"
TicketSalt: ""
```

Example usage:
```yaml
vars:
- i2_constants:
foo: "bar"
TicketSalt: "My ticket salt"
Foo: "bar"
```


### Variables system specific
The following variables are system specific and don't need to be overwritten in most cases. Be careful when making
changes to any of these variables.
Expand Down
8 changes: 1 addition & 7 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,4 @@ i2_include_plugins:
- "manubulon"
- "windows-plugins"
- "nscp"
i2_const_plugindir: "{{ i2_lib_dir }}/nagios/plugins"
i2_const_manubulonplugindir: "{{ i2_lib_dir }}/nagios/plugins"
i2_const_plugincontribdir: "{{ i2_lib_dir }}/nagios/plugins"
i2_const_nodename: "{{ ansible_fqdn }}"
i2_const_zonename: "{{ ansible_fqdn }}"
i2_const_ticketsalt: ""
i2_constants: {}
i2_custom_constants: {}
16 changes: 16 additions & 0 deletions tasks/icinga2-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
mode: 0644
notify: reload icinga2

- name: Check for constants sample file
stat:
path: "{{ i2_conf_dir }}/constants.conf-sample"
register: sample_file

- name: Copy original Icinga 2 config to sample
become: yes
copy:
remote_src: true
src: "{{ i2_conf_dir }}/constants.conf"
dest: "{{ i2_conf_dir }}/constants.conf-sample"
owner: "{{ i2_user }}"
group: "{{ i2_group }}"
mode: 0644
when: not sample_file.stat.exists

- name: Manage Icinga 2 constants
become: yes
template:
Expand Down
28 changes: 1 addition & 27 deletions templates/constants.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,6 @@
* the other configuration files.
*/

/* The directory which contains the plugins from the Monitoring Plugins project. */
const PluginDir = "{{ i2_const_plugindir }}"

/* The directory which contains the Manubulon plugins.
* Check the documentation, chapter "SNMP Manubulon Plugin Check Commands", for details.
*/
const ManubulonPluginDir = "{{ i2_const_manubulonplugindir }}"

/* The directory which you use to store additional plugins which ITL provides user contributed command definitions for.
* Check the documentation, chapter "Plugins Contribution", for details.
*/
const PluginContribDir = "{{ i2_const_plugincontribdir }}"

/* Our local instance name. By default this is the server's hostname as returned by `hostname --fqdn`.
* This should be the common name from the API certificate.
*/
const NodeName = "{{ i2_const_nodename }}"

/* Our local zone name. */
const ZoneName = "{{ i2_const_zonename }}"

/* Secret key for remote node tickets */
const TicketSalt = "{{ i2_const_ticketsalt }}"

/* Additional Constants */

{% for name, value in i2_constants.items() %}
const {{ name }} = "{{ value }}""
const {{ name }} = "{{ value }}"
{% endfor %}
8 changes: 8 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
---
i2_conf_dir: "/etc/icinga2"
i2_default_constants:
PluginDir: "{{ i2_lib_dir }}/nagios/plugins"
ManubulonPluginDir: "{{ i2_lib_dir }}/nagios/plugins"
PluginContribDir: "{{ i2_lib_dir }}/nagios/plugins"
NodeName: "{{ ansible_fqdn }}"
ZoneName: "{{ ansible_fqdn }}"
TicketSalt: ""
i2_constants: "{{ i2_default_constants | combine(i2_custom_constants) }}"

0 comments on commit 0fd8afe

Please sign in to comment.