commit edc824f8b81ba88ae3424dbe720453ff195af270
parent fc2f9eff4190d4ba1f12d2267d76044bad58806b
Author: Leah (ctucx) <leah@ctu.cx>
Date: Wed, 3 Feb 2021 18:19:18 +0100
parent fc2f9eff4190d4ba1f12d2267d76044bad58806b
Author: Leah (ctucx) <leah@ctu.cx>
Date: Wed, 3 Feb 2021 18:19:18 +0100
mbusd: add role
3 files changed, 170 insertions(+), 0 deletions(-)
A
|
117
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/roles/mbusd/files/mbusd@.service b/roles/mbusd/files/mbusd@.service @@ -0,0 +1,15 @@ +[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/tasks/main.yml b/roles/mbusd/tasks/main.yml @@ -0,0 +1,117 @@ +--- + +# 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!" + when: + - system.enableOwnRepos is false + - services.mbusd.enable is true + +- fail: msg="Option 'services.mbusd.device' has to be set!" + when: + - services.mbusd.device is not defined + - services.mbusd.enable is true + +- fail: msg="Option 'services.mbusd.mode' has to be set!" + when: + - services.mbusd.mode is not defined + - services.mbusd.enable is true + +- fail: msg="Option 'services.mbusd.port' has to be set!" + 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 true + +- name: "Create systemd-service file for mbusd" + copy: + src: mbusd@.service + dest: /etc/systemd/system/mbusd@.service + mode: "0755" + owner: root + group: root + +# 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 + 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 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/templates/mbusd.conf.j2 b/roles/mbusd/templates/mbusd.conf.j2 @@ -0,0 +1,36 @@ +# Serial port device name +device = /dev/{{ services.mbusd.device }} + +# Serial port speed +speed = {{ services.mbusd.baudrate }} + +# Serial port mode +mode = {{ services.mbusd.mode }} + +# RS-485 data direction control type (addc, rts, sysfs_0, sysfs_1) +trx_control = addc + +# Sysfs file to use to control data direction +# trx_sysfile = + +############# TCP port settings ############# + +# TCP server port number +port = {{ services.mbusd.port }} + +# Maximum number of simultaneous TCP connections +maxconn = 32 + +# Connection timeout value in seconds +timeout = 60 + +######### Request/response settings ######### + +# Maximum number of request retries +retries = 3 + +# Pause between requests in milliseconds +pause = 100 + +# Response wait time in milliseconds +wait = 500+ \ No newline at end of file