Minor fixes for openwrt_init for busybox ps and worthless exit codes

pull/18777/head
Andrew Gaffney 8 years ago committed by Matt Clay
parent e064fd3256
commit c238d3070a

@ -78,10 +78,22 @@ import glob
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_bytes, to_native 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 # Main control flow
def main(): def main():
global module, init_script
# init # init
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
@ -111,11 +123,7 @@ def main():
# Enable/disable service startup at boot if requested # Enable/disable service startup at boot if requested
if module.params['enabled'] is not None: if module.params['enabled'] is not None:
# do we need to enable the service? # do we need to enable the service?
enabled = False enabled = is_enabled()
(rc, out, err) = module.run_command("%s enabled" % init_script)
if rc == 0:
enabled = True
# default to current state # default to current state
result['enabled'] = enabled result['enabled'] = enabled
@ -130,7 +138,10 @@ def main():
if not module.check_mode: if not module.check_mode:
(rc, out, err) = module.run_command("%s %s" % (init_script, action)) (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)) module.fail_json(msg="Unable to %s service %s: %s" % (action, service, err))
result['enabled'] = not enabled result['enabled'] = not enabled
@ -143,7 +154,8 @@ def main():
# Find ps binary # Find ps binary
psbin = module.get_bin_path('ps', True) 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 is 0, set running as appropriate
if rc == 0: if rc == 0:
lines = psout.split("\n") lines = psout.split("\n")
@ -154,7 +166,6 @@ def main():
break break
else: else:
(rc, out, err) = module.run_command("%s running" % init_script) (rc, out, err) = module.run_command("%s running" % init_script)
if rc == 0: if rc == 0:
running = True running = True

Loading…
Cancel
Save