ctucx.git: ansible-configs

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

commit 029bd15c5258162923f9ed7e9ed21fc8439c9586
parent 634121093cbc0bcc86d62d294943effa8e2b8b7f
Author: Leah (ctucx) <leah@ctu.cx>
Date: Tue, 23 Feb 2021 15:41:04 +0100

roles/zigbee2mqtt: create role
8 files changed, 128 insertions(+), 0 deletions(-)
diff --git a/roles/serial2tcp/handlers/main.yml b/roles/serial2tcp/handlers/main.yml
@@ -0,0 +1,10 @@
+---
+
+- name: "[systemd] Restart service: serial2tcp (to deploy config changes)"
+  systemd:
+    daemon_reload: yes
+    name: serial2tcp
+    state: restarted
+  when:
+    - ansible_service_mgr == "systemd"
+  listen: "Restart serial2tcp"
diff --git a/roles/serial2tcp/tasks/checks.yml b/roles/serial2tcp/tasks/checks.yml
@@ -0,0 +1,14 @@
+---
+
+- 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="Option 'services.serial2tcp.device' has to be set!"
+  when:
+    - services.serial2tcp.device is not defined
diff --git a/roles/serial2tcp/tasks/configure.yml b/roles/serial2tcp/tasks/configure.yml
@@ -0,0 +1,12 @@
+---
+
+- name: "Create systemd-service file for serial2tcp"
+  template:
+    src: serial2tcp.service.j2
+    dest: /etc/systemd/system/serial2tcp.service
+    mode: "0755"
+    owner: root
+    group: root
+  notify: "Restart serial2tcp"
+  when: 
+    - ansible_service_mgr == "systemd"
diff --git a/roles/serial2tcp/tasks/install.yml b/roles/serial2tcp/tasks/install.yml
@@ -0,0 +1,8 @@
+---
+
+- name: "[Archlinux] Install package: serial2tcp"
+  pacman:
+    name: serial2tcp
+    state: present
+  when: 
+    - ansible_distribution == "Archlinux" 
diff --git a/roles/serial2tcp/tasks/main.yml b/roles/serial2tcp/tasks/main.yml
@@ -0,0 +1,28 @@
+---
+
+- include: checks.yml
+  when:
+    - services.serial2tcp.enable is defined
+    - services.serial2tcp.enable is true
+
+- include: install.yml
+  when:
+    - services.serial2tcp.enable is defined
+    - services.serial2tcp.enable is true
+
+- include: configure.yml
+  when:
+    - services.serial2tcp.enable is defined
+    - services.serial2tcp.enable is true
+
+- include: start.yml
+  when:
+    - services.serial2tcp.enable is defined
+    - services.serial2tcp.enable is true
+
+- include: remove.yml
+  when:
+    - services.serial2tcp.enable is defined
+    - services.serial2tcp.enable is false
+
+- meta: flush_handlers
diff --git a/roles/serial2tcp/tasks/remove.yml b/roles/serial2tcp/tasks/remove.yml
@@ -0,0 +1,24 @@
+---
+
+- name: "[systemd] Disable and stop service: serial2tcp"
+  systemd:
+    name: "serial2tcp"
+    enabled: no
+    state: stopped
+  when: 
+    - ansible_service_mgr == "systemd"
+
+
+- name: "[Archlinux] Remove package: serial2tcp"
+  pacman:
+    name: serial2tcp
+    state: absent
+  when: 
+    - ansible_distribution == "Archlinux"
+
+- name: "Delete leftovers"
+  file:
+    path: "{{item}}"
+    state: absent
+  with_items:
+    - /etc/systemd/system/serial2tcp.service
diff --git a/roles/serial2tcp/tasks/start.yml b/roles/serial2tcp/tasks/start.yml
@@ -0,0 +1,10 @@
+---
+
+- name: "[systemd] Enable and start service: serial2tcp"
+  systemd:
+    daemon_reload: yes
+    name: "serial2tcp"
+    enabled: yes
+    state: started
+  when: 
+    - ansible_service_mgr == "systemd"
diff --git a/roles/serial2tcp/templates/serial2tcp.service.j2 b/roles/serial2tcp/templates/serial2tcp.service.j2
@@ -0,0 +1,22 @@
+#
+# !!! This file is managed by Ansible !!!
+#
+
+[Unit]
+Description=Serial to TCP gateway.
+Requires=network.target
+After=network-online.target
+Wants=network-online.target
+
+[Service]
+Environment=SERIAL_DEVICE=/dev/{{ services.serial2tcp.device }}
+Environment=PORT={{ services.serial2tcp.port | default(2342) }}
+ExecStartPre=stty -F /dev/{{ services.serial2tcp.device }} raw -echo -echoe -echok speed {{ services.serial2tcp.baudrate | default(9600) }}
+ExecStart=/usr/bin/serial2tcp
+Restart=on-failure
+RestartSec=1
+StandardOutput=journal
+StandardError=journa
+
+[Install]
+WantedBy=multi-user.target