---
- name: install deps 
  yum: name={{ item }} state=installed
  sudo: true
  with_items:
    - gcc
    - perl
    - wget
    - pciutils
    - kernel-headers
    - kernel-devel 
    - xterm
    - libX11-common
    - libX11-devel
    - libX11
    - xorg-x11-server-common
    - xorg-x11-util-macros
    - xorg-x11-server-utils
    - xorg-x11-font-utils
    - xorg-x11-server-Xorg
    - xorg-x11-glamor
    - xorg-x11-xinit
    - xorg-x11-utils
    - xorg-x11-xauth
    - xorg-x11-proto-devel
    - xorg-x11-xkb-utils

- name: install development tools
  yum: name="@Development Tools" state=installed
  become: true
  become_user: root

- name: disable nouveau
  template: src=blacklist-nouveau.conf.j2 dest=/etc/modprobe.d/blacklist-nouveau.conf
  become: true
  become_user: root

- name: template unit for for persistenced
  template: src=nvidia-persistenced.service dest=/etc/systemd/system/nvidia-persistenced.service
  become: true
  become_user: root

- name: create the nvidia-persistenced user
  user: name=nvidia-persistenced state=present system=yes shell=/bin/false
  become: true
  become_user: root


- name: remove nouveau
  modprobe: name=nouveau state=absent
  become: true 
  become_user: root

- name: get kernel version
  shell: uname -r
  register: kernel_version


- name: check nvidia driver
  stat: path="/lib/modules/{{ kernel_version.stdout }}/kernel/drivers/video/nvidia.ko"
  register: nvidia_driver
  ignore_errors: true

- name: get nvidia driver 
  get_url: url=http://consistency0/src/NVIDIA-Linux-x86_64-{{ nvidia_version }}.run dest=/tmp/NVIDIA-Linux-x86_64-{{ nvidia_version }}.run
  become: true
  become_user: root
  when: not nvidia_driver.stat.exists

#- name: Copy boot file
#  template: src=grub.conf.j2 dest=/boot/grub/grub.conf 
#  sudo: true
#
#- name: Copy X config file
#  template: src=xorg.conf.j2 dest=/etc/X11/xorg.conf 
#  sudo: true

- name: Copy xserver file
  template: src=xserver.j2 dest=/etc/pam.d/xserver
  sudo: true

- name: build nvidia driver 
  shell: chmod 755 /tmp/NVIDIA-Linux-x86_64-{{ nvidia_version }}.run; /tmp/NVIDIA-Linux-x86_64-{{ nvidia_version }}.run --silent
  sudo: true
  when: not nvidia_driver.stat.exists

- name: set the GOM
  shell: nvidia-smi --gom=0
  become: true
  become_user: root

- name: enable persistenced on boot
  service: name=nvidia-persistenced state=started enabled=yes
  become: true
  become_user: root

- name: Configure xorg.conf with nvidia-xconfig so xorg.conf matches gpu number
  shell: /usr/bin/nvidia-xconfig -a --use-display-device=none
  become: true
  become_user: root
  args: 
    creates: /etc/X11/xorg.conf
