---
# patching.yml
#
# This playbook can be used to patch all the servers in an inventory to the latest on the repo servers
# Variables: 
# - hosts: the host grouper in the inventory -- default: all
# - action: update or upgrade -- default: update
# - delay: minutes to wait after a reboot -- default 5
# 
#
# Patch then restart a node
- hosts: "{{ targets | default('geth_hubs') }}"
  order: sorted
  ignore_unreachable: true
  serial: 1
  vars: 
    ansible_become: yes
    ansible_become_method: sudo
    oldmajor: stretch
    newmajor: buster
  tasks:
    - name: Check /var free percentage
      command: /bin/bash -c "df -m /var | tail -n 1 | awk '{ print $5; }' | sed 's/%//' "
      become: no
      register: df_output

    - name: Verify /var space
      assert: 
        that: 
          - 90 > {{ df_output.stdout }}
        fail_msg: "Not enough free space"

    - apt:
        name: python-apt
        state: present

    - name: apt dist-upgrade
      apt:
          update_cache: yes
          upgrade: dist
      ignore_errors: yes
           
    - name: Replace repo
      command: "sed -i 's/{{ oldmajor }}/{{ newmajor }}/g' /etc/apt/sources.list"
      become: yes

    - name: Update packages
      apt:
          upgrade: full
          update_cache: yes
          autoremove: yes
          autoclean: yes
      ignore_errors: yes

    # - name: Perform firmware-update
    #   command: rpi-update
    #   become: yes

    - reboot:

    - wait_for_connection:
