2016-01-31 15:29:10 +01:00
Title: Debian updates with Ansible
2016-01-31 15:19:29 +01:00
Date: 2016-01-31
Category: Ansible Playbook
I've recently bought a [HP Proliant Microserver Gen8 ](http://www8.hp.com/us/en/products/proliant-servers/product-detail.html?oid=5379860 ) to play around with LXC and try new stuff.
From the 4 Debian machines I had to keep up-to-date, I now have 7, so it became quite time-consumming to manually SSH to each of them whenever an update became available.
2016-01-31 15:29:10 +01:00
I ended up looking at [Ansible ](http://www.ansible.com/ ) to speed up the process and, within an hour, I had a working playbook that updates the debian packages, pip packages and git repos installed on all of my servers with a single command.
2016-01-31 15:19:29 +01:00
I figured I'd share the playbook I use to update the Debian packages !
## The playbook
I modified [this gist ](https://gist.github.com/maethor/380676f6b1cec8cc7439 ) to only use apt-get instead of both apt-get and aptitude.
2016-01-31 15:40:33 +01:00
```yaml
2016-06-01 22:59:17 +02:00
- hosts: all
2016-01-31 15:19:29 +01:00
tasks:
2016-06-01 22:59:17 +02:00
2016-01-31 15:19:29 +01:00
- name: update cache
apt: update_cache=yes
2016-06-01 22:59:17 +02:00
2016-01-31 15:19:29 +01:00
- name: list packages to upgrade (1/2)
2016-05-31 20:45:06 +02:00
shell: apt-get upgrade --show-upgraded --assume-no | grep -A1 'The following packages will be upgraded:' | sed '1d;s/ //;s/ /\n/g'
2016-01-31 15:19:29 +01:00
register: updates
changed_when: False
2016-06-01 22:59:17 +02:00
2016-01-31 15:19:29 +01:00
- name: list packages to upgrade (2/2)
debug: msg="{{ updates.stdout_lines | count }} packages to upgrade ({{ updates.stdout_lines | join(', ') }})"
when: (updates.stdout_lines)
2016-06-01 22:59:17 +02:00
2016-01-31 15:19:29 +01:00
- name: upgrade packages
apt: upgrade=dist
2016-01-31 15:29:10 +01:00
when: (updates.stdout_lines)
2016-06-01 22:59:17 +02:00
2016-01-31 15:19:29 +01:00
- name: check what the new version is
shell: lsb_release -r | awk '{print $2}'
changed_when: False
register: new_release
2016-06-01 22:59:17 +02:00
2016-01-31 15:19:29 +01:00
- 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
2016-06-01 22:59:17 +02:00
2016-05-31 20:45:06 +02:00
- name: /wheezy/ install the debian-goodies package if it is missing
2016-01-31 15:19:29 +01:00
apt: name=debian-goodies state=present
2016-05-31 20:45:06 +02:00
when: ansible_distribution_release == 'wheezy'
2016-06-01 22:59:17 +02:00
2016-05-31 20:45:06 +02:00
- name: /jessie/ install the needrestart package if it is missing
apt: name=needrestart state=present default_release=jessie-backports
when: ansible_distribution_release == 'jessie'
2016-06-01 22:59:17 +02:00
- name: /wheezy/ list services to restart (1/3)
shell: checkrestart | awk '/^service/{print $2}'
register: wheezy_services
2016-01-31 15:19:29 +01:00
changed_when: False
2016-05-31 20:45:06 +02:00
when: ansible_distribution_release == 'wheezy'
2016-06-01 22:59:17 +02:00
- name: /jessie/ list services to restart (1/3)
shell: needrestart -bl | awk '/^NEEDRESTART-SVC/{print $2}'
register: jessie_services
2016-05-31 20:45:06 +02:00
changed_when: False
when: ansible_distribution_release != 'wheezy'
2016-06-01 22:59:17 +02:00
- name: merge services list (2/3)
set_fact:
services: "{{ wheezy_services if ansible_distribution_release == 'wheezy' else jessie_services }}"
- name: list services to restart (3/3)
2016-01-31 15:19:29 +01:00
debug: msg="{{ services.stdout_lines | count }} services to restart ({{ services.stdout_lines | join (', ') }})"
when: (services.stdout_lines)
2016-06-01 22:59:17 +02:00
2016-01-31 15:19:29 +01:00
- name: cache cleanup
shell: apt-get autoclean
```
## Conclusion
2016-01-31 15:40:33 +01:00
That's all ! Please leave a comment if you've found this playbook helpful !