Icinga / ansible-icinga2

Ansible Role for Icinga 2
Apache License 2.0
50 stars 23 forks source link

Generic Feature Handling #33

Closed mkayontour closed 5 years ago

mkayontour commented 5 years ago

Put some work into the generic feature handling, quick overview. I added two templates in jinja2:

I added a dictionary which handles all features and their attributes. By Default the dictionary is empty and won't manage features until some are defined.

The structure is easy and gives every user the ability to customize and add features.

i2_custom_features:
  ApiListener:                           #ObjectType
    api:                                       #ObjectName
      accept_command: true    #ObjectAttribute
      accept_config: true          #ObjectAttribute
  GraphiteWriter:
    graphite:
      host: "127.0.0.1"
      port: "2004"
    graphite2:
      host: "10.10.0.5"
      port: "2014"
  IdoMysqlConnection:
    mysql-ido:
      host: "127.0.0.1"
      port: 3306
      user: "icinga"
      password: "icinga"
      database: "icinga"
      cleanup: 
        downtimehistory_age: 48h
        contactnotifications_age: 31d

In addition I added a trigger that the user can remove unmanaged features to have the full control over all features.

How does this sound and look, please give feedback.

Test is available at the branch via molecule converge

refs #12

mkayontour commented 5 years ago

@aflatto can you have a look at?

aflatto commented 5 years ago

I am running some testing, when I am done I will merge it

aflatto commented 5 years ago

@mkayontour custom variable should not be in the /defaults but in the /vars as that is the place for customisation.

aflatto commented 5 years ago

I have added the following configuration to the defaults/main.yml file : i2_custom_features: { IdoMysqlConnection: mysql-ido: host: "127.0.0.1" port: 3306 user: "icinga" password: "icinga" database: "icinga" cleanup: downtimehistory_age: 48h contactnotifications_age: 31d } and run the ansible playbook - the result failed to generate the ido feature file. [root@ip-172-31-34-250 icinga2]# ll features-enabled/ total 0 lrwxrwxrwx. 1 root root 34 Nov 20 13:34 checker.conf -> ../features-available/checker.conf lrwxrwxrwx. 1 root root 34 Nov 20 13:34 mainlog.conf -> ../features-available/mainlog.conf lrwxrwxrwx. 1 root root 39 Nov 20 13:34 notification.conf -> ../features-available/notification.conf

Can you show me your tests ?

kwisatz commented 5 years ago

I tried this and I had to change the object_attributes.js template like so:

{% macro eval_value(key, val) %}
{%- if val is iterable and val is not string %}
{{ key }} = [ "{{ val | map('quote') | join('", "') }}" ]
- {%- elif val is match('^\d+(ms|s|h|d)?$') %}
+ {%- elif val is string and val is match('^\d+(ms|s|h|d)?$') %}
{{ key }} = {{ val }}
{%- elif val | bool == False and val | string == "false" %}
{{ key }} = {{ val }}
{%- else %}
{{ key }} = "{{ val | quote }}"
{% endif %}
{% endmacro %}

Otherwise it would fail, running the match on something that was neither a string or a buffer… booleans maybe?

kwisatz commented 5 years ago

For me, this still wouldn't pass. From a no or a false in a group_vars file, ansible/jinja2 would make the string False. The object_attributes.j2 thus had to be modified further as follows:

{%- elif val | bool == False and val | string | lower == "false" %}
{{ key }} = {{ val | lower }}