Skip to content

Instantly share code, notes, and snippets.

@seriald
Forked from maethor/debian_upgrade.yml
Created May 28, 2016 04:38
Show Gist options
  • Save seriald/79a3a2451b29cf5e86fbe3b9654d9929 to your computer and use it in GitHub Desktop.
Save seriald/79a3a2451b29cf5e86fbe3b9654d9929 to your computer and use it in GitHub Desktop.
Ansible playbook to update and upgrade Debian hosts
---
- hosts: all
sudo: yes
tasks:
- name: Update packages list
apt: update_cache=yes
when: ansible_os_family == 'Debian'
- name: List packages to upgrade (1/2)
shell: aptitude -q -F%p --disable-columns search "~U"
register: updates
changed_when: False
when: ansible_os_family == 'Debian'
- name: List packages to upgrade (2/2)
debug: msg="{{ updates.stdout_lines | count }} packages to upgrade ({{ updates.stdout_lines | join(', ') }})"
when: (ansible_os_family == 'Debian' and updates.stdout_lines)
- name: Upgrade packages
apt: upgrade=safe
when: ansible_os_family == 'Debian'
- name: Check what the new version is
shell: lsb_release -r | awk '{print $2}'
changed_when: False
register: new_release
- name: Notify distribution version upgrade
debug: msg="Debian has been upgraded from {{ ansible_lsb.release }} to {{ new_release.stdout }}"
when: ansible_lsb.release != new_release.stdout
- name: List services to restart (1/2)
shell: checkrestart | grep ^service | awk '{print $2}'
register: services
changed_when: False
when: ansible_os_family == 'Debian'
- name: List services to restart (2/2)
debug: msg="{{ services.stdout_lines | count }} services to restart ({{ services.stdout_lines | join (', ') }})"
when: (ansible_os_family == 'Debian' and services.stdout_lines)
@seriald
Copy link
Author

seriald commented May 28, 2016

Run with --ask-sudo-pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment