diff --git a/changelogs/fragments/3284-openwrt_init-improvements.yaml b/changelogs/fragments/3284-openwrt_init-improvements.yaml new file mode 100644 index 0000000000..99a60dfce8 --- /dev/null +++ b/changelogs/fragments/3284-openwrt_init-improvements.yaml @@ -0,0 +1,4 @@ +minor_changes: + - openwrt_init - minor refactoring (https://github.com/ansible-collections/community.general/pull/3284). +bugfixes: + - openwrt_init - calling ``run_command`` with arguments as ``list`` instead of ``str`` (https://github.com/ansible-collections/community.general/pull/3284). diff --git a/plugins/modules/system/openwrt_init.py b/plugins/modules/system/openwrt_init.py index afc3c3a956..fa9488ecb2 100644 --- a/plugins/modules/system/openwrt_init.py +++ b/plugins/modules/system/openwrt_init.py @@ -70,9 +70,7 @@ RETURN = ''' ''' import os -import glob from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.common.text.converters import to_bytes, to_native module = None init_script = None @@ -81,15 +79,12 @@ init_script = None # =============================== # Check if service is enabled def is_enabled(): - (rc, out, err) = module.run_command("%s enabled" % init_script) - if rc == 0: - return True - return False + rc, dummy, dummy = module.run_command([init_script, 'enabled']) + return rc == 0 # =========================================== # Main control flow - def main(): global module, init_script # init @@ -98,22 +93,19 @@ def main(): name=dict(required=True, type='str', aliases=['service']), state=dict(type='str', choices=['started', 'stopped', 'restarted', 'reloaded']), enabled=dict(type='bool'), - pattern=dict(type='str', required=False, default=None), + pattern=dict(type='str'), ), supports_check_mode=True, - required_one_of=[['state', 'enabled']], + required_one_of=[('state', 'enabled')], ) # initialize service = module.params['name'] init_script = '/etc/init.d/' + service - rc = 0 - out = err = '' result = { 'name': service, 'changed': False, } - # check if service exists if not os.path.exists(init_script): module.fail_json(msg='service %s does not exist' % service) @@ -129,13 +121,10 @@ def main(): # Change enable/disable if needed if enabled != module.params['enabled']: result['changed'] = True - if module.params['enabled']: - action = 'enable' - else: - action = 'disable' + action = 'enable' if module.params['enabled'] else 'disable' if not module.check_mode: - (rc, out, err) = module.run_command("%s %s" % (init_script, action)) + rc, dummy, err = module.run_command([init_script, action]) # openwrt init scripts can return a non-zero exit code on a successful 'enable' # command if the init script doesn't contain a STOP value, so we ignore the exit # code and explicitly check if the service is now in the desired state @@ -153,17 +142,13 @@ def main(): psbin = module.get_bin_path('ps', True) # this should be busybox ps, so we only want/need to the 'w' option - (rc, psout, pserr) = module.run_command('%s w' % psbin) + rc, psout, dummy = module.run_command([psbin, 'w']) # If rc is 0, set running as appropriate if rc == 0: lines = psout.split("\n") - for line in lines: - if module.params['pattern'] in line and "pattern=" not in line: - # so as to not confuse ./hacking/test-module.py - running = True - break + running = any((module.params['pattern'] in line and "pattern=" not in line) for line in lines) else: - (rc, out, err) = module.run_command("%s running" % init_script) + rc, dummy, dummy = module.run_command([init_script, 'running']) if rc == 0: running = True @@ -187,7 +172,7 @@ def main(): if action: if not module.check_mode: - (rc, out, err) = module.run_command("%s %s" % (init_script, action)) + rc, dummy, err = module.run_command([init_script, action]) if rc != 0: module.fail_json(msg="Unable to %s service %s: %s" % (action, service, err))