From 26f41a3bf88cd490434055590e2fffa30bb6a1a9 Mon Sep 17 00:00:00 2001 From: Ruben De Smet Date: Thu, 23 Mar 2017 19:56:51 +0100 Subject: [PATCH] Implement automatic zones --- icinga2-ansible-add-zones/defaults/main.yml | 6 + icinga2-ansible-add-zones/handlers/main.yml | 12 ++ icinga2-ansible-add-zones/meta/main.yml | 128 ++++++++++++++++++ .../tasks/icinga2_add_zones.yml | 29 ++++ icinga2-ansible-add-zones/tasks/main.yml | 11 ++ .../templates/endpoint_zones.j2 | 14 ++ icinga2-ansible-add-zones/templates/zone.j2 | 12 ++ .../templates/zones.conf | 1 + 8 files changed, 213 insertions(+) create mode 100644 icinga2-ansible-add-zones/defaults/main.yml create mode 100644 icinga2-ansible-add-zones/handlers/main.yml create mode 100644 icinga2-ansible-add-zones/meta/main.yml create mode 100644 icinga2-ansible-add-zones/tasks/icinga2_add_zones.yml create mode 100644 icinga2-ansible-add-zones/tasks/main.yml create mode 100644 icinga2-ansible-add-zones/templates/endpoint_zones.j2 create mode 100644 icinga2-ansible-add-zones/templates/zone.j2 create mode 100644 icinga2-ansible-add-zones/templates/zones.conf diff --git a/icinga2-ansible-add-zones/defaults/main.yml b/icinga2-ansible-add-zones/defaults/main.yml new file mode 100644 index 00000000..0443be13 --- /dev/null +++ b/icinga2-ansible-add-zones/defaults/main.yml @@ -0,0 +1,6 @@ +--- +# defaults file for icinga2-ansible-add-zones + +icinga2_zones_dir: "/etc/icinga2/zones.d/" +icinga_master_zone: "master" +icinga_merged_zones: '{{icinga_zones|combine({ icinga_master_zone : {} })}}' diff --git a/icinga2-ansible-add-zones/handlers/main.yml b/icinga2-ansible-add-zones/handlers/main.yml new file mode 100644 index 00000000..866546ae --- /dev/null +++ b/icinga2-ansible-add-zones/handlers/main.yml @@ -0,0 +1,12 @@ +--- +# handlers file for icinga2-ansible-add-zones + +- name: restart icinga2 + service: name=icinga2 + state=restarted + enabled=yes + +- name: reload icinga2 + service: name=icinga2 + state=reloaded + enabled=yes diff --git a/icinga2-ansible-add-zones/meta/main.yml b/icinga2-ansible-add-zones/meta/main.yml new file mode 100644 index 00000000..aee201dc --- /dev/null +++ b/icinga2-ansible-add-zones/meta/main.yml @@ -0,0 +1,128 @@ +--- +galaxy_info: + author: your name + description: + company: your company (optional) + # Some suggested licenses: + # - BSD (default) + # - MIT + # - GPLv2 + # - GPLv3 + # - Apache + # - CC-BY + license: license (GPLv2, CC-BY, etc) + min_ansible_version: 1.2 + # + # Below are all platforms currently available. Just uncomment + # the ones that apply to your role. If you don't see your + # platform on this list, let us know and we'll get it added! + # + platforms: + - name: EL + versions: + # - all + # - 5 + - 6 + - 7 + #- name: GenericUNIX + # versions: + # - all + # - any + - name: Fedora + versions: + # - all + # - 16 + # - 17 + # - 18 + # - 19 + - 20 + - 21 + - 22 + - 23 + - 24 + #- name: opensuse + # versions: + # - all + # - 12.1 + # - 12.2 + # - 12.3 + # - 13.1 + # - 13.2 + #- name: Amazon + # versions: + # - all + # - 2013.03 + # - 2013.09 + #- name: GenericBSD + # versions: + # - all + # - any + #- name: FreeBSD + # versions: + # - all + # - 8.0 + # - 8.1 + # - 8.2 + # - 8.3 + # - 8.4 + # - 9.0 + # - 9.1 + # - 9.1 + # - 9.2 + #- name: Ubuntu + # versions: + # - all + # - lucid + # - maverick + # - natty + # - oneiric + # - precise + # - quantal + # - raring + # - saucy + # - trusty + #- name: SLES + # versions: + # - all + # - 10SP3 + # - 10SP4 + # - 11 + # - 11SP1 + # - 11SP2 + # - 11SP3 + #- name: GenericLinux + # versions: + # - all + # - any + #- name: Debian + # versions: + # - all + # - etch + # - lenny + # - squeeze + # - wheezy + # + # Below are all categories currently available. Just as with + # the platforms above, uncomment those that apply to your role. + # + #categories: + #- cloud + #- cloud:ec2 + #- cloud:gce + #- cloud:rax + #- clustering + #- database + #- database:nosql + #- database:sql + #- development + #- monitoring + #- networking + #- packaging + #- system + #- web +dependencies: [] + # List your role dependencies here, one per line. Only + # dependencies available via galaxy should be listed here. + # Be sure to remove the '[]' above if you add dependencies + # to this list. + diff --git a/icinga2-ansible-add-zones/tasks/icinga2_add_zones.yml b/icinga2-ansible-add-zones/tasks/icinga2_add_zones.yml new file mode 100644 index 00000000..f79acaa2 --- /dev/null +++ b/icinga2-ansible-add-zones/tasks/icinga2_add_zones.yml @@ -0,0 +1,29 @@ +--- +- name: Copy Endpoint Zone Definitions + template: src=endpoint_zones.j2 + dest={{ icinga2_zones_dir }}/{{ hostvars[item]['ansible_fqdn'] }}.conf + owner=icinga + group=icinga + mode=0640 + with_items: "{{ groups['all'] }}" + notify: + - reload icinga2 + +- name: Copy Zone Definitions + template: + src: zone.j2 + dest: "{{ icinga2_zones_dir }}/{{ item }}.conf" + owner: icinga + group: icinga + mode: 0640 + with_items: "{{ icinga_merged_zones }}" + notify: + - reload icinga2 + +- name: Copy zones.conf + template: + src: zones.conf + dest: /etc/icinga2/zones.conf + owner: icinga + group: icinga + mode: 0640 diff --git a/icinga2-ansible-add-zones/tasks/main.yml b/icinga2-ansible-add-zones/tasks/main.yml new file mode 100644 index 00000000..c60a86a9 --- /dev/null +++ b/icinga2-ansible-add-zones/tasks/main.yml @@ -0,0 +1,11 @@ +--- +# tasks file for icinga2-ansible-add-zones + +- name: Ensure existance of {{ icinga2_zones_dir }} + file: + name: "{{ icinga2_zones_dir }}" + state: directory + owner: icinga + group: icinga + +- include: icinga2_add_zones.yml diff --git a/icinga2-ansible-add-zones/templates/endpoint_zones.j2 b/icinga2-ansible-add-zones/templates/endpoint_zones.j2 new file mode 100644 index 00000000..11f4a08e --- /dev/null +++ b/icinga2-ansible-add-zones/templates/endpoint_zones.j2 @@ -0,0 +1,14 @@ +object Endpoint "{{ hostvars[item]['ansible_fqdn'] }}" { + host = "{{ hostvars[item]['ansible_fqdn'] }}" +} + +{% if 'icinga_zone' in hostvars[item] %} +object Zone "{{ hostvars[item]['ansible_fqdn'] }}" { + endpoints = [ "{{ hostvars[item]['ansible_fqdn'] }}" ] + {% if 'icinga_zone' in hostvars[item] %} + parent = "{{ hostvars[item]['icinga_zone'] }}" + {% else %} + parent = "{{ icinga_master_zone }}" + {% endif %} +} +{% endif %} diff --git a/icinga2-ansible-add-zones/templates/zone.j2 b/icinga2-ansible-add-zones/templates/zone.j2 new file mode 100644 index 00000000..ebf5aaa0 --- /dev/null +++ b/icinga2-ansible-add-zones/templates/zone.j2 @@ -0,0 +1,12 @@ +object Zone "{{ item }}" { + {% if 'parent' in icinga_merged_zones[item] %} + parent = "{{ icinga_merged_zones[item].parent }}" + {% endif %} + + endpoints = [ + {% for host, params in hostvars.iteritems() %} + {% if item == icinga_master_zone and 'icinga_zone' not in params %} + "{{ host }}", + {% endif %}{% endfor %} + ] +} diff --git a/icinga2-ansible-add-zones/templates/zones.conf b/icinga2-ansible-add-zones/templates/zones.conf new file mode 100644 index 00000000..e11f55b0 --- /dev/null +++ b/icinga2-ansible-add-zones/templates/zones.conf @@ -0,0 +1 @@ +include_recursive "zones.d"