From c238d3070a5eb116efa5de1d5027e4a8c377294e Mon Sep 17 00:00:00 2001 From: Andrew Gaffney Date: Fri, 28 Oct 2016 22:13:24 -0600 Subject: [PATCH] Minor fixes for openwrt_init for busybox ps and worthless exit codes --- .../modules/extras/system/openwrt_init.py | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/ansible/modules/extras/system/openwrt_init.py b/lib/ansible/modules/extras/system/openwrt_init.py index 0a92b18e93c..c54cd3295b3 100644 --- a/lib/ansible/modules/extras/system/openwrt_init.py +++ b/lib/ansible/modules/extras/system/openwrt_init.py @@ -78,10 +78,22 @@ import glob from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_bytes, to_native +module = None +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 + # =========================================== # Main control flow def main(): + global module, init_script # init module = AnsibleModule( argument_spec = dict( @@ -111,11 +123,7 @@ def main(): # Enable/disable service startup at boot if requested if module.params['enabled'] is not None: # do we need to enable the service? - enabled = False - (rc, out, err) = module.run_command("%s enabled" % init_script) - - if rc == 0: - enabled = True + enabled = is_enabled() # default to current state result['enabled'] = enabled @@ -130,7 +138,10 @@ def main(): if not module.check_mode: (rc, out, err) = module.run_command("%s %s" % (init_script, action)) - if rc != 0: + # 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 + if is_enabled() != module.params['enabled']: module.fail_json(msg="Unable to %s service %s: %s" % (action, service, err)) result['enabled'] = not enabled @@ -143,7 +154,8 @@ def main(): # Find ps binary psbin = module.get_bin_path('ps', True) - (rc, psout, pserr) = module.run_command('%s auxwww' % psbin) + # this should be busybox ps, so we only want/need to the 'w' option + (rc, psout, pserr) = module.run_command('%s w' % psbin) # If rc is 0, set running as appropriate if rc == 0: lines = psout.split("\n") @@ -154,7 +166,6 @@ def main(): break else: (rc, out, err) = module.run_command("%s running" % init_script) - if rc == 0: running = True