diff --git a/group_vars/all.yml b/group_vars/all.yml index 4bb3807..7d2dabb 100644 --- a/group_vars/all.yml +++ b/group_vars/all.yml @@ -277,4 +277,18 @@ hydroqc2mqtt: 66363939656339373365303532313365333964326139643261653832653132636237 homeassistant: - version: 2024.1.3 \ No newline at end of file + version: 2024.1.3 + +nut_server: + ansible_host: usbip + ups: + liebert-ps1: + driver: usbhid-ups + vendorid: "10af" + productid: "0001" + +nut_client: + ups: liebert-ps1 + sched: + # How many seconds we are allowed to run on battery + on_bat: 300 \ No newline at end of file diff --git a/hosts b/hosts index 41379f6..00ef7eb 100644 --- a/hosts +++ b/hosts @@ -18,6 +18,11 @@ all: hosts: router-0: ansible_host: 192.168.10.10 + nut_client: + ups: liebert-ps1 + sched: + # We want to shutdown this server last + on_bat: 3600 server-0: ansible_host: 192.168.10.11 vfio_pci_ids: @@ -28,6 +33,11 @@ all: # - '1002:731f' # Advanced Micro Devices, Inc. [AMD/ATI] Navi 10 [Radeon RX 5600 OEM/5600 XT / 5700/5700 XT] - '10de:13c2' # NVIDIA Corporation GM204 [GeForce GTX 970] - '10de:13f1' # VGA compatible controller [0300]: NVIDIA Corporation GM204GL [Quadro M4000] + nut_client: + ups: liebert-ps1 + sched: + # We want to shutdown this server quick + on_bat: 10 bastion: hosts: bastion: diff --git a/playbook.yml b/playbook.yml index 1233d87..28a5b0c 100644 --- a/playbook.yml +++ b/playbook.yml @@ -1,13 +1,22 @@ + +- hosts: usbip + roles: + - usbip + - nut-server + - hosts: proxmox roles: - proxmox + - nut-client - hosts: k3s-server roles: - k3s-server + - nut-client - hosts: k3s-agent roles: - k3s-agent + - nut-client - hosts: generic-linux roles: @@ -52,8 +61,4 @@ - hosts: cups roles: - - cups - -- hosts: usbip - roles: - - usbip \ No newline at end of file + - cups \ No newline at end of file diff --git a/roles/k3s-agent/templates/k3s.service b/roles/k3s-agent/templates/k3s.service index 328942b..40e22ac 100644 --- a/roles/k3s-agent/templates/k3s.service +++ b/roles/k3s-agent/templates/k3s.service @@ -6,7 +6,7 @@ After=network-online.target Type=notify ExecStartPre=-/sbin/modprobe br_netfilter ExecStartPre=-/sbin/modprobe overlay -ExecStart=/usr/local/bin/k3s agent --server https://{{ hostvars[groups['k3s-server'][0]]['ansible_facts']['default_ipv4']['address'] }}:6443 --token {{ hostvars[groups['k3s-server'][0]]['token'] }} {{ k3s.extra_agent_args | default("") }} {{ k3s_extra_args }} +ExecStart=/usr/local/bin/k3s agent --server https://{{ hostvars[groups['k3s-server'][0]]['ansible_default_ipv4']['address'] }}:6443 --token {{ hostvars[groups['k3s-server'][0]]['token'] }} {{ k3s.extra_agent_args | default("") }} {{ k3s_extra_args }} KillMode=process Delegate=yes # Having non-zero Limit*s causes performance problems due to accounting overhead diff --git a/roles/nut-client/files/nut.conf b/roles/nut-client/files/nut.conf new file mode 100644 index 0000000..efe5760 --- /dev/null +++ b/roles/nut-client/files/nut.conf @@ -0,0 +1 @@ +MODE=netclient \ No newline at end of file diff --git a/roles/nut-client/files/upssched-cmd b/roles/nut-client/files/upssched-cmd new file mode 100644 index 0000000..9b55c26 --- /dev/null +++ b/roles/nut-client/files/upssched-cmd @@ -0,0 +1,14 @@ +#!/bin/sh +case $1 in + shutdown) + logger -t upssched-cmd "UPS on battery too long, shutdown" + /usr/bin/sudo /usr/sbin/shutdown -h +0 + ;; + shutdown-critical) + logger -t upssched-cmd "UPS on battery critical, forced shutdown" + /usr/sbin/upsmon -c fsd + ;; + *) + logger -t upssched-cmd "Unrecognized command: $1" + ;; +esac \ No newline at end of file diff --git a/roles/nut-client/handlers/main.yml b/roles/nut-client/handlers/main.yml new file mode 100644 index 0000000..e13080f --- /dev/null +++ b/roles/nut-client/handlers/main.yml @@ -0,0 +1,5 @@ +- name: Restart nut-client + service: + name: nut-client + enabled: true + state: restarted diff --git a/roles/nut-client/tasks/main.yml b/roles/nut-client/tasks/main.yml new file mode 100644 index 0000000..a9ea292 --- /dev/null +++ b/roles/nut-client/tasks/main.yml @@ -0,0 +1,41 @@ +- name: Install NUT + apt: + update_cache: true + name: + - nut-client + +- name: Configure upsmon.conf + template: + src: upsmon.conf + dest: /etc/nut/upsmon.conf + notify: + - Restart nut-client + +- name: Configure nut.conf + copy: + src: nut.conf + dest: /etc/nut/nut.conf + notify: + - Restart nut-client + +- name: Configure upssched.conf + template: + src: upssched.conf + dest: /etc/nut/upssched.conf + notify: + - Restart nut-client + +- name: Configure upssched-cmd + copy: + src: upssched-cmd + dest: /etc/nut/upssched-cmd + mode: '0755' + notify: + - Restart nut-client + +- name: Allow nut user to invoke shutdown with sudo + lineinfile: + path: /etc/sudoers + line: 'nut ALL=NOPASSWD: /usr/sbin/shutdown' + regexp: '^nut\s+' + validate: '/usr/sbin/visudo -cf %s' \ No newline at end of file diff --git a/roles/nut-client/templates/upsmon.conf b/roles/nut-client/templates/upsmon.conf new file mode 100644 index 0000000..65db913 --- /dev/null +++ b/roles/nut-client/templates/upsmon.conf @@ -0,0 +1,21 @@ +# TODO: setup credentials +MONITOR {{ nut_client.ups }}@{{ hostvars[nut_server.ansible_host]['ansible_default_ipv4']['address'] }} 1 monuser admin secondary + + +MINSUPPLIES 1 +SHUTDOWNCMD "/sbin/shutdown -h +0" +NOTIFYCMD /usr/sbin/upssched +POLLFREQ 5 +POLLFREQALERT 5 +HOSTSYNC 15 +DEADTIME 15 +POWERDOWNFLAG /run/killpower + +NOTIFYFLAG ONLINE SYSLOG+WALL+EXEC +NOTIFYFLAG ONBATT SYSLOG+WALL+EXEC +NOTIFYFLAG LOWBATT SYSLOG+WALL+EXEC + +RBWARNTIME 43200 +NOCOMMWARNTIME 300 + +FINALDELAY 5 \ No newline at end of file diff --git a/roles/nut-client/templates/upssched.conf b/roles/nut-client/templates/upssched.conf new file mode 100644 index 0000000..066b995 --- /dev/null +++ b/roles/nut-client/templates/upssched.conf @@ -0,0 +1,11 @@ +CMDSCRIPT /etc/nut/upssched-cmd +PIPEFN /run/nut/upssched.pipe +LOCKFN /run/nut/upssched.lock + +AT ONBATT * START-TIMER shutdown {{ nut_client.sched.on_bat }} +AT ONLINE * CANCEL-TIMER shutdown +AT LOWBATT * EXECUTE shutdown-critical +#AT COMMBAD * START-TIMER commbad {#{ nut_client.sched.comm_bad}#} +#AT COMMOK * CANCEL-TIMER commbad commok +#AT NOCOMM * EXECUTE commbad +#AT SHUTDOWN * EXECUTE powerdown \ No newline at end of file diff --git a/roles/nut-server/files/nut.conf b/roles/nut-server/files/nut.conf new file mode 100644 index 0000000..e141f91 --- /dev/null +++ b/roles/nut-server/files/nut.conf @@ -0,0 +1 @@ +MODE=netserver diff --git a/roles/nut-server/files/upsd.conf b/roles/nut-server/files/upsd.conf new file mode 100644 index 0000000..61c76fc --- /dev/null +++ b/roles/nut-server/files/upsd.conf @@ -0,0 +1 @@ +LISTEN 0.0.0.0 3493 diff --git a/roles/nut-server/files/upsd.users b/roles/nut-server/files/upsd.users new file mode 100644 index 0000000..c2ccda4 --- /dev/null +++ b/roles/nut-server/files/upsd.users @@ -0,0 +1,4 @@ +# TODO: setup credentials +[monuser] + password = admin + upsmon secondary diff --git a/roles/nut-server/handlers/main.yml b/roles/nut-server/handlers/main.yml new file mode 100644 index 0000000..9f7821c --- /dev/null +++ b/roles/nut-server/handlers/main.yml @@ -0,0 +1,17 @@ +- name: Restart nut-server + service: + name: nut-server + enabled: true + state: restarted + +- name: Restart nut-client + service: + name: nut-server + enabled: true + state: restarted + +- name: Restart nut-monitor + service: + name: nut-monitor + enabled: true + state: restarted diff --git a/roles/nut-server/tasks/main.yml b/roles/nut-server/tasks/main.yml new file mode 100644 index 0000000..4d1844a --- /dev/null +++ b/roles/nut-server/tasks/main.yml @@ -0,0 +1,52 @@ +- name: Install NUT + apt: + update_cache: true + name: + - nut + - nut-client + - nut-server + +- name: Configure ups.conf + template: + src: ups.conf + dest: /etc/nut/ups.conf + notify: + - Restart nut-server + - Restart nut-client + - Restart nut-monitor + +- name: Configure upsmon.conf + template: + src: upsmon.conf + dest: /etc/nut/upsmon.conf + notify: + - Restart nut-server + - Restart nut-client + - Restart nut-monitor + +- name: Configure upsd.conf + copy: + src: upsd.conf + dest: /etc/nut/upsd.conf + notify: + - Restart nut-server + - Restart nut-client + - Restart nut-monitor + +- name: Configure nut.conf + copy: + src: nut.conf + dest: /etc/nut/nut.conf + notify: + - Restart nut-server + - Restart nut-client + - Restart nut-monitor + +- name: Configure upsd.users + copy: + src: upsd.users + dest: /etc/nut/upsd.users + notify: + - Restart nut-server + - Restart nut-client + - Restart nut-monitor diff --git a/roles/nut-server/templates/ups.conf b/roles/nut-server/templates/ups.conf new file mode 100644 index 0000000..92187d4 --- /dev/null +++ b/roles/nut-server/templates/ups.conf @@ -0,0 +1,9 @@ +maxretry = 3 + +{% for name, config in nut_server.ups.items() %} +[{{ name }}] + driver = {{ config.driver }} + port = auto + vendorid = {{ config.vendorid }} + productid = {{ config.productid }} +{% endfor %} diff --git a/roles/nut-server/templates/upsmon.conf b/roles/nut-server/templates/upsmon.conf new file mode 100644 index 0000000..0735707 --- /dev/null +++ b/roles/nut-server/templates/upsmon.conf @@ -0,0 +1,4 @@ +# TODO: setup credentials +{% for name in nut_server.ups.keys() %} +MONITOR {{ name }}@localhost 1 monuser admin primary +{% endfor %}