---
- hosts: ubuntu
become: true
any_errors_fatal: true
serial: 1
max_fail_percentage: 0
vars:
ansible_user: ubuntu
tasks:
# do an "apt-get update", to ensure latest package lists
- name: apt-get update
apt:
update-cache: yes
changed_when: 0
# get a list of packages that have updates
- name: get list of pending upgrades
command: apt-get --simulate dist-upgrade
args:
warn: false # don't warn us about apt having its own plugin
register: apt_simulate
changed_when: 0
- name: Update cache
apt:
update-cache: yes
changed_when: false
- name: Fetch package list of updates
command: apt list --upgradable
register: aptlist
- set_fact:
updates: "{{ aptlist.stdout_lines | difference(['Listing...'])
| map('regex_replace', '^(.*?)/(.*)', '\\1') | list }}"
- debug: var=updates
# tell user about packages being updated
- name: show pending updates
debug:
var: updates
when: updates.0 is defined
# running package ack each server update with a prompt
- pause:
when: updates.0 is defined
# if a new kernel is incoming, remove old ones to avoid full /boot
- name: apt-get autoremove
command: apt-get -y autoremove
args:
warn: false
when: '"Inst linux-image-" in apt_simulate.stdout'
changed_when: 0
# do the actual apt-get dist-upgrade
- name: apt-get dist-upgrade
apt:
upgrade: dist # upgrade all packages to latest version
register: upgrade_output
# check if we need a reboot
- name: check if reboot needed
stat: path=/var/run/reboot-required
register: file_reboot_required
# "meta: end_play" aborts the rest of the tasks in the current «tasks:»
# section, for the current desired server
- meta: end_play
when: not file_reboot_required.stat.exists
# because of the above meta/when we at this point know that the current
# host needs a reboot
# prompt for manual input before doing the actual reboot
- name: Confirm reboot of ubuntu
pause:
- name: reboot node
shell: sleep 2 && shutdown -r now "Reboot triggered by ansible"
async: 1
poll: 0
ignore_errors: true
# poll ssh port until we get a tcp connect
- name: wait for node to finish booting
become: false
local_action: wait_for host=ubuntu
port=22
state=started
delay=5
timeout=600
# give sshd time to start fully
- name: wait for ssh to start fully
pause:
seconds: 15
# wait a few minutes between hosts, unless we're on the last
- name: waiting between hosts
pause:
minutes: 10
when: inventory_hostname != ansible_play_hosts[-1]
Output
PLAY [ubuntu] ************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [10.0.2.236]
TASK [apt-get update] ****************************************************************************************************************************************
ok: [10.0.2.236]
TASK [get list of pending upgrades] **************************************************************************************************************************
ok: [10.0.2.236]
TASK [Update cache] ******************************************************************************************************************************************
ok: [10.0.2.236]
TASK [Fetch package list of updates] *************************************************************************************************************************
changed: [10.0.2.236]
TASK [set_fact] **********************************************************************************************************************************************
ok: [10.0.2.236]
TASK [debug] *************************************************************************************************************************************************
ok: [10.0.2.236] => {
"updates": []
}
TASK [show pending updates] **********************************************************************************************************************************
skipping: [10.0.2.236]
TASK [pause] *************************************************************************************************************************************************
skipping: [10.0.2.236]
TASK [apt-get autoremove] ************************************************************************************************************************************
skipping: [10.0.2.236]
TASK [apt-get dist-upgrade] **********************************************************************************************************************************
ok: [10.0.2.236]
TASK [check if reboot needed] ********************************************************************************************************************************
ok: [10.0.2.236]
PLAY RECAP ***************************************************************************************************************************************************
10.0.2.236 : ok=9 changed=1 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0