debian_upgrade.yml

Ansible playbook to update and upgrade Debian hosts.

1

Votes

---

- 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)

Vote Here

You must earn at least 1 vote on your snippets to be allowed to vote

Terms Of Use

Privacy Policy

Featured snippets are MIT license

Gears & Masters

Advertise

DevOpsnipp.com © 2020

medium.png