Skip to content

Instantly share code, notes, and snippets.

@daggerhart
Last active May 24, 2017 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daggerhart/85ba73f736a1b41ee4c9b15d0346332e to your computer and use it in GitHub Desktop.
Save daggerhart/85ba73f736a1b41ee4c9b15d0346332e to your computer and use it in GitHub Desktop.
Ansible task for Debian/Ubuntu that updates package cache, then as necessary: upgrades, reboots, wait for server to come back up to continue.
# Upgrade Debian/Ubuntu based systems and reboot if necessary.
---
- name: "Update apt-get cache"
apt:
update_cache: yes
- name: "Check if there are packages available to be installed/upgraded"
command: /usr/lib/update-notifier/apt-check --package-names
register: packages
- name: "Upgrade all packages to the latest version"
apt:
upgrade: dist
when: packages.stderr != ""
- name: "Check if a reboot is required"
register: rebootfile
stat: path=/var/run/reboot-required get_md5=no
- name: "Rebooting server if needed"
shell: sleep 2 && shutdown -r now "Ansible reboot"
async: 1
poll: 0
ignore_errors: true
when: rebootfile.stat.exists == true
- name: "Waiting for server to come back after reboot"
local_action: wait_for
args:
host: "{{ inventory_hostname }}"
port: 22
state: started
delay: 20
timeout: 120
become: false
when: rebootfile.stat.exists == true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment