Skip to content

Commit

Permalink
Simplify role variables by removing one layer (#234)
Browse files Browse the repository at this point in the history
* simplify role variables by removing one layer

previously you could create multiple different objects with the same config:

```
     icinga_hostgroups:
      - hostgroup_object:
        - "service_abbreviation-environement"
        - "service_abbreviation-environement-web"
        state: present
```

However internally I don't see any broad adaption of this and it makes it harder to
use (the list after the list next to the key-value pairs is confusing) and it is harder
to write and maintain.

Compare:

```
 - name: icinga_hostgroup
   icinga_hostgroup:
     url: "{{ icinga_url }}"
@@ -11,15 +9,15 @@
     force_basic_auth: "{{ icinga_force_basic_auth | default(omit) }}"
     client_cert: "{{ icinga_client_cert | default(omit) }}"
     client_key: "{{ icinga_client_key | default(omit) }}"
    state: "{{ hostgroup.0.state | default(omit) }}"
    object_name: "{{ hostgroup.1 }}"
    display_name: "{{ hostgroup.0.display_name | default(omit) }}"
    assign_filter: "{{ hostgroup.0.assign_filter | default('host.name=\"' + hostgroup.1 + '-*\"') }}"
   retries: 3
   delay: 3
   register: result
   until: result is succeeded
  loop: "{{ icinga_hostgroups|subelements('hostgroup_object') }}"
   loop_control:
     loop_var: hostgroup
   tags: hostgroup
```

with:

```
 - name: icinga_hostgroup
   icinga_hostgroup:
     url: "{{ icinga_url }}"
@@ -11,15 +9,15 @@
     force_basic_auth: "{{ icinga_force_basic_auth | default(omit) }}"
     client_cert: "{{ icinga_client_cert | default(omit) }}"
     client_key: "{{ icinga_client_key | default(omit) }}"
    state: "{{ hostgroup.state | default(omit) }}"
    object_name: "{{ hostgroup.name }}"
    display_name: "{{ hostgroup.display_name | default(omit) }}"
    assign_filter: "{{ hostgroup.assign_filter | default('host.name=\"' + hostgroup.name + '-*\"') }}"
   retries: 3
   delay: 3
   register: result
   until: result is succeeded
   loop: "{{ icinga_hostgroups }}"
   loop_control:
     loop_var: hostgroup
   tags: hostgroup
```

Finally it allows us to define a role argument specification. This helps us prevent issues like
#34

* add tests for new role

* revert tests

* roll back automated ansible-lint fixes
  • Loading branch information
rndmh3ro authored Dec 6, 2023
1 parent f62560f commit 8119c52
Show file tree
Hide file tree
Showing 25 changed files with 482 additions and 368 deletions.
17 changes: 1 addition & 16 deletions roles/ansible_icinga/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
---
# icinga_timeperiod
icinga_timeperiods: []

# icinga_user_template
icinga_user_templates: []

# icinga_user
icinga_users: []
icinga_user_imports: []
icinga_user_disabled: false
icinga_user_email: null

icinga_user_email:
# icinga_endpoints
icinga_endpoints: []

# icinga_zone
icinga_zones: []

# icinga_hostgroup
icinga_hostgroups: []

# icinga_user_group
icinga_user_groups: []

# icinga_host_template
icinga_host_templates: []

# icinga_host
icinga_hosts: []
icinga_host_imports: []

# icinga_command_template
icinga_command_templates: []
icinga_command_template_command_type: PluginCheck
Expand All @@ -41,21 +32,15 @@ icinga_command_disabled: false

# icinga_service
icinga_services: []

# icinga_serviceset
icinga_servicesets: []

# icinga_service_template
icinga_service_templates: []

# icinga_service_apply
icinga_service_applies: []

# servicegroup
icinga_servicegroups: []

# icinga_notification
icinga_notifications: []

# icinga deploy
icinga_deploy_config: false
2 changes: 1 addition & 1 deletion roles/ansible_icinga/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
when: icinga_deploy_config and icinga_deploy_config is defined
listen: config_deploy
listen: config_deploy
26 changes: 12 additions & 14 deletions roles/ansible_icinga/tasks/icinga_command.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
---
# command.1 = command array
# command.0 = icinga_command attribute
- name: icinga_command
- name: Icinga_command
icinga_command:
url: "{{ icinga_url }}"
use_proxy: "{{ icinga_use_proxy | default(omit) }}"
Expand All @@ -11,21 +9,21 @@
force_basic_auth: "{{ icinga_force_basic_auth | default(omit) }}"
client_cert: "{{ icinga_client_cert | default(omit) }}"
client_key: "{{ icinga_client_key | default(omit) }}"
state: "{{ command.0.state | default(omit) }}"
command: "{{ command.0.command | default(omit) }}"
command_type: "{{ command.0.command_type | default(icinga_command_type) }}"
disabled: "{{ command.0.disabled | default(icinga_command_disabled) }}"
object_name: "{{ command.1 }}"
imports: "{{ command.0.imports | default(omit) }}"
timeout: "{{ command.0.timeout | default(omit) }}"
zone: "{{ command.0.zone | default(omit) }}"
vars: "{{ command.0.vars | default(omit) }}"
arguments: "{{ command.0.arguments | default(omit) }}"
state: "{{ command.state | default(omit) }}"
command: "{{ command.command | default(omit) }}"
command_type: "{{ command.command_type | default(icinga_command_type) }}"
disabled: "{{ command.disabled | default(icinga_command_disabled) }}"
object_name: "{{ command.name }}"
imports: "{{ command.imports | default(omit) }}"
timeout: "{{ command.timeout | default(omit) }}"
zone: "{{ command.zone | default(omit) }}"
vars: "{{ command.vars | default(omit) }}"
arguments: "{{ command.arguments | default(omit) }}"
retries: 3
delay: 3
register: result
until: result is succeeded
loop: "{{ icinga_commands|subelements('command_object') }}"
loop: "{{ icinga_commands }}"
loop_control:
loop_var: command
tags: command
Expand Down
28 changes: 13 additions & 15 deletions roles/ansible_icinga/tasks/icinga_command_template.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
---
# command_template.1 = command_template array
# command_template.0 = icinga_command_template attribute
- name: icinga_command_template
- name: Icinga_command_template
icinga_command_template:
url: "{{ icinga_url }}"
use_proxy: "{{ icinga_use_proxy | default(omit) }}"
Expand All @@ -11,22 +9,22 @@
force_basic_auth: "{{ icinga_force_basic_auth | default(omit) }}"
client_cert: "{{ icinga_client_cert | default(omit) }}"
client_key: "{{ icinga_client_key | default(omit) }}"
state: "{{ command_template.0.state | default(omit) }}"
object_name: "{{ command_template.1 }}"
display_name: "{{ command_template.0.display_name | default(omit) }}"
command: "{{ command_template.0.command | default(omit) }}"
command_type: "{{ command_template.0.command_type | default(icinga_command_template_command_type) }}"
timeout: "{{ command_template.0.timeout | default(omit) }}"
imports: "{{ command_template.0.imports | default(omit) }}"
disabled: "{{ command_template.0.disabled | default(omit) }}"
zone: "{{ command_template.0.zone | default(omit) }}"
vars: "{{ command_template.0.vars | default(omit) }}"
arguments: "{{ command_template.0.arguments | default(omit) }}"
state: "{{ command_template.state | default(omit) }}"
object_name: "{{ command_template.name }}"
display_name: "{{ command_template.display_name | default(omit) }}"
command: "{{ command_template.command | default(omit) }}"
command_type: "{{ command_template.command_type | default(icinga_command_template_command_type) }}"
timeout: "{{ command_template.timeout | default(omit) }}"
imports: "{{ command_template.imports | default(omit) }}"
disabled: "{{ command_template.disabled | default(omit) }}"
zone: "{{ command_template.zone | default(omit) }}"
vars: "{{ command_template.vars | default(omit) }}"
arguments: "{{ command_template.arguments | default(omit) }}"
retries: 3
delay: 3
register: result
until: result is succeeded
loop: "{{ icinga_command_templates|subelements('command_template_object') }}"
loop: "{{ icinga_command_templates }}"
loop_control:
loop_var: command_template
tags: command_template
Expand Down
18 changes: 8 additions & 10 deletions roles/ansible_icinga/tasks/icinga_endpoint.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
---
# endpoint.1 = endpoint array
# endpoint.0 = icinga_endpoint attribute
- name: icinga_endpoint
- name: Icinga_endpoint
icinga_endpoint:
url: "{{ icinga_url }}"
use_proxy: "{{ icinga_use_proxy | default(omit) }}"
Expand All @@ -11,17 +9,17 @@
force_basic_auth: "{{ icinga_force_basic_auth | default(omit) }}"
client_cert: "{{ icinga_client_cert | default(omit) }}"
client_key: "{{ icinga_client_key | default(omit) }}"
state: "{{ endpoint.0.state | default(omit) }}"
object_name: "{{ endpoint.1 }}"
host: "{{ endpoint.0.host | default(omit) }}"
port: "{{ endpoint.0.port | default(omit) }}"
log_duration: "{{ endpoint.0.log_duration | default(omit) }}"
zone: "{{ endpoint.0.zone | default(omit) }}"
state: "{{ endpoint.state | default(omit) }}"
object_name: "{{ endpoint.name }}"
host: "{{ endpoint.host | default(omit) }}"
port: "{{ endpoint.port | default(omit) }}"
log_duration: "{{ endpoint.log_duration | default(omit) }}"
zone: "{{ endpoint.zone | default(omit) }}"
retries: 3
delay: 3
register: result
until: result is succeeded
loop: "{{ icinga_endpoints|subelements('endpoint_object') }}"
loop: "{{ icinga_endpoints }}"
loop_control:
loop_var: endpoint
tags: endpoint
Expand Down
72 changes: 35 additions & 37 deletions roles/ansible_icinga/tasks/icinga_host.yml
Original file line number Diff line number Diff line change
@@ -1,55 +1,53 @@
---
# host.1 = host array
# host.0 = icinga_host attribute
- name: icinga_host
- name: Icinga_host
icinga_host:
client_cert: "{{ icinga_client_cert | default(omit) }}"
client_key: "{{ icinga_client_key | default(omit) }}"
force_basic_auth: "{{ icinga_force_basic_auth | default(omit) }}"
state: "{{ host.0.state | default(omit) }}"
state: "{{ host.state | default(omit) }}"
url_password: "{{ icinga_pass }}"
url_username: "{{ icinga_user }}"
url: "{{ icinga_url }}"
use_proxy: "{{ icinga_use_proxy | default(omit) }}"
validate_certs: "{{ icinga_validate_certs | default(omit) }}"
accept_config: "{{ host.0.accept_config | default(omit) }}"
address: "{{ host.0.address | default(omit) }}"
address6: "{{ host.0.address6 | default(omit) }}"
check_command: "{{ host.0.check_command | default(omit) }}"
check_interval: "{{ host.0.check_interval | default(omit) }}"
check_period: "{{ host.0.check_period | default(omit) }}"
check_timeout: "{{ host.0.check_timeout | default(omit) }}"
accept_config: "{{ host.accept_config | default(omit) }}"
address: "{{ host.address | default(omit) }}"
address6: "{{ host.address6 | default(omit) }}"
check_command: "{{ host.check_command | default(omit) }}"
check_interval: "{{ host.check_interval | default(omit) }}"
check_period: "{{ host.check_period | default(omit) }}"
check_timeout: "{{ host.check_timeout | default(omit) }}"
command_endpoint: "{{ command_endpoint | default(omit) }}"
disabled: "{{ host.0.disabled | default(omit) }}"
display_name: "{{ host.0.display_name | default(omit) }}"
enable_active_checks: "{{ host.0.enable_active_checks | default(omit) }}"
enable_event_handler: "{{ host.0.enable_event_handler | default(omit) }}"
enable_flapping: "{{ host.0.enable_flapping | default(omit) }}"
enable_notifications: "{{ host.0.enable_notifications | default(omit) }}"
enable_passive_checks: "{{ host.0.enable_passive_checks | default(omit) }}"
enable_perfdata: "{{ host.0.enable_perfdata | default(omit) }}"
event_command: "{{ host.0.event_command | default(omit) }}"
flapping_threshold_high: "{{ host.0.flapping_threshold_high | default(omit) }}"
flapping_threshold_low: "{{ host.0.flapping_threshold_low | default(omit) }}"
groups: "{{ host.0.groups | default(omit) }}"
has_agent: "{{ host.0.has_agent | default(omit) }}"
icon_image_alt: "{{ host.0.icon_image_alt | default(omit) }}"
icon_image: "{{ host.0.icon_image | default(omit) }}"
imports: "{{ host.0.imports | default(icinga_host_imports) }}"
master_should_connect: "{{ host.0.master_should_connect | default(omit) }}"
max_check_attempts: "{{ host.0.max_check_attempts | default(omit) }}"
notes_url: "{{ host.0.notes_url | default(omit) }}"
notes: "{{ host.0.notes | default(omit) }}"
object_name: "{{ host.1 }}"
retry_interval: "{{ host.0.retry_interval | default(omit) }}"
vars: "{{ host.0.vars | default(omit) }}"
volatile: "{{ host.0.volatile | default(omit) }}"
zone: "{{ host.0.zone | default(omit) }}"
disabled: "{{ host.disabled | default(omit) }}"
display_name: "{{ host.display_name | default(omit) }}"
enable_active_checks: "{{ host.enable_active_checks | default(omit) }}"
enable_event_handler: "{{ host.enable_event_handler | default(omit) }}"
enable_flapping: "{{ host.enable_flapping | default(omit) }}"
enable_notifications: "{{ host.enable_notifications | default(omit) }}"
enable_passive_checks: "{{ host.enable_passive_checks | default(omit) }}"
enable_perfdata: "{{ host.enable_perfdata | default(omit) }}"
event_command: "{{ host.event_command | default(omit) }}"
flapping_threshold_high: "{{ host.flapping_threshold_high | default(omit) }}"
flapping_threshold_low: "{{ host.flapping_threshold_low | default(omit) }}"
groups: "{{ host.groups | default(omit) }}"
has_agent: "{{ host.has_agent | default(omit) }}"
icon_image_alt: "{{ host.icon_image_alt | default(omit) }}"
icon_image: "{{ host.icon_image | default(omit) }}"
imports: "{{ host.imports | default(icinga_host_imports) }}"
master_should_connect: "{{ host.master_should_connect | default(omit) }}"
max_check_attempts: "{{ host.max_check_attempts | default(omit) }}"
notes_url: "{{ host.notes_url | default(omit) }}"
notes: "{{ host.notes | default(omit) }}"
object_name: "{{ host.name }}"
retry_interval: "{{ host.retry_interval | default(omit) }}"
vars: "{{ host.vars | default(omit) }}"
volatile: "{{ host.volatile | default(omit) }}"
zone: "{{ host.zone | default(omit) }}"
retries: 3
delay: 3
register: result
until: result is succeeded
loop: "{{ icinga_hosts|subelements('host_object') }}"
loop: "{{ icinga_hosts }}"
loop_control:
loop_var: host
tags: host
Expand Down
72 changes: 35 additions & 37 deletions roles/ansible_icinga/tasks/icinga_host_template.yml
Original file line number Diff line number Diff line change
@@ -1,54 +1,52 @@
---
# host_template.1 = host_template array
# host_template.0 = icinga_host_template attribute
- name: icinga_host_template
- name: Icinga_host_template
icinga_host_template:
client_cert: "{{ icinga_client_cert | default(omit) }}"
client_key: "{{ icinga_client_key | default(omit) }}"
force_basic_auth: "{{ icinga_force_basic_auth | default(omit) }}"
state: "{{ host.0.state | default(omit) }}"
state: "{{ host.state | default(omit) }}"
url_password: "{{ icinga_pass }}"
url_username: "{{ icinga_user }}"
url: "{{ icinga_url }}"
use_proxy: "{{ icinga_use_proxy | default(omit) }}"
validate_certs: "{{ icinga_validate_certs | default(omit) }}"
accept_config: "{{ host_template.0.accept_config | default(omit) }}"
address: "{{ host_template.0.address | default(omit) }}"
address6: "{{ host_template.0.address6 | default(omit) }}"
check_command: "{{ host_template.0.check_command | default(omit) }}"
check_interval: "{{ host_template.0.check_interval | default(omit) }}"
check_period: "{{ host_template.0.check_period | default(omit) }}"
check_timeout: "{{ host_template.0.check_timeout | default(omit) }}"
disabled: "{{ host_template.0.disabled | default(omit) }}"
display_name: "{{ host_template.0.display_name | default(omit) }}"
enable_active_checks: "{{ host_template.0.enable_active_checks | default(omit) }}"
enable_event_handler: "{{ host_template.0.enable_event_handler | default(omit) }}"
enable_flapping: "{{ host_template.0.enable_flapping | default(omit) }}"
enable_notifications: "{{ host_template.0.enable_notifications | default(omit) }}"
enable_passive_checks: "{{ host_template.0.enable_passive_checks | default(omit) }}"
enable_perfdata: "{{ host_template.0.enable_perfdata | default(omit) }}"
event_command: "{{ host_template.0.event_command | default(omit) }}"
flapping_threshold_high: "{{ host_template.0.flapping_threshold_high | default(omit) }}"
flapping_threshold_low: "{{ host_template.0.flapping_threshold_low | default(omit) }}"
groups: "{{ host_template.0.address | default(omit) }}"
has_agent: "{{ host_template.0.has_agent | default(omit) }}"
icon_image_alt: "{{ host_template.0.icon_image_alt | default(omit) }}"
icon_image: "{{ host_template.0.icon_image | default(omit) }}"
imports: "{{ host_template.0.imports | default(omit) }}"
master_should_connect: "{{ host_template.0.master_should_connect | default(omit) }}"
max_check_attempts: "{{ host_template.0.max_check_attempts | default(omit) }}"
notes_url: "{{ host_template.0.notes_url | default(omit) }}"
notes: "{{ host_template.0.notes | default(omit) }}"
object_name: "{{ host_template.1 }}"
retry_interval: "{{ host_template.0.retry_interval | default(omit) }}"
vars: "{{ host_template.0.vars | default(omit) }}"
volatile: "{{ host_template.0.volatile | default(omit) }}"
zone: "{{ host_template.0.zone | default(omit) }}"
accept_config: "{{ host_template.accept_config | default(omit) }}"
address: "{{ host_template.address | default(omit) }}"
address6: "{{ host_template.address6 | default(omit) }}"
check_command: "{{ host_template.check_command | default(omit) }}"
check_interval: "{{ host_template.check_interval | default(omit) }}"
check_period: "{{ host_template.check_period | default(omit) }}"
check_timeout: "{{ host_template.check_timeout | default(omit) }}"
disabled: "{{ host_template.disabled | default(omit) }}"
display_name: "{{ host_template.display_name | default(omit) }}"
enable_active_checks: "{{ host_template.enable_active_checks | default(omit) }}"
enable_event_handler: "{{ host_template.enable_event_handler | default(omit) }}"
enable_flapping: "{{ host_template.enable_flapping | default(omit) }}"
enable_notifications: "{{ host_template.enable_notifications | default(omit) }}"
enable_passive_checks: "{{ host_template.enable_passive_checks | default(omit) }}"
enable_perfdata: "{{ host_template.enable_perfdata | default(omit) }}"
event_command: "{{ host_template.event_command | default(omit) }}"
flapping_threshold_high: "{{ host_template.flapping_threshold_high | default(omit) }}"
flapping_threshold_low: "{{ host_template.flapping_threshold_low | default(omit) }}"
groups: "{{ host_template.address | default(omit) }}"
has_agent: "{{ host_template.has_agent | default(omit) }}"
icon_image_alt: "{{ host_template.icon_image_alt | default(omit) }}"
icon_image: "{{ host_template.icon_image | default(omit) }}"
imports: "{{ host_template.imports | default(omit) }}"
master_should_connect: "{{ host_template.master_should_connect | default(omit) }}"
max_check_attempts: "{{ host_template.max_check_attempts | default(omit) }}"
notes_url: "{{ host_template.notes_url | default(omit) }}"
notes: "{{ host_template.notes | default(omit) }}"
object_name: "{{ host_template.name }}"
retry_interval: "{{ host_template.retry_interval | default(omit) }}"
vars: "{{ host_template.vars | default(omit) }}"
volatile: "{{ host_template.volatile | default(omit) }}"
zone: "{{ host_template.zone | default(omit) }}"
retries: 3
delay: 3
register: result
until: result is succeeded
loop: "{{ icinga_host_templates|subelements('host_template_object') }}"
loop: "{{ icinga_host_templates }}"
loop_control:
loop_var: host_template
tags: host_template
Expand Down
14 changes: 6 additions & 8 deletions roles/ansible_icinga/tasks/icinga_hostgroup.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
---
# hostgroup.1 = hostgroup array
# hostgroup.0 = icinga_hostgroup attribute
- name: icinga_hostgroup
- name: Icinga_hostgroup
icinga_hostgroup:
url: "{{ icinga_url }}"
use_proxy: "{{ icinga_use_proxy | default(omit) }}"
Expand All @@ -11,15 +9,15 @@
force_basic_auth: "{{ icinga_force_basic_auth | default(omit) }}"
client_cert: "{{ icinga_client_cert | default(omit) }}"
client_key: "{{ icinga_client_key | default(omit) }}"
state: "{{ hostgroup.0.state | default(omit) }}"
object_name: "{{ hostgroup.1 }}"
display_name: "{{ hostgroup.0.display_name | default(omit) }}"
assign_filter: "{{ hostgroup.0.assign_filter | default('host.name=\"' + hostgroup.1 + '-*\"') }}"
state: "{{ hostgroup.state | default(omit) }}"
object_name: "{{ hostgroup.name }}"
display_name: "{{ hostgroup.display_name | default(omit) }}"
assign_filter: "{{ hostgroup.assign_filter | default('host.name=\"' + hostgroup.name + '-*\"') }}"
retries: 3
delay: 3
register: result
until: result is succeeded
loop: "{{ icinga_hostgroups|subelements('hostgroup_object') }}"
loop: "{{ icinga_hostgroups }}"
loop_control:
loop_var: hostgroup
tags: hostgroup
Expand Down
Loading

0 comments on commit 8119c52

Please sign in to comment.