---
- include_vars: mellanoxVars.yml

- name: yum install dependencies
  yum:  name=perl,pciutils,gtk2,atk,cairo,gcc-gfortran,libxml2-python,tcsh,libnl,lsof,tcl,tk,kernel-devel,python-devel,createrepo,rpm-build
  sudo: true
  ignore_errors: true
  when: ansible_os_family == "RedHat"
  
- name: test for existing installation of drivers
  command: ibv_devinfo
  sudo: true
  register: drivers_installed
  ignore_errors: true
  check_mode: no

- name: default dont install
  set_fact: 
    install_now: false 
    reboot_now: false

- name: get driver version
  command: 'ofed_info -l | head -n 1 | cut -f 1 -d " "'
  register: driver_version
  ignore_errors: true
  check_mode: no

- name: get desired driver version
  shell: 'echo {{ MELLANOX_DRIVER_SRC }} | cut -f 1,2,3 -d "-"'
  register: desired_driver_version
  check_mode: no


- name: set install due to drivers not installed
  set_fact: 
    install_now: true 
    reboot_now: true
  when: drivers_installed | failed

- name: debug
  debug: var=driver_version

- name: debug
  debug: var=desired_driver_version

- name: set install due to version mismatch
  set_fact: 
    install_now: true 
    reboot_now: true
  when: driver_version | failed or not desired_driver_version.stdout in driver_version.stdout

- name: copy driver source
  unarchive: copy=no src="http://consistency0/src/{{ MELLANOX_DRIVER_SRC }}.tgz" dest=/tmp 
  sudo: true
  when: install_now

- name: install drivers
  shell: ./mlnxofedinstall -q --add-kernel-support --force
  args:
    #more changes
    chdir: "/tmp/{{ MELLANOX_DRIVER_SRC }}"
  sudo: true
  when: install_now



#
# A REBOOT IS NEEDED AFTER SUCCESSFUL INSTALL
#
- name: restart machine
  shell: "sleep 5; sudo shutdown -r now"
  async: 2
  poll: 1
  ignore_errors: true
  sudo: true
  when: reboot_now

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