commit 2e18cda8f8ed0e69b3d96c984c75191c65231796
parent 15d2b87331499459b098058bc9519d84275ae936
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 21 Feb 2021 22:06:57 +0100
parent 15d2b87331499459b098058bc9519d84275ae936
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sun, 21 Feb 2021 22:06:57 +0100
roles/fritzboxExporter: split tasks into multiple files, add support for archlinux
10 files changed, 195 insertions(+), 136 deletions(-)
M
|
149
++++++++-----------------------------------------------------------------------
A
|
57
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/roles/fritzboxExporter/handlers/main.yml b/roles/fritzboxExporter/handlers/main.yml @@ -0,0 +1,17 @@ +--- + +- name: "[OpenRC] Restart service: fritzbox-exporter (to deploy config changes)" + service: + name: fritzbox-exporter + state: restarted + when: + - ansible_service_mgr == "openrc" + listen: "Restart fritzbox-exporter" + +- name: "[systemd] Restart service: fritzbox-exporter (to deploy config changes)" + systemd: + name: fritzbox-exporter + state: restarted + when: + - ansible_service_mgr == "systemd" + listen: "Restart fritzbox-exporter"
diff --git a/roles/fritzboxExporter/meta/main.yml b/roles/fritzboxExporter/meta/main.yml @@ -0,0 +1,5 @@ +--- + +dependencies: + - nginx-handler + - nftables-handler+ \ No newline at end of file
diff --git a/roles/fritzboxExporter/tasks/checks.yml b/roles/fritzboxExporter/tasks/checks.yml @@ -0,0 +1,40 @@ +--- + +- fail: msg="This Role only works when Option 'system.enableOwnRepos' is true!" + when: + - services.fritzboxExporter.enable is defined + - services.fritzboxExporter.enable is true + - system.enableOwnRepos is false + +- fail: msg="Nginx role has to be enabled when using nginx options!" + when: + - services.fritzboxExporter.nginx.enable is true + - services.nginx.enable is false + +- fail: msg="Option 'services.fritzboxExporter.nginx.domain' has to be set when using nginx!" + when: + - services.fritzboxExporter.nginx.enable is defined + - services.fritzboxExporter.nginx.enable is true + - services.fritzboxExporter.nginx.domain is not defined + +- fail: msg="Option 'services.fritzboxExporter.nginx.sslOnly' has to be set when using nginx!" + when: + - services.fritzboxExporter.nginx.enable is defined + - services.fritzboxExporter.nginx.enable is true + - services.fritzboxExporter.nginx.sslOnly is not defined + +- fail: msg="Option 'services.fritzboxExporter.nginx.ssl.cert' has to be set when using nginx with ssl!" + when: + - services.fritzboxExporter.nginx.enable is defined + - services.fritzboxExporter.nginx.enable is true + - services.fritzboxExporter.nginx.ssl.enable is defined + - services.fritzboxExporter.nginx.ssl.enable is true + - services.fritzboxExporter.nginx.ssl.cert is not defined + +- fail: msg="Option 'services.fritzboxExporter.nginx.ssl.privkey' has to be set when using nginx with ssl!" + when: + - services.fritzboxExporter.nginx.enable is defined + - services.fritzboxExporter.nginx.enable is true + - services.fritzboxExporter.nginx.ssl.enable is defined + - services.fritzboxExporter.nginx.ssl.enable is true + - services.fritzboxExporter.nginx.ssl.privkey is not defined
diff --git a/roles/fritzboxExporter/tasks/firewall.yml b/roles/fritzboxExporter/tasks/firewall.yml @@ -0,0 +1,7 @@ +--- + +- name: "[nftables] Create rule for: fritzbox-exporter" + copy: + src: nftables-rule.nft + dest: /etc/nftables.d/fritzbox-exporter.nft + notify: Restart nftables+ \ No newline at end of file
diff --git a/roles/fritzboxExporter/tasks/install.yml b/roles/fritzboxExporter/tasks/install.yml @@ -0,0 +1,19 @@ +--- + +- name: "[Alpine] Install package: fritzbox-exporter" + apk: + name: fritzbox-exporter + state: present + update_cache: yes + when: + - ansible_distribution == "Alpine" + notify: Restart fritzbox-exporter + +- name: "[Archlinux] Install package: fritzbox-exporter" + pacman: + name: fritzbox-exporter + state: present + update_cache: yes + when: + - ansible_distribution == "Archlinux" + notify: Restart fritzbox-exporter+ \ No newline at end of file
diff --git a/roles/fritzboxExporter/tasks/main.yaml b/roles/fritzboxExporter/tasks/main.yaml @@ -1,155 +1,34 @@ --- -# check - -- fail: msg="This Role only works on Alpine!" - when: - - services.fritzboxExporter.enable is defined and services.fritzboxExporter.enable is true - - ansible_distribution != "Alpine" - -- fail: msg="This Role only works when Option 'system.enableOwnRepos' is true!" +- include: checks.yml when: - - services.fritzboxExporter.enable is defined and services.fritzboxExporter.enable is true - - system.enableOwnRepos is false - - -# install - -- name: "[Alpine] Install package: fritzbox-exporter" - apk: - name: fritzbox-exporter - state: present - update_cache: yes - when: - - ansible_distribution == "Alpine" - - services.fritzboxExporter.enable is true - - -# configure -- name: "[nginx] Create vhost" - template: - src: nginx-vhost.conf.j2 - dest: /etc/nginx/conf.d/fritzbox-exporter.conf - mode: 0644 - owner: nginx - group: nginx - when: - - services.fritzboxExporter.enable is true - -- name: "[OpenRC] Restart service: nginx" - service: - name: nginx - state: restarted - when: + - services.fritzboxExporter.enable is defined - services.fritzboxExporter.enable is true - - ansible_service_mgr == "openrc" -- name: "[systemd] Restart service: nginx" - systemd: - name: nginx - state: restarted - when: - - services.fritzboxExporter.enable is true - - ansible_service_mgr == "systemd" - - - -# firewall - -- name: "[nftables] Create rule for: fritzbox-exporter" - copy: - src: nftables-rule.nft - dest: /etc/nftables.d/fritzbox-exporter.nft +- include: install.yml when: - - network.nftables.enable is true + - services.fritzboxExporter.enable is defined - services.fritzboxExporter.enable is true -- name: "[OpenRC] Restart service: nftables" - service: - name: nftables - state: restarted +- include: nginx.yml when: - - ansible_service_mgr == "openrc" - - network.nftables.enable is true + - services.fritzboxExporter.enable is defined - services.fritzboxExporter.enable is true -- name: "[systemd] Restart service: nftables" - systemd: - name: nftables - state: restarted +- include: firewall.yml when: - - ansible_service_mgr == "systemd" - network.nftables.enable is true - services.fritzboxExporter.enable is true -# start and enable - -- name: "[OpenRC] Enable and restart service: fritzbox-exporter" - service: - name: fritzbox-exporter - enabled: yes - state: started - when: - - ansible_service_mgr == "openrc" - - services.fritzboxExporter.enable is true - - -# stop - -- name: "[OpenRC] Disable and stop service: fritzbox-exporter" - service: - name: fritzbox-exporter - enabled: no - state: stopped - when: - - ansible_service_mgr == "openrc" - - services.fritzboxExporter.enable is false - - -#defirewall - -- name: "[nftables] Delete rule for: fritzbox-exporter" - file: - path: /etc/nftables.d/fritzbox-exporter.nft - state: absent +- include: start.yml when: - - network.nftables.enable is true - - services.fritzboxExporter.enable is false + - services.fritzboxExporter.enable is defined + - services.fritzboxExporter.enable is true -- name: "[OpenRC] Restart service: nftables" - service: - name: nftables - state: restarted - when: - - ansible_service_mgr == "openrc" - - network.nftables.enable is true - - services.fritzboxExporter.enable is false +- name: Run handlers + meta: flush_handlers -- name: "[systemd] Restart service: nftables" - systemd: - name: nftables - state: restarted +- include: remove.yml when: - - ansible_service_mgr == "systemd" - - network.nftables.enable is true - - services.fritzboxExporter.enable is false - -# remove - -- name: "[Alpine] Remove package: fritzbox-exporter" - apk: - name: fritzbox-exporter - state: absent - when: - - ansible_distribution == "Alpine" - - services.fritzboxExporter.enable is false - - -# remove leftover files - -- name: "Remove directory: /etc/nginx/conf.d/fritzbox-exporter.conf" - file: - path: /etc/nginx/conf.d/fritzbox-exporter.conf - state: absent - when: + - services.fritzboxExporter.enable is defined - services.fritzboxExporter.enable is false
diff --git a/roles/fritzboxExporter/tasks/nginx.yml b/roles/fritzboxExporter/tasks/nginx.yml @@ -0,0 +1,12 @@ +--- + +- name: "[nginx] Create vhost" + template: + src: nginx-vhost.conf.j2 + dest: /etc/nginx/conf.d/fritzbox-exporter.conf + mode: 0644 + owner: nginx + group: nginx + when: + - services.fritzboxExporter.enable is true + notify: Restart nginx+ \ No newline at end of file
diff --git a/roles/fritzboxExporter/tasks/remove.yml b/roles/fritzboxExporter/tasks/remove.yml @@ -0,0 +1,57 @@ +--- + +- name: "[OpenRC] Disable and stop service: fritzbox-exporter" + service: + name: fritzbox-exporter + enabled: no + state: stopped + when: + - ansible_service_mgr == "openrc" + +- name: "[systemd] Disable and stop service: fritzbox-exporter" + systemd: + name: fritzbox-exporter + enabled: no + state: stopped + when: + - ansible_service_mgr == "systemd" + + +- name: "[Alpine] Remove package: fritzbox-exporter" + apk: + name: fritzbox-exporter + state: absent + when: + - ansible_distribution == "Alpine" + +- name: "[Archlinux] Remove package: fritzbox-exporter" + pacman: + name: fritzbox-exporter + state: absent + when: + - ansible_distribution == "Archlinux" + + +- name: "Delete leftovers" + file: + path: "{{item}}" + state: absent + with_items: + - /etc/nginx/conf.d/fritzbox-exporter.conf + - /etc/nftables.d/fritzbox-exporter.nft + +- name: "[OpenRC] Restart service: nftables" + service: + name: nftables + state: restarted + when: + - ansible_service_mgr == "openrc" + - network.nftables.enable is true + +- name: "[systemd] Restart service: nftables" + systemd: + name: nftables + state: restarted + when: + - ansible_service_mgr == "systemd" + - network.nftables.enable is true
diff --git a/roles/fritzboxExporter/tasks/start.yml b/roles/fritzboxExporter/tasks/start.yml @@ -0,0 +1,17 @@ +--- + +- name: "[OpenRC] Enable and start service: fritzbox-exporter" + service: + name: fritzbox-exporter + enabled: yes + state: started + when: + - ansible_service_mgr == "openrc" + +- name: "[systemd] Enable and start service: fritzbox-exporter" + systemd: + name: fritzbox-exporter + enabled: yes + state: started + when: + - ansible_service_mgr == "systemd"
diff --git a/roles/fritzboxExporter/templates/nginx-vhost.conf.j2 b/roles/fritzboxExporter/templates/nginx-vhost.conf.j2 @@ -32,4 +32,5 @@ server { include /etc/nginx/proxy.conf; } } -{% endif %} + +{% endif %}+ \ No newline at end of file