You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
5.6 KiB
178 lines
5.6 KiB
---
|
|
- name: NUT pi install/maintenance
|
|
hosts: nutpis
|
|
tasks:
|
|
- name: Installing pre-prerequisites
|
|
ansible.builtin.package:
|
|
name: gpg
|
|
state: latest
|
|
become: true
|
|
become_method: sudo
|
|
- name: Checking for influx repo key
|
|
ansible.builtin.get_url:
|
|
dest: /usr/share/keyrings/influxdb.key
|
|
url: https://repos.influxdata.com/influxdb.key
|
|
checksum: "sha256:23a1c8836f0afc5ed24e0486339d7cc8f6790b83886c4c96995b88a061c5bb5d"
|
|
mode: '0444'
|
|
become: true
|
|
become_method: sudo
|
|
- name: Dearmoring influx GPG key if needed
|
|
ansible.builtin.command:
|
|
cmd: "gpg --output /etc/apt/trusted.gpg.d/influxdb.gpg --dearmor /usr/share/keyrings/influxdb.key"
|
|
creates: /etc/apt/trusted.gpg.d/influxdb.gpg
|
|
become: true
|
|
become_method: sudo
|
|
- name: Verifying influx repository is installed
|
|
ansible.builtin.apt_repository:
|
|
repo: deb [signed-by=/etc/apt/trusted.gpg.d/influxdb.gpg] https://repos.influxdata.com/debian stable main
|
|
filename: influxdata.list
|
|
state: present
|
|
become: true
|
|
become_method: sudo
|
|
- name: Verifying log2ram key is installed
|
|
ansible.builtin.get_url:
|
|
dest: /usr/share/keyrings/azlux-archive-keyring.gpg
|
|
url: https://azlux.fr/repo.gpg
|
|
mode: '0444'
|
|
become: true
|
|
become_method: sudo
|
|
- name: Verifying log2ram repository installed
|
|
ansible.builtin.apt_repository:
|
|
repo: deb [signed-by=/usr/share/keyrings/azlux-archive-keyring.gpg] http://packages.azlux.fr/debian/ bullseye main
|
|
filename: azlux.list
|
|
state: present
|
|
become: true
|
|
become_method: sudo
|
|
|
|
- name: Performing APT cache update and package upgrade (SLOW on Pi0)
|
|
apt:
|
|
upgrade: yes
|
|
update_cache: yes
|
|
cache_valid_time: 86400
|
|
become: true
|
|
become_method: sudo
|
|
- name: Installing any new packages
|
|
ansible.builtin.package:
|
|
name:
|
|
- vim
|
|
- nut-server
|
|
- wget
|
|
- telegraf
|
|
- log2ram
|
|
state: present
|
|
become: true
|
|
become_method: sudo
|
|
|
|
- name: Download vim config from aperture (root)
|
|
ansible.builtin.get_url:
|
|
dest: ~/.vimrc
|
|
url: https://aperture.dismyserver.net/.vimrc
|
|
become: true
|
|
become_method: sudo
|
|
- name: Download vim config from aperture (pi)
|
|
ansible.builtin.get_url:
|
|
dest: /home/pi/.vimrc
|
|
url: https://aperture.dismyserver.net/.vimrc
|
|
|
|
- name: Ensure netserver mode is set
|
|
ansible.builtin.lineinfile:
|
|
path: /etc/nut/nut.conf
|
|
line: "MODE=netserver"
|
|
state: present
|
|
become: true
|
|
become_method: sudo
|
|
- name: Ensure listening port is set
|
|
ansible.builtin.lineinfile:
|
|
path: /etc/nut/nut.conf
|
|
insertafter: "# you'll need to restart upsd, reload will have no effect"
|
|
line: "LISTEN 127.0.0.1 3493"
|
|
state: present
|
|
become: true
|
|
become_method: sudo
|
|
register: listenport
|
|
|
|
- name: Check if ups.conf is configured
|
|
ansible.builtin.shell:
|
|
cmd: "egrep '^\\s*driver' /etc/nut/ups.conf && echo has || echo needs"
|
|
executable: /bin/bash
|
|
become: true
|
|
become_method: sudo
|
|
register: upsconf_cmd
|
|
|
|
- name: Adding UPS name if not set
|
|
ansible.builtin.lineinfile:
|
|
path: /etc/nut/ups.conf
|
|
line: "[{{ upsname }}]"
|
|
state: present
|
|
become: true
|
|
become_method: sudo
|
|
- name: Creating UPS config if not present
|
|
ansible.builtin.shell:
|
|
cmd: "nut-scanner -U 2>/dev/null | grep driver -A4 > /home/pi/ups.txt"
|
|
executable: /bin/bash
|
|
creates: /home/pi/ups.txt
|
|
become: true
|
|
become_method: sudo
|
|
- name: Adding UPS config if not set
|
|
ansible.builtin.shell:
|
|
cmd: "cat /home/pi/ups.txt >> /etc/nut/ups.conf"
|
|
executable: /bin/bash
|
|
become: true
|
|
become_method: sudo
|
|
when: upsconf_cmd.stdout == "needs"
|
|
|
|
- name: Verify directory exists for python script
|
|
ansible.builtin.file:
|
|
path: /home/pi/telegraf-nut-input
|
|
state: directory
|
|
- name: Verify python template is deployed
|
|
ansible.builtin.template:
|
|
dest: /home/pi/telegraf-nut-input/input.py
|
|
src: ~/ansible/templates/nut-telegraf-input.py.j2
|
|
mode: '0644'
|
|
- name: Verify telegraf template is deployed
|
|
ansible.builtin.template:
|
|
dest: /etc/telegraf/telegraf.d/nut-telegraf.conf
|
|
mode: '0644'
|
|
src: ~/ansible/templates/nut-telegraf.conf.j2
|
|
become: true
|
|
become_method: sudo
|
|
register: telegraf_deployed
|
|
- name: Verify monitoring template is deployed
|
|
ansible.builtin.template:
|
|
dest: /home/pi/nutmon.sh
|
|
src: ~/ansible/templates/nutmon.sh.j2
|
|
mode: '0755'
|
|
|
|
- name: Ensure monitoring script is scheduled
|
|
ansible.builtin.cron:
|
|
name: "Check on the UPS"
|
|
minute: "*/5"
|
|
job: "/home/pi/nutmon.sh"
|
|
user: root
|
|
state: present
|
|
become: true
|
|
become_method: sudo
|
|
|
|
- name: Enable and restart services
|
|
ansible.builtin.service:
|
|
name: telegraf
|
|
state: restarted
|
|
enabled: true
|
|
ignore_errors: true
|
|
become: true
|
|
become_method: sudo
|
|
when: telegraf_deployed.changed
|
|
|
|
- name: Check for OS required reboots
|
|
ansible.builtin.stat:
|
|
path: /var/run/reboot-required
|
|
register: reboot_os
|
|
|
|
- name: Reboot if needed
|
|
ansible.builtin.reboot:
|
|
post_reboot_delay: 30
|
|
become: true
|
|
become_method: sudo
|
|
when: reboot_os.stat.exists or listenport.changed
|