---
- name: get kernel version
  shell: uname -r
  register: uname_r_output
  check_mode: no
  changed_when: False
  
- name: remove conflicting packages if kernel is going to changed
  block:
    - name: see if we have a lustre-client service
      shell: systemctl status lustre-client
      register: lustreStatus
      changed_when: False
      failed_when: '"could not be found" in lustreStatus.stdout'

    - name: stop lustre
      service: name=lustre-client state=stopped
      become: true
      when: lustreStatus

    - name: remove conflicting packages if kernel is going to changed
      package:
        state: absent
        name:
          - kmod-lustre-client
          - kernel-devel
      become: true
    - name: remove mellanox rpms
      yum:
        state: absent
        name:
          - mlnx-ofa_kernel
          - mlnx-ofa_kernel-devel
          - mlnx-ofa_kernel-modules
          - libibverbs
          - libgpod
          - usbmuxd
          - libmlx5
          - libmlx4
      become: true
  when: uname_r_output.stdout != KERNEL_VERSION

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

#- name: apt-get upgrade
#  apt: upgrade=safe
#  become: 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
    update_cache: yes
    exclude: kernel*,mlnx-ofa_kernel*,kmod-lustre-client*,kmod-mlnx-ofa_kernel
    disablerepo: "{{ yumdisablerepo|join(',') }}"
    enablerepo: "{{ yumenablerepo|join(',') }}"
  become: true
  become_user: root
  when: ansible_os_family=="RedHat" and yumdisablerepo is defined

- name: yum upgrade
  yum:
    name: '*'
    state: latest
    update_cache: yes
    exclude: kernel*,mlnx-ofa_kernel*,kmod-lustre-client*,kmod-mlnx-ofa_kernel
  become: true
  become_user: root
  when: ansible_os_family=="RedHat" and yumdisablerepo is undefined

- name: Clear yum pending transaction
  command: yum-complete-transaction --cleanup-only
  become: true
  become_user: root
  when: ansible_os_family=="RedHat"
  register: yumtransactioncleanup
  changed_when: '"No unfinished transactions left." not in yumtransactioncleanup.stdout'

- name: install kernel-devel
  yum:
    name:
      - kernel-devel-{{ KERNEL_VERSION }}
      - kernel-{{ KERNEL_VERSION }}
      - kernel-headers-{{ KERNEL_VERSION }}
      - kernel-tools-{{ KERNEL_VERSION }}
      - kernel-tools-libs-{{ KERNEL_VERSION }}
    state: present
  become: true
  when: ansible_os_family=="RedHat"


- name: get kernel-devel version
  shell: rpm -q kernel-devel | cut -f 3,4 -d "-" | sort | tail -n 1
  register: rpm_q_output
  when: ansible_os_family=="RedHat"
  check_mode: no
  changed_when: False
  args:
    warn: False

- name: get kernel-devel version
  shell: dpkg -l linux-image* | grep "^ii" | grep "linux-image-[0-9]" | sed 's/\ \ */ /g' | cut -f 2 -d " " | cut -f 3-5 -d "-"
  register: dpkg_l_output
  when: ansible_os_family=="Debian"
  check_mode: no
  changed_when: False

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

- 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: ansible_os_family=="RedHat" and not uname_r_output.stdout in rpm_q_output.stdout

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

- name: debug3
  debug: var=reboot_now

- name: restart machine
#  shell: "sleep 5; sudo shutdown -r now"
#  async: 2
#  poll: 1
#  ignore_errors: true
  reboot:
  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

