Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added conditional-reload support #1

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions system/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
- Name of the service.
state:
required: false
choices: [ started, stopped, restarted, reloaded ]
choices: [ started, stopped, restarted, reloaded, reloaded-if-running ]
description:
- C(started)/C(stopped) are idempotent actions that will not run
commands unless necessary. C(restarted) will always bounce the
service. C(reloaded) will always reload. B(At least one of state
and enabled are required.)
service. C(reloaded) will always reload. C(reloaded-if-running)
will pnly force a reload if the service is already running. B(At
least one of state and enabled are required.)
sleep:
required: false
version_added: "1.3"
Expand Down Expand Up @@ -108,6 +109,9 @@

# Example action to restart nova-compute if it exists
- service: name=nova-compute state=restarted must_exist=no

# Example action to only reload xinetd if it's previously been started
- service: name=xinetd state=reloaded-if-running must_exist=no
'''

import platform
Expand Down Expand Up @@ -298,7 +302,7 @@ def check_service_changed(self):
# Find out if state has changed
if not self.running and self.state in ["started", "running", "reloaded"]:
self.svc_change = True
elif self.running and self.state in ["stopped","reloaded"]:
elif self.running and self.state in ["stopped","reloaded","reloaded-if-running"]:
self.svc_change = True
elif self.state == "restarted":
self.svc_change = True
Expand All @@ -320,6 +324,8 @@ def modify_service_state(self):
self.action = "reload"
elif self.state == 'restarted':
self.action = "restart"
elif self.running and self.state == 'reloaded-if-running':
self.action = "reload"

if self.module.check_mode:
self.module.exit_json(changed=True, msg='changing service state')
Expand Down Expand Up @@ -1436,7 +1442,7 @@ def main():
module = AnsibleModule(
argument_spec = dict(
name = dict(required=True),
state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded']),
state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded', 'reloaded-if-running']),
sleep = dict(required=False, type='int', default=None),
pattern = dict(required=False, default=None),
enabled = dict(type='bool'),
Expand Down Expand Up @@ -1519,7 +1525,7 @@ def main():
else:
# as we may have just bounced the service the service command may not
# report accurate state at this moment so just show what we ran
if service.module.params['state'] in ['started','restarted','running','reloaded']:
if service.module.params['state'] in ['started','restarted','running','reloaded','reloaded-if-running']:
result['state'] = 'started'
else:
result['state'] = 'stopped'
Expand Down