From 27a08dc647a47c36fd140def0c3f5a8e368c8c85 Mon Sep 17 00:00:00 2001 From: David Stygstra Date: Wed, 18 Sep 2013 19:34:32 -0400 Subject: [PATCH 1/2] Module to manage kernel modules --- library/system/modprobe | 93 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100755 library/system/modprobe diff --git a/library/system/modprobe b/library/system/modprobe new file mode 100755 index 0000000000..017b6eaa47 --- /dev/null +++ b/library/system/modprobe @@ -0,0 +1,93 @@ +#!/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 . + + +DOCUMENTATION = ''' +--- +module: modprobe +short_description: Add or remove kernel modules +requirements: [] +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: + with open('/proc/modules') as modules: + present = any(module.startswith(args['name'] + ' ') for module in modules) + except IOError as 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 +#<> +main() From 58620ca4771a5ff214bd0215323c595525452a39 Mon Sep 17 00:00:00 2001 From: David Stygstra Date: Thu, 19 Sep 2013 00:04:20 -0400 Subject: [PATCH 2/2] Add Python 2.4 support to modprobe module --- library/system/modprobe | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/library/system/modprobe b/library/system/modprobe index 017b6eaa47..90537c2f79 100755 --- a/library/system/modprobe +++ b/library/system/modprobe @@ -57,9 +57,14 @@ def main(): # Check if module is present try: - with open('/proc/modules') as modules: - present = any(module.startswith(args['name'] + ' ') for module in modules) - except IOError as e: + 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