- name: "Upgrading EVERYTHING" apt: update_cache=yes upgrade=full - name: "autoremove" apt: autoremove=yes - name: Check if reboot is required stat: path: /var/run/reboot-required register: reboot_required_file - name: Prompt for rebooting pause: prompt: "Press ENTER to reboot {{ item }} now, or Ctrl+C to abort." # We need to check for the existence of 'reboot_required_file' first because play_hosts also # include hosts that have failed. When a host has failed, it stops executing and thus doesn't # have 'reboot_required_file'. And if we try to access 'stat', boom! failure. We don't want that. when: "'reboot_required_file' in hostvars[item] and hostvars[item]['reboot_required_file'].stat.exists" with_items: "{{ play_hosts }}" # ref https://support.ansible.com/hc/en-us/articles/201958037-Reboot-a-server-and-wait-for-it-to-come-back - name: Rebooting machine shell: sleep 2 && shutdown -r now "Ansible updates triggered" async: 1 poll: 0 ignore_errors: true when: reboot_required_file.stat.exists - name: Waiting for the machine to come back local_action: wait_for host={{ ansible_host }} state=started port=22 delay=30 become: no when: reboot_required_file.stat.exists