Skip to content

Instantly share code, notes, and snippets.

@maethor
Created July 18, 2014 14:06
Show Gist options
  • Select an option

  • Save maethor/380676f6b1cec8cc7439 to your computer and use it in GitHub Desktop.

Select an option

Save maethor/380676f6b1cec8cc7439 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)
@fefulowe
Copy link
Copy Markdown

fefulowe commented Jul 2, 2017

register: updates
changed_when: False
when: ansible_os_family == 'Debian'
+ failed_when: updates.rc < 0

solves an issue in Debian stretch.v9.0.

[1] fatal: [srvx]: FAILED! => {"changed": false, "cmd": "aptitude -q -F%p --disable-co
lumns search "~U"", "delta": "0:00:00.803267", "end": "2017-07-01 15:20:00.668301", "failed": true, "rc": 1, "start": "2017-
07-01 15:19:59.865034", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}

@skazi0
Copy link
Copy Markdown

skazi0 commented Oct 2, 2017

Better go with failed_when: updates.rc not in [0, 1]. If aptitude is not installed at all, it will return 127 which will silently pass with updates.rc < 0.

@IoTPlay
Copy link
Copy Markdown

IoTPlay commented Nov 5, 2017

Hi, on the debian_upgrade.yml, which passed successfully and upgraded 3 Pi's yesterday, now one Pi is reporting:

`TASK [List packages to upgrade (1/2)] ***************************************************************************************************

fatal: [dabar3]: FAILED! => {"changed": false, "cmd": "aptitude -q -F%p --disable-columns search "~U"", "delta": "0:00:03.330742", "end": "2017-11-05 07:38:08.981687", "failed": true, "msg": "non-zero return code", "rc": 1, "start": "2017-11-05 07:38:05.650945", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}`

That Pi is the only one running Pi Stretch, Any ideas?

@Ljz7
Copy link
Copy Markdown

Ljz7 commented Nov 21, 2017

Thanks for this.

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