Yes you can try something like this:
---
- name: Reboot Server Playbook
hosts: all
user: ambot
sudo: True
tasks:
- name: Do an upgrade
command: yum upgrade -y
- name: Check what the new version is
shell: lsb_release -r | awk '{print $2}'
register: new_release
- name: Reboot
command: /sbin/reboot
when: ansible_distribution_version != new_release.stdout
The above will reboot the server if the CentOS release has changed. It's also using the ansible_distribution_version
from the ansible facts variables populated initially the first time your playbook runs.
Add -y in yum upgrade command, so process is not stuck waiting for confirmation in stdin from ansible.
To see the ansible facts on your localhost you can run something like:
ansible localhost -m setup
[Possible solution to regenerate facts in a playbook]
# You can try this to store the initial version
vars:
current_os_version: $ansible_distribution_version
tasks:
- name: Regenerate facts ?
setup: filter=*