---

- name: "Force this role to fail if no Mellanox hardware is present"
  #Exclude the role via tags ans ansible-playbook --skip-tags mlx
  shell: "lspci | grep Mellanox"
  check_mode: yes

#- 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
  become: true
  become_user: root
  when: ansible_os_family == "RedHat"

- name: test for existing installation of drivers
  stat:
    path: /bin/ibv_devinfo
  become: true
  register: drivers_installed

- name: debug - print out installed driver
  debug: var=drivers_installed

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

- name: get driver version
  shell: '/bin/ofed_info -l | head -n 1 | cut -f 1 -d " "'
  register: driver_version
  check_mode: no
  changed_when: False

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

- name: set install due to drivers not installed
  set_fact:
    install_now: true
    reboot_now: true
  when: not drivers_installed.stat.exists

- 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: debug - print out value of install_now
  debug: var=install_now

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

#remove old mellanox drivers as they may interfere with an update
- name: stop lustre
  service: name=lustre-client state=stopped
  become: true
  become_user: root
  when: install_now and drivers_installed.stat.exists

- name: remove ipa stuff
  package:
    state: absent
    name:
      - ipa-common
      - ipa-python-compat
      - python2-ipalib
  become: true
  when: install_now

- name: remove unnecessary packages that break stuff
  package:
    state: absent
    name:
      - libgpod
      - usbmuxd
      - libmlx5
      - libmlx4
      - libibverbs
  become: true
  when: install_now

- name: remove mellanox rpms
  yum:
    state: absent
    name:
      - mlnx-ofa_kernel
      - mlnx-ofa_kernel-devel
      - mlnx-ofa_kernel-modules
  become: true
  when: install_now

- name: install ofa_kernel 
  yum:
    name: kmod-mlnx-ofa_kernel-{{ MELLANOX_VERSION }}
    state: present
    allow_downgrade: yes
  become: true

- name: install drivers
  shell: ./mlnxofedinstall -q --add-kernel-support --force --skip-repo
  args:
    chdir: "/tmp/{{ MELLANOX_DRIVER_SRC }}"
  become: true
  become_user: root
  when: install_now

- name: Copy roce_mode.service file
  copy: dest=/etc/systemd/system src=files/roce_mode.service owner=root group=root mode=0644
  become: true
  become_user: root
  register: service_file

- name: Reload systemd
  shell: systemctl daemon-reload
  become: true
  become_user: root
  when: service_file.changed
  register: reload_service

- name: enable roce_mode setting
  service: name=roce_mode enabled=yes
  become: true
#
# A REBOOT IS NEEDED AFTER SUCCESSFUL INSTALL
#
- 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

- name: enable roce_mode setting
  service: name=roce_mode state=started enabled=yes
  become: true