---
# Script to run apt update and upgrade command on servers
#
# To run the script:
# ansible-playbook -K launch_apt_upgrade.yml -i hosts-xxx -e 'target=targethost'
# ansible-playbook -K launch_apt_upgrade.yml -i hosts-xxx -e 'target=targethost reboot=1' --limit=*.mydomain.com
#

- name: Update and upgrade apt packages
  hosts: '{{target}}'
  become: yes
  become_method: sudo
  become_user: root
  tasks:
  - name: Switch instances in maintenance mode
    command: /home/admin/wwwroot/dolibarr_sellyoursaas/scripts/make_instances_offlineonline.sh maintenance.php offline
    register: command_output

  - debug:
      var: command_output.stdout_lines
      
  - name: Launch apt
    apt:
      upgrade: yes
      update_cache: yes
      cache_valid_time: 86400
    register: command_output

  - name: Reboot the server and wait for it to come back up.
    reboot:
    when:
      - reboot is defined
      
  - debug:
      var: command_output.stdout_lines
      
  - name: Restore the production mode
    command: /home/admin/wwwroot/dolibarr_sellyoursaas/scripts/make_instances_offlineonline.sh maintenance.php online
    register: command_output

  - debug:
      var: command_output.stdout_lines
      
   
