Working example to update and reboot is new kernel installed

From MyWiki
Jump to: navigation, search
---
# The kernel is updated first, this registers a variable "task_result" to signify a reboot needed
# then we upgrade all packages, obviously here the kernel will not be upgraded again.
# 16-09-2019 Added check to ensure yum-utils is installed (not really nexessary for Centos 7)
#

- hosts: centos7 
  gather_facts: true
  serial: 1
  become: yes
  max_fail_percentage: 0
  tasks:
    - name: Yum update the kernel
      package:
        name: kernel
        state: latest
      register: task_result 

    - name: Ensure yum-utils is installed
      yum:
        name: yum-utils
        state: present
 

    - name: upgrade all packages
      yum: name=* state=latest
 
 
 
 
    - name: Reboot immediately if there was a new kernel installed.
      shell: "sleep 5 && reboot"
      async: 1
      poll: 0
      when: task_result is changed

    - name: Wait for the reboot to complete if there was a new kernel installed.
      wait_for_connection:
       connect_timeout: 20
       sleep: 5
       delay: 5
       timeout: 300
      when: task_result is changed