---

- name: install aptitude
  apt: name=aptitude state=installed
  become: true
  become_user: root
  when: ansible_os_family=="Debian"

- name: apt-get upgrade
  apt: upgrade=safe
  sudo: true
  when: ansible_os_family=="Debian"

- name: yum remove
  yum: name=ipa-client-common state=absent
  become: true
  become_user: root
  when: ansible_os_family=="RedHat"

- name: yum upgrade
  yum: name=* state=latest
  become: true
  become_user: root
  when: ansible_os_family=="RedHat"

- name: install kernel-devel
  yum: name=kernel-devel state=latest
  become: true
  become_user: root
  when: ansible_os_family=="RedHat"

- name: get kernel-devel version
  shell: rpm -q kernel-devel | cut -f 3,4 -d "-"
  register: rpm_q_output
  when: ansible_os_family=="RedHat"
  check_mode: no

- name: get kernel version
  shell: uname -r
  register: uname_r_output
  check_mode: no

- name: default dont reboot
  set_fact: 
    reboot_now: false

- name: debug1
  debug: var=rpm_q_output

- name: debug2
  debug: var=uname_r_output

- name: set reboot when kernel has changed
  set_fact: 
    reboot_now: true
  when: not uname_r_output.stdout in rpm_q_output.stdout and ansible_os_family=="RedHat"


- name: debug3
  debug: var=reboot_now

- name: restart machine
  shell: "sleep 5; sudo shutdown -r now"
  async: 2
  poll: 1
  ignore_errors: true
  become: true
  become_user: root
  when: reboot_now

- name: waiting for server to come back
  wait_for_connection: sleep=60 timeout=600 delay=60
  when: reboot_now

