For use with eg CentOS.
Assumes you have a file /etc/ansible/hosts with groups and members already configured.
A properly formatted .yml-file can be found at the end of the article.
What this playbook does:
- Excludes a member foo.uu.se from the group centos-cli.
- Doesn't gather facts to speed things up.
- Updates all packages to their latest version.
- Compares the running kernel version with the latest installed as a hint if a reboot is needed to activate the latest installed kernel version.
- Reboots.
- Waits till the connection is back.
yum.update.with.reboot.as.needed.yml
---
- hosts: centos-cli:!foo.uu.se
gather_facts: no
tasks:
- name: upgrade all packages
yum: name='*' state=latest
- name: check for reboot hint
shell: if [ $(rpm -q --last kernel | awk 'NR==1 {print $1}') != kernel-$(uname -r) ]; then echo 'reboot'; else echo 'Kernel is current'; fi
ignore_errors: true
register: reboot_hint
- name: Rebooting ...
command: shutdown -r now "Reboot required for updated kernel"
async: 0
poll: 0
ignore_errors: true
changed_when: "reboot_hint.stdout == 'reboot'"
register: rebooting
- name: Wait for computer to reboot...
pause: seconds=60
when: rebooting|changed
...