modprobe: PEP8 compliancy and doc fixes (#31255)

This PR includes:
- PEP8 compliancy fixes
- Documentation fixes
- Make module a bit more consistent with existing practices
pull/31453/head
Dag Wieers 7 years ago committed by ansibot
parent 4804e99b44
commit ae5d8e5ebb

@ -1,57 +1,52 @@
#!/usr/bin/python #!/usr/bin/python
#coding: utf-8 -*- # -*- coding: utf-8 -*-
# (c) 2013, David Stygstra <david.stygstra@gmail.com> # Copyright: (c) 2013, David Stygstra <david.stygstra@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1', ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: modprobe module: modprobe
short_description: Add or remove kernel modules short_description: Load or unload kernel modules
requirements: []
version_added: 1.4 version_added: 1.4
author: author:
- "David Stygstra (@stygstra)" - David Stygstra (@stygstra)
- "Julien Dauphant" - Julien Dauphant
- "Matt Jeffery" - Matt Jeffery
description: description:
- Add or remove kernel modules. - Load or unload kernel modules.
options: options:
name: name:
required: true required: true
description: description:
- Name of kernel module to manage. - Name of kernel module to manage.
state: state:
required: false
default: "present"
choices: [ present, absent ]
description: description:
- Whether the module should be present or absent. - Whether the module should be present or absent.
choices: [ absent, present ]
default: present
params: params:
required: false
default: ""
version_added: "1.6"
description: description:
- Modules parameters. - Modules parameters.
default: ''
version_added: "1.6"
''' '''
EXAMPLES = ''' EXAMPLES = '''
# Add the 802.1q module - name: Add the 802.1q module
- modprobe: modprobe:
name: 8021q name: 8021q
state: present state: present
# Add the dummy module - name: Add the dummy module
- modprobe: modprobe:
name: dummy name: dummy
state: present state: present
params: 'numdummies=2' params: 'numdummies=2'
@ -66,61 +61,58 @@ from ansible.module_utils._text import to_native
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec={ argument_spec=dict(
'name': {'required': True}, name=dict(type='str', required=True),
'state': {'default': 'present', 'choices': ['present', 'absent']}, state=dict(type='str', default='present', choices=['absent', 'present']),
'params': {'default': ''}, params=dict(type='str', default=''),
}, ),
supports_check_mode=True, supports_check_mode=True,
) )
args = {
'changed': False, name = module.params['name']
'failed': False, params = module.params['params']
'name': module.params['name'], state = module.params['state']
'state': module.params['state'],
'params': module.params['params'], # FIXME: Adding all parameters as result values is useless
} result = dict(
changed=False,
name=name,
params=params,
state=state,
)
# Check if module is present # Check if module is present
try: try:
modules = open('/proc/modules') modules = open('/proc/modules')
present = False present = False
module_name = args['name'].replace('-', '_') + ' ' module_name = name.replace('-', '_') + ' '
for line in modules: for line in modules:
if line.startswith(module_name): if line.startswith(module_name):
present = True present = True
break break
modules.close() modules.close()
except IOError as e: except IOError as e:
module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **args) module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **result)
# 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 # Add/remove module as needed
if args['state'] == 'present': if state == 'present':
if not present: if not present:
command = [module.get_bin_path('modprobe', True), args['name']] if not module.check_mode:
command.extend(shlex.split(args['params'])) command = [module.get_bin_path('modprobe', True), name]
rc, _, err = module.run_command(command) command.extend(shlex.split(params))
if rc != 0: rc, out, err = module.run_command(command)
module.fail_json(msg=err, **args) if rc != 0:
args['changed'] = True module.fail_json(msg=err, rc=rc, stdout=out, stderr=err, **result)
elif args['state'] == 'absent': result['changed'] = True
elif state == 'absent':
if present: if present:
rc, _, err = module.run_command([module.get_bin_path('modprobe', True), '-r', args['name']]) if not module.check_mode:
if rc != 0: rc, out, err = module.run_command([module.get_bin_path('modprobe', True), '-r', name])
module.fail_json(msg=err, **args) if rc != 0:
args['changed'] = True module.fail_json(msg=err, rc=rc, stdout=out, stderr=err, **result)
result['changed'] = True
module.exit_json(**args) module.exit_json(**result)
if __name__ == '__main__': if __name__ == '__main__':

@ -406,7 +406,6 @@ lib/ansible/modules/system/kernel_blacklist.py
lib/ansible/modules/system/locale_gen.py lib/ansible/modules/system/locale_gen.py
lib/ansible/modules/system/lvg.py lib/ansible/modules/system/lvg.py
lib/ansible/modules/system/lvol.py lib/ansible/modules/system/lvol.py
lib/ansible/modules/system/modprobe.py
lib/ansible/modules/system/ohai.py lib/ansible/modules/system/ohai.py
lib/ansible/modules/system/open_iscsi.py lib/ansible/modules/system/open_iscsi.py
lib/ansible/modules/system/openwrt_init.py lib/ansible/modules/system/openwrt_init.py

Loading…
Cancel
Save