Bläddra i källkod

Add blacklist module to disable nouveau

Matthias Vogelgesang 10 år sedan
förälder
incheckning
782fd29371
4 ändrade filer med 132 tillägg och 4 borttagningar
  1. 127 0
      library/blacklist
  2. 0 2
      roles/nvidia/handlers/main.yml
  3. 3 2
      roles/nvidia/tasks/main.yml
  4. 2 0
      setup.yml

+ 127 - 0
library/blacklist

@@ -0,0 +1,127 @@
+#!/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/>.
+
+import os
+import re
+
+
+DOCUMENTATION = '''
+---
+module: blacklist
+short_description: Blacklist kernel modules
+requirements: []
+version_added: 1.4
+description:
+    - Add or remove kernel modules from blacklist.
+options:
+    name:
+        required: true
+        description:
+            - Name of kernel module to black- or whitelist.
+    state:
+        required: false
+        default: "present"
+        choices: [ present, absent ]
+        description:
+            - Whether the module should be present in the blacklist or absent.
+'''
+
+EXAMPLES = '''
+# Blacklist the nouveau driver module
+- blacklist: name=nouveau state=present
+'''
+
+
+class Blacklist(object):
+    def __init__(self, module, filename='/etc/modprobe.d/blacklist-ansible.conf'):
+        if not os.path.exists(filename):
+            open(filename, 'a').close()
+
+        self.filename = filename
+        self.module = module
+
+    def get_pattern(self):
+        return '^blacklist\s*' + self.module + '$'
+
+    def readlines(self):
+        f = open(self.filename, 'r')
+        lines = f.readlines()
+        f.close()
+        return lines
+
+    def module_listed(self):
+        lines = self.readlines()
+        pattern = self.get_pattern()
+
+        for line in lines:
+            stripped = line.strip()
+            if stripped.startswith('#'):
+                continue
+
+            if re.match(pattern, stripped):
+                return True
+
+        return False
+
+    def remove_module(self):
+        lines = self.readlines()
+        pattern = self.get_pattern()
+
+        f = open(self.filename, 'w')
+
+        for line in lines:
+            if not re.match(pattern, line.strip()):
+                f.write(line)
+
+        f.close()
+
+    def add_module(self):
+        f = open(self.filename, 'a')
+        f.write('blacklist %s' % self.module)
+
+
+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'],
+    }
+
+    blacklist = Blacklist(args['name'])
+
+    if blacklist.module_listed():
+        if args['state'] == 'absent':
+            blacklist.add_module()
+            args['changed'] = True
+    else:
+        if args['state'] == 'present':
+            blacklist.remove_module()
+            args['changed'] = True
+
+    module.exit_json(**args)
+
+# this is magic, see lib/ansible/module_common.py
+#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
+main()

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

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

+ 3 - 2
roles/nvidia/tasks/main.yml

@@ -16,8 +16,9 @@
 
 - name: unload nouveau
   modprobe: name=nouveau state=absent
-  notify:
-    - blacklist nouveau
+
+- name: blacklist nouveau
+  blacklist: name=nouveau state=present
 
 - name: install nvidia drivers
   zypper: name={{ item }}

+ 2 - 0
setup.yml

@@ -41,3 +41,5 @@
     - vars.yml
   roles:
     - nvidia
+  tags:
+    - nvidia