-
Notifications
You must be signed in to change notification settings - Fork 5
/
nut.yml
140 lines (119 loc) · 3.46 KB
/
nut.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
---
- hosts: homelab
vars:
application: nut
# variables are in group_vars. override as needed
nut_host: localhost
nut_mode: netserver
nut_user_mode: "master"
nut_max_retry: 3
nut_ups_devices:
cyberpower: |
driver = usbhid-ups
port = auto
ignorelb
pollinterval = 15
override.battery.charge.low = 20
override.battery.charge.warning = 40
override.battery.runtime.low = 300
override.ups.delay.start = 120
override.ups.delay.shutdown = 120
nut_ups_listen: |
LISTEN 127.0.0.1 3493
LISTEN {{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }} 3493
LISTEN ::1 3493
handlers:
- name: Restart
ansible.builtin.service:
name: "{{ item }}"
state: restarted
enabled: true
loop: >-
{{
['nut-monitor', 'nut-server'] +
(nut_ups_devices | dict2items | map(attribute='key') | map('string') | map('regex_replace', '^', 'nut-driver@') | list)
}}
tasks:
- name: Update apt cache
ansible.builtin.apt:
update_cache: true
cache_valid_time: 600
changed_when: false
- name: Install
ansible.builtin.apt:
name: nut
state: present
update_cache: true
cache_valid_time: 3600
register: result
until: result is success
retries: 5
delay: 5
- name: Reload systemd
ansible.builtin.systemd:
daemon_reload: true
- name: Install NUT configuration files.
ansible.builtin.template:
src: "{{ item }}"
dest: "/etc/nut/{{ item | basename | regex_replace('\\.j2$', '') }}"
owner: root
group: nut
mode: "0655"
with_fileglob:
- "{{ files_directory }}/*"
notify: Restart
- name: Grab public SSH key to add to NVR host
ansible.builtin.slurp:
src: /root/.ssh/id_ed25519.pub
register: __homelab_public_key
- hosts: nvr
handlers:
- name: Restart nut
ansible.builtin.service:
name: nut-monitor
state: restarted
enabled: true
tasks:
- name: Set authorized keys
ansible.posix.authorized_key:
user: root
state: present
key: "{{ item }}"
comment: homelab
loop:
- https://github.com/calvinbui.keys
- "{{ hostvars['homelab.' + common_local_tld]['__homelab_public_key']['content'] | b64decode }}"
- name: Update apt cache
ansible.builtin.apt:
update_cache: true
cache_valid_time: 600
changed_when: false
- name: Install nut
ansible.builtin.apt:
name: nut-client
state: present
register: result
until: result is success
retries: 5
delay: 5
- name: Configure nut
notify: Restart nut
block:
- name: Set nut mode
ansible.builtin.lineinfile:
path: "/etc/nut/{{ item.file }}.conf"
regex: "{{ item.regex }}"
line: "{{ item.line }}"
loop:
- file: nut
regex: "^MODE="
line: "MODE={{ nut_mode }}"
- name: Configure nut
ansible.builtin.blockinfile:
path: "/etc/nut/{{ item.file }}.conf"
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ item.name }}"
block: "{{ item.block }}"
loop:
- file: upsmon
name: monitors
block: "{{ nut_ups_monitors | string }}"