Files
Server_Monitorizare_v2/ansible/playbooks/migrate_to_wmt.yml

137 lines
5.2 KiB
YAML

---
# Migrate devices from Prezenta app to WMT
# ──────────────────────────────────────────────────────────────────────────
# What this playbook does (in order):
#
# 1. Create /home/pi/Desktop/WMT on the target
# 2. Copy /home/pi/Desktop/WMT_project from the CONTROLLER to /home/pi/Desktop/WMT on the target
# 3. Read /home/pi/Desktop/Prezenta/data/idmasa.txt from the target
# 4. Write that value as work_place in WMT/data/config.txt (replaces notconfig)
# 5. Update ~/.config/wayfire.ini [autostart] start_python to launch from WMT
# 6. Rename /home/pi/Desktop/Prezenta → /home/pi/Desktop/Prezenta_Old_Data
# 7. Reboot the host
#
# Run via: Ansible > Playbooks > "Migrate to WMT" or POST /api/ansible/execute
# ──────────────────────────────────────────────────────────────────────────
- name: Migrate devices from Prezenta to WMT
hosts: all
gather_facts: false
become: false
tasks:
# ── 1. Ensure destination directory exists ────────────────────────────
- name: Create WMT directory on target
file:
path: /home/pi/Desktop/WMT
state: directory
owner: pi
group: pi
mode: '0755'
# ── 2. Copy WMT_project from controller → target as WMT ──────────────
- name: Copy WMT_project folder to target as /home/pi/Desktop/WMT
copy:
src: /home/pi/Desktop/WMT_project/
dest: /home/pi/Desktop/WMT/
owner: pi
group: pi
mode: preserve
force: true
# ── 3. Ensure WMT data directory exists (in case config.txt is missing)
- name: Ensure WMT/data directory exists
file:
path: /home/pi/Desktop/WMT/data
state: directory
owner: pi
group: pi
mode: '0755'
# ── 4. Read idmasa.txt from the Prezenta data folder ─────────────────
- name: Read idmasa.txt from Prezenta
slurp:
src: /home/pi/Desktop/Prezenta/data/idmasa.txt
register: idmasa_raw
- name: Decode idmasa value
set_fact:
work_place_value: "{{ (idmasa_raw.content | b64decode).strip() }}"
- name: Show detected work_place value
debug:
msg: "work_place will be set to: '{{ work_place_value }}'"
# ── 5. Ensure config.txt has a [device] section with work_place ───────
- name: Ensure [device] section exists in config.txt
ini_file:
path: /home/pi/Desktop/WMT/data/config.txt
section: device
option: work_place
value: "{{ work_place_value }}"
backup: true
create: true
owner: pi
group: pi
mode: '0644'
# ── 6. Update wayfire.ini autostart to launch from WMT ───────────────
- name: Update wayfire.ini start_python path from Prezenta to WMT
lineinfile:
path: /home/pi/.config/wayfire.ini
regexp: "^start_python\\s*=.*"
line: "start_python = lxterminal -e \"bash -c 'cd /home/pi/Desktop/WMT; python3 app.py; exec bash'\""
backrefs: false
backup: true
- name: Confirm wayfire.ini change
command: grep "start_python" /home/pi/.config/wayfire.ini
register: wayfire_check
changed_when: false
- name: Show wayfire.ini start_python line
debug:
msg: "{{ wayfire_check.stdout }}"
# ── 7. Kill any running Prezenta app.py before renaming ──────────────
- name: Kill any process running from Prezenta app.py
shell: pkill -f "python3.*Prezenta/app.py"
ignore_errors: true
changed_when: false
- name: Wait a moment for the process to terminate
wait_for:
timeout: 3
# ── 8. Rename Prezenta → Prezenta_Old_Data ─────────────────────────── #
- name: Check if Prezenta folder exists
stat:
path: /home/pi/Desktop/Prezenta
register: prezenta_stat
- name: Check if Prezenta_Old_Data already exists
stat:
path: /home/pi/Desktop/Prezenta_Old_Data
register: prezenta_old_stat
- name: Rename Prezenta to Prezenta_Old_Data
command: mv /home/pi/Desktop/Prezenta /home/pi/Desktop/Prezenta_Old_Data
when:
- prezenta_stat.stat.exists
- not prezenta_old_stat.stat.exists
- name: Warn if Prezenta_Old_Data already exists (rename skipped)
debug:
msg: "⚠ Prezenta_Old_Data already exists — rename skipped to avoid overwrite."
when: prezenta_old_stat.stat.exists
# ── 9. Reboot ─────────────────────────────────────────────────────────
- name: Reboot host to apply all changes
become: true
reboot:
msg: "Rebooting after WMT migration"
reboot_timeout: 180
pre_reboot_delay: 3
post_reboot_delay: 15