From ae5d8e5ebbecbbdb57e44974a865f4193fec39a0 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Sun, 8 Oct 2017 15:21:58 +0200 Subject: [PATCH] modprobe: PEP8 compliancy and doc fixes (#31255) This PR includes: - PEP8 compliancy fixes - Documentation fixes - Make module a bit more consistent with existing practices --- lib/ansible/modules/system/modprobe.py | 106 ++++++++++++------------- test/sanity/pep8/legacy-files.txt | 1 - 2 files changed, 49 insertions(+), 58 deletions(-) diff --git a/lib/ansible/modules/system/modprobe.py b/lib/ansible/modules/system/modprobe.py index 1d8ebff6e5c..0aa3f01ef4c 100644 --- a/lib/ansible/modules/system/modprobe.py +++ b/lib/ansible/modules/system/modprobe.py @@ -1,57 +1,52 @@ #!/usr/bin/python -#coding: utf-8 -*- +# -*- coding: utf-8 -*- -# (c) 2013, David Stygstra +# Copyright: (c) 2013, David Stygstra # 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 __metaclass__ = type - ANSIBLE_METADATA = {'metadata_version': '1.1', 'status': ['preview'], 'supported_by': 'community'} - DOCUMENTATION = ''' --- module: modprobe -short_description: Add or remove kernel modules -requirements: [] +short_description: Load or unload kernel modules version_added: 1.4 author: - - "David Stygstra (@stygstra)" - - "Julien Dauphant" - - "Matt Jeffery" + - David Stygstra (@stygstra) + - Julien Dauphant + - Matt Jeffery description: - - Add or remove kernel modules. + - Load or unload 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. + choices: [ absent, present ] + default: present params: - required: false - default: "" - version_added: "1.6" description: - Modules parameters. + default: '' + version_added: "1.6" ''' EXAMPLES = ''' -# Add the 802.1q module -- modprobe: +- name: Add the 802.1q module + modprobe: name: 8021q state: present -# Add the dummy module -- modprobe: +- name: Add the dummy module + modprobe: name: dummy state: present params: 'numdummies=2' @@ -66,61 +61,58 @@ from ansible.module_utils._text import to_native def main(): module = AnsibleModule( - argument_spec={ - 'name': {'required': True}, - 'state': {'default': 'present', 'choices': ['present', 'absent']}, - 'params': {'default': ''}, - }, + argument_spec=dict( + name=dict(type='str', required=True), + state=dict(type='str', default='present', choices=['absent', 'present']), + params=dict(type='str', default=''), + ), supports_check_mode=True, ) - args = { - 'changed': False, - 'failed': False, - 'name': module.params['name'], - 'state': module.params['state'], - 'params': module.params['params'], - } + + name = module.params['name'] + params = module.params['params'] + state = module.params['state'] + + # FIXME: Adding all parameters as result values is useless + result = dict( + changed=False, + name=name, + params=params, + state=state, + ) # Check if module is present try: modules = open('/proc/modules') present = False - module_name = args['name'].replace('-', '_') + ' ' + module_name = name.replace('-', '_') + ' ' for line in modules: if line.startswith(module_name): present = True break modules.close() except IOError as e: - module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **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) + module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **result) # Add/remove module as needed - if args['state'] == 'present': + if state == 'present': if not present: - command = [module.get_bin_path('modprobe', True), args['name']] - command.extend(shlex.split(args['params'])) - rc, _, err = module.run_command(command) - if rc != 0: - module.fail_json(msg=err, **args) - args['changed'] = True - elif args['state'] == 'absent': + if not module.check_mode: + command = [module.get_bin_path('modprobe', True), name] + command.extend(shlex.split(params)) + rc, out, err = module.run_command(command) + if rc != 0: + module.fail_json(msg=err, rc=rc, stdout=out, stderr=err, **result) + result['changed'] = True + elif state == 'absent': if present: - rc, _, err = module.run_command([module.get_bin_path('modprobe', True), '-r', args['name']]) - if rc != 0: - module.fail_json(msg=err, **args) - args['changed'] = True + if not module.check_mode: + rc, out, err = module.run_command([module.get_bin_path('modprobe', True), '-r', name]) + if rc != 0: + 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__': diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 57fd1506390..69faa3904b0 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -406,7 +406,6 @@ lib/ansible/modules/system/kernel_blacklist.py lib/ansible/modules/system/locale_gen.py lib/ansible/modules/system/lvg.py lib/ansible/modules/system/lvol.py -lib/ansible/modules/system/modprobe.py lib/ansible/modules/system/ohai.py lib/ansible/modules/system/open_iscsi.py lib/ansible/modules/system/openwrt_init.py