Files
my_cluster/fix-auto-bind.yml
T

106 lines
3.0 KiB
YAML

---
- hosts: fnos
gather_facts: no
become: yes
vars:
src_bases:
fn-home: "/vol00/HGST HSH721414ALE6M4"
fn-tower: "/vol00/HSH721414ALN6M0"
tasks:
- name: Deploy fixed auto-bind script
ansible.builtin.copy:
dest: /usr/local/bin/auto-bind-same-dirs.sh
mode: "0755"
content: |
#!/bin/bash
set -euo pipefail
SRC_BASE="{{ src_bases[inventory_hostname] }}"
DST_BASE="/vol1/1000"
LOG_TAG="auto-bind-same-dirs"
logger -t "$LOG_TAG" "start"
if [ ! -d "$SRC_BASE" ]; then
logger -t "$LOG_TAG" "source base not found: $SRC_BASE"
exit 0
fi
if [ ! -d "$DST_BASE" ]; then
logger -t "$LOG_TAG" "target base not found: $DST_BASE"
exit 0
fi
find "$DST_BASE" -mindepth 1 -maxdepth 1 -type d | while read -r dst_dir; do
name="$(basename "$dst_dir")"
src_dir="$SRC_BASE/$name"
if [ -d "$src_dir" ]; then
if mountpoint -q "$dst_dir" && stat "$dst_dir/." >/dev/null 2>&1; then
logger -t "$LOG_TAG" "skip already mounted: $dst_dir"
else
umount "$dst_dir" 2>/dev/null || true
mount --bind "$src_dir" "$dst_dir"
logger -t "$LOG_TAG" "mounted $src_dir -> $dst_dir"
fi
fi
done
logger -t "$LOG_TAG" "done"
exit 0
- name: Deploy service unit
ansible.builtin.copy:
dest: /etc/systemd/system/auto-bind-same-dirs.service
mode: "0644"
content: |
[Unit]
Description=Bind mount NAS directories
[Service]
Type=oneshot
ExecStart=/usr/local/bin/auto-bind-same-dirs.sh
- name: Deploy timer unit
ansible.builtin.copy:
dest: /etc/systemd/system/auto-bind-same-dirs.timer
mode: "0644"
content: |
[Unit]
Description=Periodic bind mount check for external disks
[Timer]
OnBootSec=15s
OnUnitActiveSec=60s
[Install]
WantedBy=timers.target
- name: Stop and disable old path unit
ansible.builtin.systemd:
name: auto-bind-same-dirs.path
state: stopped
enabled: false
failed_when: false
- name: Remove old path unit file
ansible.builtin.file:
path: /etc/systemd/system/auto-bind-same-dirs.path
state: absent
- name: systemd daemon-reload
ansible.builtin.systemd:
daemon_reload: true
- name: Reset any failed state on auto-bind units
ansible.builtin.command:
cmd: systemctl reset-failed auto-bind-same-dirs.service auto-bind-same-dirs.timer
failed_when: false
changed_when: false
- name: Enable and start timer
ansible.builtin.systemd:
name: auto-bind-same-dirs.timer
enabled: true
state: started