Enable/Disable and optionally force a specific version for an SELinux module

    name
        The name of the module to control

    module_state
        Should the module be enabled or disabled?

    version
        Defaults to no preference, set to a specified value if required.
        Currently can only alert if the version is incorrect.

    install
        Setting to True installs module

    source
        Points to module source file, used only when install is True

    remove
        Setting to True removes module

    .. versionadded:: 2016.3.0

def module(name, module_state='Enabled', version='any', **opts):
    '''
    Enable/Disable and optionally force a specific version for an SELinux module

    name
        The name of the module to control

    module_state
        Should the module be enabled or disabled?

    version
        Defaults to no preference, set to a specified value if required.
        Currently can only alert if the version is incorrect.

    install
        Setting to True installs module

    source
        Points to module source file, used only when install is True

    remove
        Setting to True removes module

    .. versionadded:: 2016.3.0
    '''
    ret = {'name': name,
           'result': True,
           'comment': '',
           'changes': {}}
    if opts.get('install', False) and opts.get('remove', False):
        ret['result'] = False
        ret['comment'] = 'Cannot install and remove at the same time'
        return ret
    if opts.get('install', False):
        module_path = opts.get('source', name)
        ret = module_install(module_path)
        if not ret['result']:
            return ret
    elif opts.get('remove', False):
        return module_remove(name)
    modules = __salt__['selinux.list_semod']()
    if name not in modules:
        ret['comment'] = 'Module {0} is not available'.format(name)
        ret['result'] = False
        return ret
    rmodule_state = _refine_module_state(module_state)
    if rmodule_state == 'unknown':
        ret['comment'] = '{0} is not a valid state for the ' \
                         '{1} module.'.format(module_state, module)
        ret['result'] = False
        return ret
    if version != 'any':
        installed_version = modules[name]['Version']
        if not installed_version == version:
            ret['comment'] = 'Module version is {0} and does not match ' \
                             'the desired version of {1} or you are ' \
                             'using semodule >= 2.4'.format(installed_version, version)
            ret['result'] = False
            return ret
    current_module_state = _refine_module_state(modules[name]['Enabled'])
    if rmodule_state == current_module_state:
        ret['comment'] = 'Module {0} is in the desired state'.format(name)
        return ret
    if __opts__['test']:
        ret['result'] = None
        ret['comment'] = 'Module {0} is set to be toggled to {1}'.format(
            name, module_state)
        return ret

    if __salt__['selinux.setsemod'](name, rmodule_state):
        ret['comment'] = 'Module {0} has been set to {1}'.format(name, module_state)
        return ret
    ret['result'] = False
    ret['comment'] = 'Failed to set the Module {0} to {1}'.format(name, module_state)
    return ret