ctucx.git: ansible-configs

My personal ansible roles and playbooks [deprecated in favor of nixos]

commit 634121093cbc0bcc86d62d294943effa8e2b8b7f
parent 7249950ef53b25c5d7448363980ebac1143d4b58
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 23 Feb 2021 15:40:40 +0100

roles/zigbee2mqtt: create role
9 files changed, 140 insertions(+), 0 deletions(-)
diff --git a/roles/zigbee2mqtt/handlers/main.yml b/roles/zigbee2mqtt/handlers/main.yml
@@ -0,0 +1,18 @@
+---
+
+- name: "[OpenRC] Restart service: zigbee2mqtt (to deploy config changes)"
+  service:
+    name: zigbee2mqtt
+    state: restarted
+  when:
+    - ansible_service_mgr == "openrc"
+  listen: "Restart zigbee2mqtt"
+
+- name: "[systemd] Restart service: zigbee2mqtt (to deploy config changes)"
+  systemd:
+    daemon_reload: yes
+    name: zigbee2mqtt
+    state: restarted
+  when:
+    - ansible_service_mgr == "systemd"
+  listen: "Restart zigbee2mqtt"
diff --git a/roles/zigbee2mqtt/meta/main.yml b/roles/zigbee2mqtt/meta/main.yml
@@ -0,0 +1,4 @@
+---
+
+dependencies:
+  - role: mosquitto
diff --git a/roles/zigbee2mqtt/tasks/checks.yml b/roles/zigbee2mqtt/tasks/checks.yml
@@ -0,0 +1,18 @@
+---
+
+- fail: msg="This role currently only supports ArchLinux!"
+  when:
+    - ansible_distribution != "Archlinux" 
+
+- fail: msg="This Role only works when Option 'system.enableOwnRepos' is true!"
+  when:
+    - system.enableOwnRepos is false
+
+
+- fail: msg="This Role depends on mosquitto!"
+  when:
+    - services.mosquitto.enable is false
+
+- fail: msg="Option 'services.zigbee2mqtt.config' has to be set!"
+  when:
+    - services.zigbee2mqtt.config is not defined
diff --git a/roles/zigbee2mqtt/tasks/configure.yml b/roles/zigbee2mqtt/tasks/configure.yml
@@ -0,0 +1,10 @@
+---
+
+- name: "Create file: /etc/zigbee2mqtt/configuration.yaml"
+  template:
+    src: zigbee2mqtt.yml.j2
+    dest: /etc/zigbee2mqtt/configuration.yaml
+    mode: "0750"
+    owner: zigbee2mqtt
+    group: zigbee2mqtt
+  notify: "Restart zigbee2mqtt"
diff --git a/roles/zigbee2mqtt/tasks/install.yml b/roles/zigbee2mqtt/tasks/install.yml
@@ -0,0 +1,15 @@
+---
+
+- name: "[Alpine] Install package: zigbee2mqtt"
+  apk:
+    name: zigbee2mqtt
+    state: present
+  when: 
+    - ansible_distribution == "Alpine" 
+
+- name: "[Archlinux] Install package: zigbee2mqtt"
+  pacman:
+    name: zigbee2mqtt
+    state: present
+  when: 
+    - ansible_distribution == "Archlinux" 
diff --git a/roles/zigbee2mqtt/tasks/main.yml b/roles/zigbee2mqtt/tasks/main.yml
@@ -0,0 +1,28 @@
+---
+
+- include: checks.yml
+  when:
+    - services.zigbee2mqtt.enable is defined
+    - services.zigbee2mqtt.enable is true
+
+- include: install.yml
+  when:
+    - services.zigbee2mqtt.enable is defined
+    - services.zigbee2mqtt.enable is true
+
+- include: configure.yml
+  when:
+    - services.zigbee2mqtt.enable is defined
+    - services.zigbee2mqtt.enable is true
+
+- include: start.yml
+  when:
+    - services.zigbee2mqtt.enable is defined
+    - services.zigbee2mqtt.enable is true
+
+- include: remove.yml
+  when:
+    - services.zigbee2mqtt.enable is defined
+    - services.zigbee2mqtt.enable is false
+
+- meta: flush_handlers
diff --git a/roles/zigbee2mqtt/tasks/remove.yml b/roles/zigbee2mqtt/tasks/remove.yml
@@ -0,0 +1,24 @@
+---
+
+- name: "[systemd] Disable and stop service: zigbee2mqtt"
+  systemd:
+    name: "zigbee2mqtt"
+    enabled: no
+    state: stopped
+  when: 
+    - ansible_service_mgr == "systemd"
+
+
+- name: "[Archlinux] Remove package: zigbee2mqtt"
+  pacman:
+    name: zigbee2mqtt
+    state: absent
+  when: 
+    - ansible_distribution == "Archlinux"
+
+- name: "Delete leftovers"
+  file:
+    path: "{{item}}"
+    state: absent
+  with_items:
+    - /etc/zigbee2mqtt
diff --git a/roles/zigbee2mqtt/tasks/start.yml b/roles/zigbee2mqtt/tasks/start.yml
@@ -0,0 +1,17 @@
+---
+
+- name: "[OpenRC] Start and enable service: zigbee2mqtt"
+  service:
+    name: zigbee2mqtt
+    enabled: yes
+    state: started
+  when: 
+    - ansible_service_mgr == "openrc"
+
+- name: "[systemd] Start and enable service: zigbee2mqtt"
+  systemd:
+    name: zigbee2mqtt
+    enabled: yes
+    state: started
+  when: 
+    - ansible_service_mgr == "systemd"+
\ No newline at end of file
diff --git a/roles/zigbee2mqtt/templates/zigbee2mqtt.yml.j2 b/roles/zigbee2mqtt/templates/zigbee2mqtt.yml.j2
@@ -0,0 +1,4 @@
+#
+# !!! This file is managed by Ansible !!!
+#
+{{ services.zigbee2mqtt.config | to_nice_yaml }}+
\ No newline at end of file