From f95a41094a47bfa9f919e792cd7b31c9969404db Mon Sep 17 00:00:00 2001 From: Rottler Tamas Date: Tue, 3 Feb 2026 17:44:02 +0100 Subject: [PATCH] switch to hosts based approach --- handlers/main.yml | 6 +++++- tasks/main.yml | 18 +++++++++++----- templates/host | 52 +++++++++++++++++++++++------------------------ 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/handlers/main.yml b/handlers/main.yml index 712ea1c..39de7c1 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,6 +1,10 @@ --- - name: reload icinga2 become: yes - service: name=icinga2 state=reloaded + systemd_service: + name: icinga2 + state: reloaded + delegate_to: localhost + run_once: yes # vim: set tabstop=2 shiftwidth=2 expandtab smarttab: diff --git a/tasks/main.yml b/tasks/main.yml index 951a751..ba66d87 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -2,18 +2,26 @@ - name: "list previous host configs" shell: ls -1 /etc/icinga2/conf.d/ansible_routeros/ | sed -e 's/\.[^\.]*$//' register: previous_hosts + delegate_to: localhost + run_once: true changed_when: false -- name: "create conf file for each host" - template: src="host" dest="/etc/icinga2/conf.d/ansible_routeros/{{ item }}.conf" - with_items: "{{ hostlist }}" +- name: "create conf file for host" + template: + src: "host" + dest: "/etc/icinga2/conf.d/ansible_routeros/{{ inventory_hostname }}.conf" notify: - reload icinga2 - name: "remove stale hosts" - file: path="/etc/icinga2/conf.d/ansible_routeros/{{ item }}.conf" state=absent + file: + path: "/etc/icinga2/conf.d/ansible_routeros/{{ item }}.conf" + state: absent with_items: "{{ previous_hosts.stdout_lines }}" - when: hostvars[item] is undefined + when: "hostvars[item] is undefined" + delegate_to: localhost + run_once: true notify: - reload icinga2 +# vim: set tabstop=2 shiftwidth=2 expandtab smarttab: diff --git a/templates/host b/templates/host index d4b9d61..436447b 100644 --- a/templates/host +++ b/templates/host @@ -4,9 +4,9 @@ {% if icinga_contact is defined %} {% set c = icinga_contact %} {% endif %} -object Host "{{ hostvars[item].inventory_hostname }}" { +object Host "{{ inventory_hostname }}" { import "generic-host" - address = "{{ hostvars[item].ansible_host }}" + address = "{{ ansible_host }}" vars.os = "routeros" vars.notification["mail"] = { groups = [ {{ c }} ] @@ -17,56 +17,56 @@ object Host "{{ hostvars[item].inventory_hostname }}" { object Service "ssh" { import "generic-service" - host_name = "{{ hostvars[item].inventory_hostname }}" + host_name = "{{ inventory_hostname }}" check_command = "ssh" -{% if hostvars[item].ansible_port is defined %} - vars.ssh_port = "{{ hostvars[item].ansible_port }}" +{% if ansible_port is defined and ansible_port != 22 %} + vars.ssh_port = "{{ ansible_port }}" {% endif %} } -object Service "mikrotik" { +object Service "mikrotik_system" { import "generic-service" - host_name = "{{ hostvars[item].inventory_hostname }}" - check_command = "mikrotik" - vars.snmpuser = "{{ hostvars[item].snmpuser }}" - vars.snmppasswd = "{{ hostvars[item].snmppasswd }}" + host_name = "{{ inventory_hostname }}" + check_command = "mikrotik_system" + vars.apiuser = "{{ apiuser }}" + vars.apipasswd = "{{ apipasswd }}" } -{% if 'check_ltebackup' in hostvars[item].group_names %} +{% if 'check_ltebackup' in group_names %} object Service "ltebackup" { import "generic-service" - host_name = "{{ hostvars[item].inventory_hostname }}" + host_name = "{{ inventory_hostname }}" check_command = "mikrotik_ltebackup" - vars.user = "{{ hostvars[item].apiuser }}" - vars.password = "{{ hostvars[item].apipasswd }}" - vars.ca = "{{ hostvars[item].apica }}" + vars.user = "{{ apiuser }}" + vars.password = "{{ apipasswd }}" + vars.ca = "{{ apica }}" } {% endif %} -{% if hostvars[item].ov_backup_ip is defined %} +{% if ov_backup_ip is defined %} object Service "ping_ov_backup_ip" { import "generic-service" - host_name = "{{ hostvars[item].inventory_hostname }}" + host_name = "{{ inventory_hostname }}" check_command = "ping" - vars.ping_address = "{{ hostvars[item].ov_backup_ip }}" + vars.ping_address = "{{ ov_backup_ip }}" } {% endif %} -{% if hostvars[item].extra_ping is defined %} -object Service "ping_{{ hostvars[item].extra_ping }}" { +{% if extra_ping is defined %} +object Service "ping_{{ extra_ping }}" { import "generic-service" - host_name = "{{ hostvars[item].inventory_hostname }}" + host_name = "{{ inventory_hostname }}" check_command = "ping" - vars.ping_address = "{{ hostvars[item].extra_ping }}" + vars.ping_address = "{{ extra_ping }}" } {% endif %} -{% if hostvars[item].extra_ssh is defined %} -object Service "ssh_{{ hostvars[item].extra_ssh }}" { +{% if extra_ssh is defined %} +object Service "ssh_{{ extra_ssh }}" { import "generic-service" - host_name = "{{ hostvars[item].inventory_hostname }}" + host_name = "{{ inventory_hostname }}" check_command = "ssh" - vars.ssh_address = "{{ hostvars[item].extra_ssh }}" + vars.ssh_address = "{{ extra_ssh }}" } {% endif %}