2014-01-15 15:52:17 +01:00
|
|
|
#!/usr/bin/python
|
2013-09-19 01:34:32 +02:00
|
|
|
#coding: utf-8 -*-
|
|
|
|
|
2014-06-20 02:02:02 +02:00
|
|
|
# (c) 2013, David Stygstra <david.stygstra@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
2013-09-19 01:34:32 +02:00
|
|
|
# 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: []
|
2013-09-23 22:02:02 +02:00
|
|
|
version_added: 1.4
|
2015-05-14 16:45:21 +02:00
|
|
|
author:
|
2015-06-16 20:32:39 +02:00
|
|
|
- "David Stygstra (@stygstra)"
|
|
|
|
- "Julien Dauphant"
|
|
|
|
- "Matt Jeffery"
|
2013-09-19 01:34:32 +02:00
|
|
|
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.
|
2014-03-05 16:42:52 +01:00
|
|
|
params:
|
|
|
|
required: false
|
|
|
|
default: ""
|
2014-03-05 16:45:20 +01:00
|
|
|
version_added: "1.6"
|
2014-03-05 16:42:52 +01:00
|
|
|
description:
|
|
|
|
- Modules parameters.
|
2013-09-19 01:34:32 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Add the 802.1q module
|
|
|
|
- modprobe: name=8021q state=present
|
2014-03-05 16:42:52 +01:00
|
|
|
# Add the dummy module
|
|
|
|
- modprobe: name=dummy state=present params="numdummies=2"
|
2013-09-19 01:34:32 +02:00
|
|
|
'''
|
|
|
|
|
2016-06-03 15:23:55 +02:00
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
from ansible.module_utils.pycompat24 import get_exception
|
2015-10-10 22:06:10 +02:00
|
|
|
import shlex
|
|
|
|
|
|
|
|
|
2013-09-19 01:34:32 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec={
|
|
|
|
'name': {'required': True},
|
|
|
|
'state': {'default': 'present', 'choices': ['present', 'absent']},
|
2014-03-05 16:42:52 +01:00
|
|
|
'params': {'default': ''},
|
2013-09-19 01:34:32 +02:00
|
|
|
},
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
args = {
|
|
|
|
'changed': False,
|
|
|
|
'failed': False,
|
|
|
|
'name': module.params['name'],
|
|
|
|
'state': module.params['state'],
|
2014-03-05 16:42:52 +01:00
|
|
|
'params': module.params['params'],
|
2013-09-19 01:34:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Check if module is present
|
|
|
|
try:
|
2013-09-19 06:04:20 +02:00
|
|
|
modules = open('/proc/modules')
|
|
|
|
present = False
|
2014-02-20 17:50:09 +01:00
|
|
|
module_name = args['name'].replace('-', '_') + ' '
|
2013-09-19 06:04:20 +02:00
|
|
|
for line in modules:
|
2014-02-20 17:50:09 +01:00
|
|
|
if line.startswith(module_name):
|
2013-09-19 06:04:20 +02:00
|
|
|
present = True
|
|
|
|
break
|
|
|
|
modules.close()
|
2016-06-03 15:23:55 +02:00
|
|
|
except IOError:
|
|
|
|
e = get_exception()
|
2013-09-19 01:34:32 +02:00
|
|
|
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:
|
2015-10-10 22:06:10 +02:00
|
|
|
command = [module.get_bin_path('modprobe', True), args['name']]
|
|
|
|
command.extend(shlex.split(args['params']))
|
|
|
|
rc, _, err = module.run_command(command)
|
2013-09-19 01:34:32 +02:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err, **args)
|
|
|
|
args['changed'] = True
|
|
|
|
elif args['state'] == 'absent':
|
|
|
|
if present:
|
2016-09-14 14:32:08 +02:00
|
|
|
rc, _, err = module.run_command([module.get_bin_path('modprobe', True), '-r', args['name']])
|
2013-09-19 01:34:32 +02:00
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(msg=err, **args)
|
|
|
|
args['changed'] = True
|
|
|
|
|
|
|
|
module.exit_json(**args)
|
|
|
|
|
|
|
|
main()
|