switch to hosts based approach

This commit is contained in:
ROTTLER Tamas 2026-02-03 17:44:02 +01:00
parent 133174dc41
commit f95a41094a
3 changed files with 44 additions and 32 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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 %}