Browse Source

Backport modprobe to remove nouveau driver

Matthias Vogelgesang 10 years ago
parent
commit
739fa8a6d1
3 changed files with 107 additions and 0 deletions
  1. 99 0
      library/modprobe
  2. 3 0
      roles/nvidia/handlers/main.yml
  3. 5 0
      roles/nvidia/tasks/main.yml

+ 99 - 0
library/modprobe

@@ -0,0 +1,99 @@
+#!/usr/bin/env python2
+#coding: utf-8 -*-
+
+# This module is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This software is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this software.  If not, see <http://www.gnu.org/licenses/>.
+
+
+DOCUMENTATION = '''
+---
+module: modprobe
+short_description: Add or remove kernel modules
+requirements: []
+version_added: 1.4
+description:
+    - Add or remove kernel modules.
+options:
+    name:
+        required: true
+        description:
+            - Name of kernel module to manage.
+    state:
+        required: false
+        default: "present"
+        choices: [ present, absent ]
+        description:
+            - Whether the module should be present or absent.
+'''
+
+EXAMPLES = '''
+# Add the 802.1q module
+- modprobe: name=8021q state=present
+'''
+
+def main():
+    module = AnsibleModule(
+        argument_spec={
+            'name': {'required': True},
+            'state': {'default': 'present', 'choices': ['present', 'absent']},
+        },
+        supports_check_mode=True,
+    )
+    args = {
+        'changed': False,
+        'failed': False,
+        'name': module.params['name'],
+        'state': module.params['state'],
+    }
+
+    # Check if module is present
+    try:
+        modules = open('/proc/modules')
+        present = False
+        for line in modules:
+            if line.startswith(args['name'] + ' '):
+                present = True
+                break
+        modules.close()
+    except IOError, e:
+        module.fail_json(msg=str(e), **args)
+
+    # Check only; don't modify
+    if module.check_mode:
+        if args['state'] == 'present' and not present:
+            changed = True
+        elif args['state'] == 'absent' and present:
+            changed = True
+        else:
+            changed = False
+        module.exit_json(changed=changed)
+
+    # Add/remove module as needed
+    if args['state'] == 'present':
+        if not present:
+            rc, _, err = module.run_command(['modprobe', args['name']])
+            if rc != 0:
+                module.fail_json(msg=err, **args)
+            args['changed'] = True
+    elif args['state'] == 'absent':
+        if present:
+            rc, _, err = module.run_command(['rmmod', args['name']])
+            if rc != 0:
+                module.fail_json(msg=err, **args)
+            args['changed'] = True
+
+    module.exit_json(**args)
+
+# this is magic, see lib/ansible/module_common.py
+#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
+main()

+ 3 - 0
roles/nvidia/handlers/main.yml

@@ -1,3 +1,6 @@
 ---
 - name: run nvidia-modprobe
   command: nvidia-modprobe
+
+- name: blacklist nouveau
+  command: echo "blacklist nouveau" >> /etc/modprobe.d/50-blacklist.conf

+ 5 - 0
roles/nvidia/tasks/main.yml

@@ -14,6 +14,11 @@
   command: zypper ref repo-nvidia
   when: is_open_suse
 
+- name: unload nouveau
+  modprobe: name=nouveau state=absent
+  notify:
+    - blacklist nouveau
+
 - name: install nvidia drivers
   zypper: name={{ item }}
   notify: