mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 19:01:26 -07:00
* Initial Commit
* Adding changelog fragment
* Ensured params are present during verbose output and enhanced check_mode
* Making specific to builtins
* Removing unneccessary external call
* Acutal bugfix
(cherry picked from commit d180390dbc
)
Co-authored-by: Ajpantuso <ajpantuso@gmail.com>
This commit is contained in:
parent
48b1bc7d47
commit
0760f60ca5
3 changed files with 263 additions and 53 deletions
|
@ -50,11 +50,90 @@ EXAMPLES = '''
|
|||
'''
|
||||
|
||||
import os.path
|
||||
import platform
|
||||
import shlex
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils._text import to_native
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
|
||||
RELEASE_VER = platform.release()
|
||||
|
||||
|
||||
class Modprobe(object):
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
self.modprobe_bin = module.get_bin_path('modprobe', True)
|
||||
|
||||
self.check_mode = module.check_mode
|
||||
self.desired_state = module.params['state']
|
||||
self.name = module.params['name']
|
||||
self.params = module.params['params']
|
||||
|
||||
self.changed = False
|
||||
|
||||
def load_module(self):
|
||||
command = [self.modprobe_bin]
|
||||
if self.check_mode:
|
||||
command.append('-n')
|
||||
command.extend([self.name] + shlex.split(self.params))
|
||||
|
||||
rc, out, err = self.module.run_command(command)
|
||||
|
||||
if rc != 0:
|
||||
return self.module.fail_json(msg=err, rc=rc, stdout=out, stderr=err, **self.result)
|
||||
|
||||
if self.check_mode or self.module_loaded():
|
||||
self.changed = True
|
||||
else:
|
||||
rc, stdout, stderr = self.module.run_command(
|
||||
[self.modprobe_bin, '-n', '--first-time', self.name] + shlex.split(self.params)
|
||||
)
|
||||
if rc != 0:
|
||||
self.module.warn(stderr)
|
||||
|
||||
def module_loaded(self):
|
||||
is_loaded = False
|
||||
try:
|
||||
with open('/proc/modules') as modules:
|
||||
module_name = self.name.replace('-', '_') + ' '
|
||||
for line in modules:
|
||||
if line.startswith(module_name):
|
||||
is_loaded = True
|
||||
break
|
||||
|
||||
if not is_loaded:
|
||||
module_file = '/' + self.name + '.ko'
|
||||
builtin_path = os.path.join('/lib/modules/', RELEASE_VER, 'modules.builtin')
|
||||
with open(builtin_path) as builtins:
|
||||
for line in builtins:
|
||||
if line.rstrip().endswith(module_file):
|
||||
is_loaded = True
|
||||
break
|
||||
except (IOError, OSError) as e:
|
||||
self.module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **self.result)
|
||||
|
||||
return is_loaded
|
||||
|
||||
def unload_module(self):
|
||||
command = [self.modprobe_bin, '-r', self.name]
|
||||
if self.check_mode:
|
||||
command.append('-n')
|
||||
|
||||
rc, out, err = self.module.run_command(command)
|
||||
if rc != 0:
|
||||
return self.module.fail_json(msg=err, rc=rc, stdout=out, stderr=err, **self.result)
|
||||
|
||||
self.changed = True
|
||||
|
||||
@property
|
||||
def result(self):
|
||||
return {
|
||||
'changed': self.changed,
|
||||
'name': self.name,
|
||||
'params': self.params,
|
||||
'state': self.desired_state,
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -67,60 +146,14 @@ def main():
|
|||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
name = module.params['name']
|
||||
params = module.params['params']
|
||||
state = module.params['state']
|
||||
modprobe = Modprobe(module)
|
||||
|
||||
# FIXME: Adding all parameters as result values is useless
|
||||
result = dict(
|
||||
changed=False,
|
||||
name=name,
|
||||
params=params,
|
||||
state=state,
|
||||
)
|
||||
if modprobe.desired_state == 'present' and not modprobe.module_loaded():
|
||||
modprobe.load_module()
|
||||
elif modprobe.desired_state == 'absent' and modprobe.module_loaded():
|
||||
modprobe.unload_module()
|
||||
|
||||
# Check if module is present
|
||||
try:
|
||||
present = False
|
||||
with open('/proc/modules') as modules:
|
||||
module_name = name.replace('-', '_') + ' '
|
||||
for line in modules:
|
||||
if line.startswith(module_name):
|
||||
present = True
|
||||
break
|
||||
if not present:
|
||||
command = [module.get_bin_path('uname', True), '-r']
|
||||
rc, uname_kernel_release, err = module.run_command(command)
|
||||
module_file = '/' + name + '.ko'
|
||||
builtin_path = os.path.join('/lib/modules/', uname_kernel_release.strip(),
|
||||
'modules.builtin')
|
||||
with open(builtin_path) as builtins:
|
||||
for line in builtins:
|
||||
if line.endswith(module_file):
|
||||
present = True
|
||||
break
|
||||
except IOError as e:
|
||||
module.fail_json(msg=to_native(e), exception=traceback.format_exc(), **result)
|
||||
|
||||
# Add/remove module as needed
|
||||
if state == 'present':
|
||||
if not present:
|
||||
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:
|
||||
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(**result)
|
||||
module.exit_json(**modprobe.result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue