semaphore/synology_upgrade_ssh.yml

205 lines
6.5 KiB
YAML

---
- name: Synology DSM - Télécharger (et optionnellement installer) une mise à jour via SSH
hosts: synology
gather_facts: false
vars:
# Si tu veux aussi lancer l'installation après le download
do_install: false
# Si tu veux reboot après install (DSM redémarre parfois tout seul selon méthode)
do_reboot: false
# Timeout long pour les téléchargements
download_timeout: 7200
tasks:
# ------------------------------------------------------------
# 0) Vérifs / infos système (DSM version)
# ------------------------------------------------------------
- name: Lire la version DSM (DSM6/DSM7)
ansible.builtin.shell: |
if test -f /etc.defaults/VERSION; then
cat /etc.defaults/VERSION
elif test -f /etc/VERSION; then
cat /etc/VERSION
else
echo "VERSION_FILE_NOT_FOUND"
fi
args:
executable: /bin/bash
register: dsm_version
changed_when: false
- name: Afficher version DSM
ansible.builtin.debug:
var: dsm_version.stdout_lines
# ------------------------------------------------------------
# 1) Trouver synoupgrade (chemins Synology)
# ------------------------------------------------------------
- name: Détecter synoupgrade (sans [ ])
ansible.builtin.shell: |
if command -v synoupgrade >/dev/null 2>&1; then
command -v synoupgrade
exit 0
fi
for p in \
/usr/syno/sbin/synoupgrade \
/usr/sbin/synoupgrade \
/sbin/synoupgrade \
/bin/synoupgrade \
/usr/bin/synoupgrade
do
if test -x "$p"; then
echo "$p"
exit 0
fi
done
echo "MISSING"
exit 0
args:
executable: /bin/bash
register: synoupgrade_path
changed_when: false
- name: Enregistrer le binaire synoupgrade
ansible.builtin.set_fact:
synoupgrade_bin: "{{ (synoupgrade_path.stdout | trim) }}"
- name: Fail si synoupgrade introuvable
ansible.builtin.fail:
msg: "synoupgrade introuvable sur ce NAS (DSM). stdout='{{ synoupgrade_path.stdout | trim }}'"
when: synoupgrade_bin == "MISSING" or synoupgrade_bin == ""
- name: Debug synoupgrade
ansible.builtin.debug:
msg: "synoupgrade détecté: {{ synoupgrade_bin }}"
# ------------------------------------------------------------
# 2) Check si une MAJ DSM est dispo
# ------------------------------------------------------------
- name: Check mise à jour disponible (best effort)
ansible.builtin.shell: |
# Selon DSM, différentes options existent.
# On essaye plusieurs modes sans échouer le playbook.
set +e
"{{ synoupgrade_bin }}" --help 2>/dev/null | head -n 50
echo "---- TRY: check ----"
"{{ synoupgrade_bin }}" check 2>&1
rc1=$?
echo "---- TRY: --check ----"
"{{ synoupgrade_bin }}" --check 2>&1
rc2=$?
echo "---- TRY: --status ----"
"{{ synoupgrade_bin }}" --status 2>&1
rc3=$?
echo "RCs: check=$rc1 --check=$rc2 --status=$rc3"
exit 0
args:
executable: /bin/bash
register: check_out
changed_when: false
- name: Afficher résultat du check
ansible.builtin.debug:
var: check_out.stdout_lines
# Heuristique : si on voit des mots indiquant une MAJ dispo
- name: Déterminer si une mise à jour semble dispo (heuristique)
ansible.builtin.set_fact:
update_available: >-
{{
(
(check_out.stdout | lower).find('update') != -1
or (check_out.stdout | lower).find('upgrade') != -1
or (check_out.stdout | lower).find('available') != -1
or (check_out.stdout | lower).find('download') != -1
or (check_out.stdout | lower).find('new') != -1
or (check_out.stdout | lower).find('télécharg') != -1
or (check_out.stdout | lower).find('dispon') != -1
)
}}
- name: Info dispo maj
ansible.builtin.debug:
msg: "MAJ dispo (heuristique) = {{ update_available }}"
# ------------------------------------------------------------
# 3) Télécharger la MAJ (si dispo)
# ------------------------------------------------------------
- name: Télécharger la mise à jour DSM (best effort)
ansible.builtin.shell: |
set -e
# On tente plusieurs syntaxes selon versions DSM.
# La commande correcte dépend du modèle/DSM.
# L'idée: déclencher le download depuis les dépôts Synology.
#
# Tentative 1:
"{{ synoupgrade_bin }}" download || true
# Tentative 2:
"{{ synoupgrade_bin }}" --download || true
# Tentative 3:
"{{ synoupgrade_bin }}" download --start || true
echo "Download commands sent."
args:
executable: /bin/bash
register: dl_out
changed_when: true
when: update_available | bool
- name: Afficher sortie download
ansible.builtin.debug:
var: dl_out.stdout_lines
when: update_available | bool
- name: Stop si aucune MAJ détectée
ansible.builtin.debug:
msg: "Aucune mise à jour détectée (heuristique). Rien à télécharger."
when: not (update_available | bool)
# ------------------------------------------------------------
# 4) Optionnel : installer après téléchargement
# ------------------------------------------------------------
- name: Lancer l'installation DSM (optionnel)
ansible.builtin.shell: |
set -e
"{{ synoupgrade_bin }}" start || true
"{{ synoupgrade_bin }}" --start || true
"{{ synoupgrade_bin }}" upgrade || true
echo "Install commands sent."
args:
executable: /bin/bash
register: install_out
changed_when: true
when:
- update_available | bool
- do_install | bool
- name: Afficher sortie install
ansible.builtin.debug:
var: install_out.stdout_lines
when:
- update_available | bool
- do_install | bool
# ------------------------------------------------------------
# 5) Optionnel : reboot
# ------------------------------------------------------------
- name: Reboot NAS (optionnel)
ansible.builtin.shell: |
sudo reboot || reboot
args:
executable: /bin/bash
when:
- update_available | bool
- do_install | bool
- do_reboot | bool