ctucx.git: ansible-configs

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

commit 52be2228f0de6ab2352789e4474029b7225d80fa
parent 8f3b8f7355c15a442d05fa54fe065db82ccf3d84
Author: Leah (ctucx) <leah@ctu.cx>
Date: Mon, 22 Feb 2021 16:59:11 +0100

roles/mbusd: split tasks into multiple files
10 files changed, 134 insertions(+), 122 deletions(-)
D
roles/mbusd/files/mbusd@.service
|
20
--------------------
A
roles/mbusd/handlers/main.yml
|
18
++++++++++++++++++
A
roles/mbusd/tasks/checks.yml
|
15
+++++++++++++++
A
roles/mbusd/tasks/configure.yml
|
20
++++++++++++++++++++
A
roles/mbusd/tasks/install.yml
|
8
++++++++
M
roles/mbusd/tasks/main.yml
|
111
+++++++++----------------------------------------------------------------------
A
roles/mbusd/tasks/remove.yml
|
28
++++++++++++++++++++++++++++
A
roles/mbusd/tasks/start.yml
|
10
++++++++++
M
roles/mbusd/templates/mbusd.conf.j2
|
6
+++---
A
roles/mbusd/templates/mbusd.service.j2
|
20
++++++++++++++++++++
diff --git a/roles/mbusd/files/mbusd@.service b/roles/mbusd/files/mbusd@.service
@@ -1,19 +0,0 @@
-#
-# !!! This file is managed by Ansible !!!
-#
-
-[Unit]
-Description=Modbus TCP to Modbus RTU (RS-232/485) gateway.
-Requires=network.target
-After=network-online.target
-Wants=network-online.target
-
-[Service]
-ExecStart=/usr/bin/mbusd -d -v2 -L - -c /etc/mbusd/mbusd-%i.conf -p /dev/%i
-Restart=on-failure
-RestartSec=1
-StandardOutput=journal
-StandardError=journal
-
-[Install]
-WantedBy=multi-user.target-
\ No newline at end of file
diff --git a/roles/mbusd/handlers/main.yml b/roles/mbusd/handlers/main.yml
@@ -0,0 +1,18 @@
+---
+
+- name: "[OpenRC] Restart service: mbusd (to deploy config changes)"
+  service:
+    name: mbusd
+    state: restarted
+  when:
+    - ansible_service_mgr == "openrc"
+  listen: "Restart mbusd"
+
+- name: "[systemd] Restart service: mbusd (to deploy config changes)"
+  systemd:
+    daemon_reload: yes
+    name: mbusd
+    state: restarted
+  when:
+    - ansible_service_mgr == "systemd"
+  listen: "Restart mbusd"
diff --git a/roles/mbusd/tasks/checks.yml b/roles/mbusd/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.mbusd.device' has to be set!"
+  when:
+    - services.mbusd.device is not defined
+ +
\ No newline at end of file
diff --git a/roles/mbusd/tasks/configure.yml b/roles/mbusd/tasks/configure.yml
@@ -0,0 +1,20 @@
+---
+
+- name: "Create systemd-service file for mbusd"
+  template:
+    src: mbusd.service.j2
+    dest: /etc/systemd/system/mbusd.service
+    mode: "0755"
+    owner: root
+    group: root
+  notify: "Restart hostapd"
+  when: 
+    - ansible_service_mgr == "systemd"
+
+- name: "Generate file: /etc/mbusd.conf"
+  template:
+    src: mbusd.conf.j2
+    dest: "/etc/mbusd.conf"
+    owner:  root
+    group: root
+  notify: "Restart mbusd"
diff --git a/roles/mbusd/tasks/install.yml b/roles/mbusd/tasks/install.yml
@@ -0,0 +1,8 @@
+---
+
+- name: "[Archlinux] Install package: mbusd"
+  pacman:
+    name: mbusd
+    state: present
+  when: 
+    - ansible_distribution == "Archlinux" 
diff --git a/roles/mbusd/tasks/main.yml b/roles/mbusd/tasks/main.yml
@@ -1,117 +1,30 @@
 ---
 
-# check
-
-- fail: msg="This role currently only supports ArchLinux!"
-  when:
-    - ansible_distribution != "Archlinux" 
-    - services.mbusd.enable is true
-
-- fail: msg="This Role only works when Option 'system.enableOwnRepos' is true!"
+- include: checks.yml
   when:
-    - system.enableOwnRepos is false
+    - services.mbusd.enable is defined
     - services.mbusd.enable is true
 
-- fail: msg="Option 'services.mbusd.device' has to be set!"
+- include: install.yml
   when:
-    - services.mbusd.device is not defined
+    - services.mbusd.enable is defined
     - services.mbusd.enable is true
 
-- fail: msg="Option 'services.mbusd.mode' has to be set!"
+- include: configure.yml
   when:
-    - services.mbusd.mode is not defined
+    - services.mbusd.enable is defined
     - services.mbusd.enable is true
 
-- fail: msg="Option 'services.mbusd.port' has to be set!"
+- include: start.yml
   when:
-    - services.mbusd.port is not defined
-    - services.mbusd.enable is true
-
-# install
-
-- name: "[Archlinux] Install package: mbusd"
-  pacman:
-    name: mbusd
-    state: present
-    update_cache: yes
-  when: 
-    - ansible_distribution == "Archlinux" 
+    - services.mbusd.enable is defined
     - services.mbusd.enable is true
 
-- name: "Create systemd-service file for mbusd"
-  copy:
-    src: mbusd@.service
-    dest: /etc/systemd/system/mbusd@.service
-    mode: "0755"
-    owner: root
-    group: root
+- name: Run handlers
+  meta: flush_handlers
 
-# configure
-
-- name: clean mbusd-config directory
-  file:
-    state: "{{ item }}"
-    path: /etc/mbusd
-    owner: root
-    group: root
-    mode: 0755
-  with_items:
-    - absent
-    - directory
-  when: 
-    - services.mbusd.enable is true
-
-- name: "[Alpine] Generate mbusd-{{ services.mbusd.device }}.conf"
-  template:
-    src: mbusd.conf.j2
-    dest: "/etc/mbusd/mbusd-{{ services.mbusd.device }}.conf"
-    owner:  root
-    group: root
+- include: remove.yml
   when:
-    - services.mbusd.enable is true
-
-
-# (re)start
-
-- name: "[systemd] Enable and start service: named"
-  systemd:
-    name: "mbusd@{{ services.mbusd.device }}"
-    enabled: yes
-    state: started
-  when: 
-    - ansible_service_mgr == "systemd"
-    - services.mbusd.enable is true
-
-
-# stop
-
-- name: "[systemd] Disable and stop service: named"
-  systemd:
-    name: "mbusd@{{ services.mbusd.device }}"
-    enabled: no
-    state: stopped
-  when: 
-    - ansible_service_mgr == "systemd"
-    - services.mbusd.enable is false
-
-
-# deinstall
-
-- name: "[Archlinux] Remove package: mbusd"
-  pacman:
-    name: mbusd
-    state: absent
-  when: 
-    - ansible_distribution == "Archlinux" 
+    - services.mbusd.enable is defined
     - services.mbusd.enable is false
 
-
-# remove leftover files
-
-- name: "Remove directory: /etc/mbusd"
-  file:
-    path: /etc/mbusd
-    state: absent
-  when: 
-    - ansible_distribution == "Alpine" 
-    - services.mbusd.enable is false
diff --git a/roles/mbusd/tasks/remove.yml b/roles/mbusd/tasks/remove.yml
@@ -0,0 +1,28 @@
+---
+
+- name: "[systemd] Disable and stop service: mbusd"
+  systemd:
+    name: "mbusd"
+    enabled: no
+    state: stopped
+  when: 
+    - ansible_service_mgr == "systemd"
+
+
+- name: "[Archlinux] Remove package: mbusd"
+  pacman:
+    name: mbusd
+    state: absent
+  when: 
+    - ansible_distribution == "Archlinux" 
+
+
+
+- name: "Delete leftovers"
+  file:
+    path: "{{item}}"
+    state: absent
+  with_items:
+    - /etc/mbusd
+    - /etc/mbusd.conf
+    - /etc/systemd/system/mbusd.service
diff --git a/roles/mbusd/tasks/start.yml b/roles/mbusd/tasks/start.yml
@@ -0,0 +1,10 @@
+---
+
+- name: "[systemd] Enable and start service: mbusd"
+  systemd:
+    daemon_reload: yes
+    name: "mbusd"
+    enabled: yes
+    state: started
+  when: 
+    - ansible_service_mgr == "systemd"
diff --git a/roles/mbusd/templates/mbusd.conf.j2 b/roles/mbusd/templates/mbusd.conf.j2
@@ -2,10 +2,10 @@
 device = /dev/{{ services.mbusd.device }}
 
 # Serial port speed
-speed = {{ services.mbusd.baudrate }}
+speed = {{ services.mbusd.baudrate | default(9600) }}
 
 # Serial port mode
-mode = {{ services.mbusd.mode }}
+mode = {{ services.mbusd.mode | default("8n1") }}
 
 # RS-485 data direction control type (addc, rts, sysfs_0, sysfs_1)
 trx_control = addc

@@ -16,7 +16,7 @@ trx_control = addc
 ############# TCP port settings #############
 
 # TCP server port number
-port = {{ services.mbusd.port }}
+port = {{ services.mbusd.port | default(502) }}
 
 # Maximum number of simultaneous TCP connections
 maxconn = 32
diff --git a/roles/mbusd/templates/mbusd.service.j2 b/roles/mbusd/templates/mbusd.service.j2
@@ -0,0 +1,19 @@
+#
+# !!! This file is managed by Ansible !!!
+#
+
+[Unit]
+Description=Modbus TCP to Modbus RTU (RS-232/485) gateway.
+Requires=network.target
+After=network-online.target
+Wants=network-online.target
+
+[Service]
+ExecStart=/usr/bin/mbusd -d -v2 -L - -c /etc/mbusd.conf -p /dev/{{ services.mbusd.device }}
+Restart=on-failure
+RestartSec=1
+StandardOutput=journal
+StandardError=journal
+
+[Install]
+WantedBy=multi-user.target+
\ No newline at end of file