added innitial daemon-reloaded support to service module

This commit is contained in:
Brian Coca 2015-06-06 13:41:22 -04:00 committed by Matt Clay
parent 59f7db899d
commit 55d04f4ddf

View file

@ -34,12 +34,13 @@ options:
- Name of the service. - Name of the service.
state: state:
required: false required: false
choices: [ started, stopped, restarted, reloaded ] choices: [ started, stopped, restarted, reloaded, daemon_reloaded ]
description: description:
- C(started)/C(stopped) are idempotent actions that will not run - C(started)/C(stopped) are idempotent actions that will not run
commands unless necessary. C(restarted) will always bounce the commands unless necessary. C(restarted) will always bounce the
service. C(reloaded) will always reload. B(At least one of state service. C(reloaded) will always reload. B(At least one of state
and enabled are required.) and enabled are required.)
- The C(daemon_reloaded) state was added in 2.0, it is exclusive for systemd.
sleep: sleep:
required: false required: false
version_added: "1.3" version_added: "1.3"
@ -279,7 +280,7 @@ class Service(object):
# Find ps binary # Find ps binary
psbin = self.module.get_bin_path('ps', True) psbin = self.module.get_bin_path('ps', True)
(rc, psout, pserr) = self.execute_command('%s %s' % (psbin, psflags)) (rc, psout, pserr) = execute_command('%s %s' % (psbin, psflags))
# If rc is 0, set running as appropriate # If rc is 0, set running as appropriate
if rc == 0: if rc == 0:
self.running = False self.running = False
@ -1413,7 +1414,7 @@ def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
name = dict(required=True), name = dict(required=True),
state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded']), state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded', 'daemon_reloaded']),
sleep = dict(required=False, type='int', default=None), sleep = dict(required=False, type='int', default=None),
pattern = dict(required=False, default=None), pattern = dict(required=False, default=None),
enabled = dict(type='bool'), enabled = dict(type='bool'),
@ -1440,66 +1441,81 @@ def main():
result = {} result = {}
result['name'] = service.name result['name'] = service.name
# Find service management tools # shortcut for systemd only daemon-reloaded
service.get_service_tools() if module.params['state'] == 'daemon_reloaded':
cmd = module.get_bin_path('systemctl', True)
svc_cmd = "%s %s %s" % (cmd, service.name, 'daemon-reloaded')
rc, stdout, stderr = module.run_command(svc_cmd)
result['msg']=stdout
if rc != 0:
result['rc'] = rc
if stderr:
result['msg']=stderr
module.fail_json(**result)
# Enable/disable service startup at boot if requested result['changed']=True
if service.module.params['enabled'] is not None:
# FIXME: ideally this should detect if we need to toggle the enablement state, though
# it's unlikely the changed handler would need to fire in this case so it's a minor thing.
service.service_enable()
result['enabled'] = service.enable
if module.params['state'] is None:
# Not changing the running state, so bail out now.
result['changed'] = service.changed
module.exit_json(**result)
result['state'] = service.state
# Collect service status
if service.pattern:
service.check_ps()
else: else:
service.get_service_status() # Find service management tools
service.get_service_tools()
# Calculate if request will change service state # Enable/disable service startup at boot if requested
service.check_service_changed() if service.module.params['enabled'] is not None:
# FIXME: ideally this should detect if we need to toggle the enablement state, though
# it's unlikely the changed handler would need to fire in this case so it's a minor thing.
service.service_enable()
result['enabled'] = service.enable
# Modify service state if necessary if module.params['state'] is None:
(rc, out, err) = service.modify_service_state() # Not changing the running state, so bail out now.
result['changed'] = service.changed
module.exit_json(**result)
if rc != 0: result['state'] = service.state
if err and "Job is already running" in err:
# upstart got confused, one such possibility is MySQL on Ubuntu 12.04 # Collect service status
# where status may report it has no start/stop links and we could if service.pattern:
# not get accurate status service.check_ps()
pass
else: else:
if err: service.get_service_status()
module.fail_json(msg=err)
# Calculate if request will change service state
service.check_service_changed()
# Modify service state if necessary
(rc, out, err) = service.modify_service_state()
if rc != 0:
if err and "Job is already running" in err:
# upstart got confused, one such possibility is MySQL on Ubuntu 12.04
# where status may report it has no start/stop links and we could
# not get accurate status
pass
else: else:
module.fail_json(msg=out) if err:
module.fail_json(msg=err)
else:
module.fail_json(msg=out)
result['changed'] = service.changed | service.svc_change result['changed'] = service.changed | service.svc_change
if service.module.params['enabled'] is not None: if service.module.params['enabled'] is not None:
result['enabled'] = service.module.params['enabled'] result['enabled'] = service.module.params['enabled']
if not service.module.params['state']: if not service.module.params['state']:
status = service.get_service_status() status = service.get_service_status()
if status is None: if status is None:
result['state'] = 'absent' result['state'] = 'absent'
elif status is False: elif status is False:
result['state'] = 'started' result['state'] = 'started'
else:
result['state'] = 'stopped'
else: else:
result['state'] = 'stopped' # as we may have just bounced the service the service command may not
else: # report accurate state at this moment so just show what we ran
# as we may have just bounced the service the service command may not if service.module.params['state'] in ['started','restarted','running','reloaded']:
# report accurate state at this moment so just show what we ran result['state'] = 'started'
if service.module.params['state'] in ['started','restarted','running','reloaded']: else:
result['state'] = 'started' result['state'] = 'stopped'
else:
result['state'] = 'stopped'
module.exit_json(**result) module.exit_json(**result)