2012-02-26 02:27:11 +01:00
|
|
|
#!/usr/bin/python
|
2012-08-03 03:29:10 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2012-02-29 01:08:09 +01:00
|
|
|
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-09-30 08:50:25 +02:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: service
|
|
|
|
author: Michael DeHaan
|
2012-10-01 09:18:54 +02:00
|
|
|
version_added: 0.1
|
2012-09-30 08:50:25 +02:00
|
|
|
short_description: Manage services.
|
|
|
|
description:
|
|
|
|
- Controls services on remote hosts.
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
required: true
|
2012-10-08 18:46:16 +02:00
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- Name of the service.
|
2012-09-30 08:50:25 +02:00
|
|
|
state:
|
|
|
|
required: false
|
2012-10-13 00:14:09 +02:00
|
|
|
choices: [ started, stopped, restarted, reloaded ]
|
2012-10-08 18:46:16 +02:00
|
|
|
description:
|
2012-11-21 18:49:30 +01:00
|
|
|
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.
|
2012-09-30 08:50:25 +02:00
|
|
|
pattern:
|
|
|
|
required: false
|
2012-10-01 09:18:54 +02:00
|
|
|
version_added: "0.7"
|
2012-10-08 18:46:16 +02:00
|
|
|
description:
|
2012-10-01 09:18:54 +02:00
|
|
|
- If the service does not respond to the status command, name a
|
|
|
|
substring to look for as would be found in the output of the I(ps)
|
|
|
|
command as a stand-in for a status result. If the string is found,
|
2012-10-08 18:46:16 +02:00
|
|
|
the service will be assumed to be running.
|
2012-09-30 08:50:25 +02:00
|
|
|
enabled:
|
|
|
|
required: false
|
2012-10-01 09:18:54 +02:00
|
|
|
choices: [ "yes", "no" ]
|
2012-10-08 18:46:16 +02:00
|
|
|
description:
|
|
|
|
- Whether the service should start on boot.
|
2012-11-15 18:31:40 +01:00
|
|
|
arguments:
|
|
|
|
description:
|
|
|
|
- Additional arguments provided on the command line
|
|
|
|
aliases: [ 'args' ]
|
2012-09-30 08:50:25 +02:00
|
|
|
examples:
|
2012-11-15 18:31:40 +01:00
|
|
|
- description: Example action to start service httpd, if not running
|
|
|
|
code: "service: name=httpd state=started"
|
|
|
|
- description: Example action to stop service httpd, if running
|
|
|
|
code: "service: name=httpd state=stopped"
|
|
|
|
- description: Example action to restart service httpd, in all cases
|
|
|
|
code: "service: name=httpd state=restarted"
|
|
|
|
- description: Example action to reload service httpd, in all cases
|
|
|
|
code: "service: name=httpd state=reloaded"
|
|
|
|
- description: Example action to start service foo, based on running process /usr/bin/foo
|
|
|
|
code: "service: name=foo pattern=/usr/bin/foo state=started"
|
|
|
|
- description: Example action to restart network service for interface eth0
|
|
|
|
code: "service: name=network state=restarted args=eth0"
|
2012-09-30 08:50:25 +02:00
|
|
|
'''
|
|
|
|
|
2012-08-17 22:55:18 +02:00
|
|
|
import platform
|
2012-10-31 03:06:11 +01:00
|
|
|
import os
|
|
|
|
import re
|
2012-08-17 22:55:18 +02:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
class Service(object):
|
|
|
|
"""
|
|
|
|
This is the generic Service manipulation class that is subclassed
|
|
|
|
based on platform.
|
|
|
|
|
|
|
|
A subclass should override the following action methods:-
|
|
|
|
- get_service_tools
|
|
|
|
- service_enable
|
|
|
|
- get_service_status
|
|
|
|
- service_control
|
|
|
|
|
|
|
|
All subclasses MUST define platform and distribution (which may be None).
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'Generic'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
return load_platform_subclass(Service, args, kwargs)
|
|
|
|
|
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
self.name = module.params['name']
|
|
|
|
self.state = module.params['state']
|
|
|
|
self.pattern = module.params['pattern']
|
|
|
|
self.enable = module.boolean(module.params.get('enabled', None))
|
|
|
|
self.changed = False
|
|
|
|
self.running = None
|
|
|
|
self.action = None
|
|
|
|
self.svc_cmd = None
|
|
|
|
self.svc_initscript = None
|
|
|
|
self.svc_initctl = None
|
|
|
|
self.enable_cmd = None
|
|
|
|
self.arguments = module.params.get('arguments', '')
|
|
|
|
|
|
|
|
# select whether we dump additional debug info through syslog
|
|
|
|
self.syslogging = False
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Platform specific methods (must be replaced by subclass).
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
self.module.fail_json(msg="get_service_tools not implemented on target platform")
|
|
|
|
|
|
|
|
def service_enable(self):
|
|
|
|
self.module.fail_json(msg="service_enable not implemented on target platform")
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
self.module.fail_json(msg="get_service_status not implemented on target platform")
|
|
|
|
|
|
|
|
def service_control(self):
|
|
|
|
self.module.fail_json(msg="service_control not implemented on target platform")
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Generic methods that should be used on all platforms.
|
|
|
|
|
|
|
|
def execute_command(self, cmd):
|
|
|
|
if self.syslogging:
|
|
|
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Command %s' % '|'.join(cmd))
|
|
|
|
|
|
|
|
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
(out, err) = p.communicate()
|
|
|
|
rc = p.returncode
|
|
|
|
return (rc, out, err)
|
|
|
|
|
|
|
|
def check_ps(self):
|
|
|
|
# Set ps flags
|
|
|
|
if platform.system() == 'SunOS':
|
|
|
|
psflags = '-ef'
|
|
|
|
else:
|
|
|
|
psflags = 'auxww'
|
|
|
|
|
|
|
|
# Find ps binary
|
|
|
|
psbin = self.module.get_bin_path('ps', True)
|
|
|
|
|
|
|
|
(rc, psout, pserr) = self.execute_command('%s %s' % (psbin, psflags))
|
|
|
|
# If rc is 0, set running as appropriate
|
|
|
|
if rc == 0:
|
|
|
|
self.running = False
|
|
|
|
lines = psout.split("\n")
|
|
|
|
for line in lines:
|
|
|
|
if self.pattern in line and not "pattern=" in line:
|
|
|
|
# so as to not confuse ./hacking/test-module
|
|
|
|
self.running = True
|
|
|
|
break
|
|
|
|
|
|
|
|
def check_service_changed(self):
|
|
|
|
if self.state and self.running == None:
|
|
|
|
self.module.fail_json(msg="failed determining the current service state => state stays unchanged")
|
|
|
|
# Find out if state has changed
|
|
|
|
if not self.running and self.state in ["started", "running"]:
|
|
|
|
self.changed = True
|
|
|
|
elif self.running and self.state in ["stopped","reloaded"]:
|
|
|
|
self.changed = True
|
|
|
|
elif self.state == "restarted":
|
|
|
|
self.changed = True
|
|
|
|
|
|
|
|
def modify_service_state(self):
|
|
|
|
# Only do something if state will change
|
|
|
|
if self.changed:
|
|
|
|
# Control service
|
|
|
|
if self.state in ['started', 'running']:
|
|
|
|
self.action = "start"
|
|
|
|
elif self.state == 'stopped':
|
|
|
|
self.action = "stop"
|
|
|
|
elif self.state == 'reloaded':
|
|
|
|
self.action = "reload"
|
|
|
|
elif self.state == 'restarted':
|
|
|
|
self.action = "restart"
|
|
|
|
|
|
|
|
return self.service_control()
|
|
|
|
|
|
|
|
else:
|
|
|
|
# If nothing needs to change just say all is well
|
|
|
|
rc = 0
|
|
|
|
err = ''
|
|
|
|
out = ''
|
|
|
|
return rc, out, err
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Subclass: Linux
|
|
|
|
|
|
|
|
class LinuxService(Service):
|
|
|
|
"""
|
|
|
|
This is the Linux Service manipulation class - it is currently supporting
|
|
|
|
a mixture of binaries and init scripts for controlling services started at
|
|
|
|
boot, as well as for controlling the current state.
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'Linux'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
paths = [ '/sbin', '/usr/sbin', '/bin', '/usr/bin' ]
|
|
|
|
binaries = [ 'service', 'chkconfig', 'update-rc.d', 'initctl', 'systemctl' ]
|
|
|
|
initpaths = [ '/etc/init.d' ]
|
|
|
|
location = dict()
|
|
|
|
|
|
|
|
for binary in binaries:
|
|
|
|
location[binary] = None
|
|
|
|
|
|
|
|
for binary in binaries:
|
|
|
|
location[binary] = self.module.get_bin_path(binary)
|
|
|
|
|
|
|
|
# Locate a tool for enable options
|
|
|
|
if location.get('systemctl', None):
|
|
|
|
self.enable_cmd = location['systemctl']
|
|
|
|
elif location.get('chkconfig', None):
|
|
|
|
self.enable_cmd = location['chkconfig']
|
|
|
|
elif location.get('update-rc.d', None):
|
|
|
|
self.enable_cmd = location['update-rc.d']
|
|
|
|
|
|
|
|
if self.enable_cmd is None:
|
|
|
|
self.module.fail_json(msg='unable to find enable binary')
|
|
|
|
|
|
|
|
# Locate a tool for runtime service management (start, stop etc.)
|
|
|
|
if location.get('service', None):
|
|
|
|
self.svc_cmd = location['service']
|
|
|
|
else:
|
|
|
|
for initdir in initpaths:
|
|
|
|
initscript = "%s/%s" % (initdir,self.name)
|
|
|
|
if os.path.isfile(initscript):
|
|
|
|
self.svc_initscript = initscript
|
|
|
|
|
|
|
|
if not self.svc_cmd and not self.svc_initscript:
|
|
|
|
self.module.fail_json(msg='unable to find service binary nor initscript')
|
|
|
|
|
|
|
|
if location.get('initctl', None):
|
|
|
|
self.svc_initctl = location['initctl']
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
rc, status_stdout, status_stderr = self.execute_command("%s %s status %s" % (self.svc_cmd, self.name, self.arguments))
|
|
|
|
|
|
|
|
# Check if we got upstart on the system and then the job state
|
|
|
|
if self.svc_initctl != None and self.running is None:
|
|
|
|
# check the job status by upstart response
|
|
|
|
initctl_rc, initctl_status_stdout, initctl_status_stderr = self.execute_command("%s status %s" % (self.svc_initctl, self.name))
|
|
|
|
if initctl_status_stdout.find("stop/waiting") != -1:
|
|
|
|
self.running = False
|
|
|
|
elif initctl_status_stdout.find("start/running") != -1:
|
|
|
|
self.running = True
|
|
|
|
|
|
|
|
# if the job status is still not known check it by response code
|
|
|
|
if self.running == None:
|
|
|
|
if rc == 3:
|
|
|
|
self.running = False
|
|
|
|
if rc == 2:
|
|
|
|
self.running = False
|
|
|
|
elif rc == 0:
|
|
|
|
self.running = True
|
|
|
|
|
|
|
|
# if the job status is still not known check it by status output keywords
|
|
|
|
if self.running == None:
|
|
|
|
# first tranform the status output that could irritate keyword matching
|
|
|
|
cleanout = status_stdout.lower().replace(self.name.lower(), '')
|
|
|
|
if "stop" in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif "run" in cleanout and "not" in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif "run" in cleanout and "not" not in cleanout:
|
|
|
|
self.running = True
|
|
|
|
elif "start" in cleanout and "not" not in cleanout:
|
|
|
|
self.running = True
|
|
|
|
elif 'could not access pid file' in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif 'is dead and pid file exists' in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif 'dead but subsys locked' in cleanout:
|
|
|
|
self.running = False
|
|
|
|
elif 'dead but pid file exists' in cleanout:
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
# if the job status is still not known check it by special conditions
|
|
|
|
if self.running == None:
|
|
|
|
if self.name == 'iptables' and status_stdout.find("ACCEPT") != -1:
|
|
|
|
# iptables status command output is lame
|
|
|
|
# TODO: lookup if we can use a return code for this instead?
|
|
|
|
self.running = True
|
|
|
|
|
|
|
|
def service_enable(self):
|
|
|
|
# we change argument depending on real binary used
|
|
|
|
# update-rc.d wants enable/disable while
|
|
|
|
# chkconfig wants on/off
|
|
|
|
# also, systemctl needs the arguments reversed
|
|
|
|
if self.enable:
|
|
|
|
on_off = "on"
|
|
|
|
enable_disable = "enable"
|
|
|
|
else:
|
|
|
|
on_off = "off"
|
|
|
|
enable_disable = "disable"
|
|
|
|
|
|
|
|
if self.enable_cmd.endswith("update-rc.d"):
|
|
|
|
args = (self.enable_cmd, self.name, enable_disable)
|
|
|
|
elif self.enable_cmd.endswith("systemctl"):
|
|
|
|
args = (self.enable_cmd, enable_disable, self.name + ".service")
|
|
|
|
else:
|
|
|
|
args = (self.enable_cmd, self.name, on_off)
|
|
|
|
|
|
|
|
if self.enable is not None:
|
|
|
|
return self.execute_command("%s %s %s" % args)
|
|
|
|
|
|
|
|
def service_control(self):
|
|
|
|
|
|
|
|
# Decide what command to run
|
|
|
|
if self.svc_cmd:
|
|
|
|
svc_cmd = "%s %s" % (self.svc_cmd, self.name)
|
|
|
|
elif self.svc_initscript:
|
|
|
|
svc_cmd = "%s" % self.svc_initscript
|
|
|
|
|
|
|
|
if self.action is not "restart":
|
|
|
|
rc_state, stdout, stderr = self.execute_command("%s %s %s" % (svc_cmd, self.action, self.arguments))
|
|
|
|
else:
|
|
|
|
rc1, stdout1, stderr1 = self.execute_command("%s %s %s" % (svc_cmd, stop, self.arguments))
|
|
|
|
rc2, stdout2, stderr2 = self.execute_command("%s %s %s" % (svc_cmd, start, self.arguments))
|
|
|
|
if rc1 != 0 and rc2 == 0:
|
|
|
|
rc_state = rc + rc2
|
|
|
|
stdout = stdout2
|
|
|
|
stderr = stderr2
|
2012-08-17 22:55:18 +02:00
|
|
|
else:
|
2012-11-23 21:03:17 +01:00
|
|
|
rc_state = rc + rc1 + rc2
|
|
|
|
stdout = stdout1 + stdout2
|
|
|
|
stderr = stderr1 + stderr2
|
|
|
|
|
|
|
|
return(rc_state, stdout, stderr)
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Subclass: FreeBSD
|
|
|
|
|
|
|
|
class FreeBsdService(Service):
|
|
|
|
"""
|
|
|
|
This is the FreeBSD Service manipulation class - it uses the /etc/rc.conf
|
|
|
|
file for controlling services started at boot and the 'service' binary to
|
|
|
|
check status and perform direct service manipulation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'FreeBSD'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
self.svc_cmd = self.module.get_bin_path('service', True)
|
|
|
|
|
|
|
|
if not self.svc_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find service binary')
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
rc, stdout, stderr = self.execute_command("%s %s %s" % (self.svc_cmd, self.name, 'onestatus'))
|
|
|
|
if rc == 1:
|
|
|
|
self.running = False
|
2012-06-17 15:55:26 +02:00
|
|
|
elif rc == 0:
|
2012-11-23 21:03:17 +01:00
|
|
|
self.running = True
|
|
|
|
|
|
|
|
def service_enable(self):
|
|
|
|
if self.enable:
|
|
|
|
rc = "YES"
|
|
|
|
else:
|
|
|
|
rc = "NO"
|
|
|
|
|
|
|
|
rcfiles = [ '/etc/rc.conf','/usr/local/etc/rc.conf' ]
|
|
|
|
for rcfile in rcfiles:
|
|
|
|
if os.path.isfile(rcfile):
|
|
|
|
rcconf = rcfile
|
|
|
|
|
|
|
|
entry = "%s_enable" % self.name
|
2012-10-31 03:06:11 +01:00
|
|
|
full_entry = '%s="%s"' % (entry,rc)
|
2012-11-23 21:03:17 +01:00
|
|
|
rc = open(rcconf,"r+")
|
2012-10-31 03:06:11 +01:00
|
|
|
rctext = rc.read()
|
|
|
|
if re.search("^%s" % full_entry,rctext,re.M) is None:
|
|
|
|
if re.search("^%s" % entry,rctext,re.M) is None:
|
|
|
|
rctext += "\n%s" % full_entry
|
|
|
|
else:
|
|
|
|
rctext = re.sub("^%s.*" % entry,full_entry,rctext,1,re.M)
|
|
|
|
rc.truncate(0)
|
|
|
|
rc.seek(0)
|
|
|
|
rc.write(rctext)
|
|
|
|
rc.close()
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
def service_control(self):
|
|
|
|
if self.action is "start":
|
|
|
|
self.action = "onestart"
|
|
|
|
if self.action is "stop":
|
|
|
|
self.action = "onestop"
|
|
|
|
if self.action is "reload":
|
|
|
|
self.action = "onereload"
|
|
|
|
|
|
|
|
return self.execute_command("%s %s %s" % (self.svc_cmd, self.name, self.action))
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# Subclass: OpenBSD
|
|
|
|
|
|
|
|
class OpenBsdService(Service):
|
|
|
|
"""
|
|
|
|
This is the OpenBSD Service manipulation class - it uses /etc/rc.d for
|
|
|
|
service control. Enabling a service is currently not supported because the
|
|
|
|
<service>_flags variable is not boolean, you should supply a rc.conf.local
|
|
|
|
file in some other way.
|
|
|
|
"""
|
|
|
|
|
|
|
|
platform = 'OpenBSD'
|
|
|
|
distribution = None
|
|
|
|
|
|
|
|
def get_service_tools(self):
|
|
|
|
rcdir = '/etc/rc.d'
|
|
|
|
|
|
|
|
rc_script = "%s/%s" % (rcdir, self.name)
|
|
|
|
if os.path.isfile(rc_script):
|
|
|
|
self.svc_cmd = rc_script
|
|
|
|
|
|
|
|
if not self.svc_cmd:
|
|
|
|
self.module.fail_json(msg='unable to find rc.d script')
|
|
|
|
|
|
|
|
def get_service_status(self):
|
|
|
|
rc, stdout, stderr = self.execute_command("%s %s" % (self.svc_cmd, 'check'))
|
|
|
|
if rc == 1:
|
|
|
|
self.running = False
|
|
|
|
elif rc == 0:
|
|
|
|
self.running = True
|
2012-05-03 08:34:36 +02:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
def service_control(self):
|
|
|
|
return self.execute_command("%s %s" % (self.svc_cmd, self.action))
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# ===========================================
|
|
|
|
# Main control flow
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-07-30 11:18:24 +02:00
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
name = dict(required=True),
|
2012-08-01 23:44:26 +02:00
|
|
|
state = dict(choices=['running', 'started', 'stopped', 'restarted', 'reloaded']),
|
2012-08-17 22:55:18 +02:00
|
|
|
pattern = dict(required=False, default=None),
|
2012-11-15 18:31:40 +01:00
|
|
|
enabled = dict(choices=BOOLEANS),
|
2012-11-21 16:11:20 +01:00
|
|
|
arguments = dict(aliases=['args'], default=''),
|
2012-07-30 11:18:24 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
service = Service(module)
|
|
|
|
|
|
|
|
if service.syslogging:
|
|
|
|
syslog.openlog('ansible-%s' % os.path.basename(__file__))
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Service instantiated - platform %s' % service.platform)
|
|
|
|
if service.distribution:
|
|
|
|
syslog.syslog(syslog.LOG_NOTICE, 'Service instantiated - distribution %s' % service.distribution)
|
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
rc = 0
|
2012-07-30 11:18:24 +02:00
|
|
|
out = ''
|
2012-11-23 21:03:17 +01:00
|
|
|
err = ''
|
|
|
|
result = {}
|
|
|
|
result['name'] = service.name
|
|
|
|
result['state'] = service.state
|
2012-08-07 02:07:02 +02:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
# Find service management tools
|
|
|
|
service.get_service_tools()
|
|
|
|
|
|
|
|
# Enable/disable service startup at boot if requested
|
|
|
|
if service.module.params['enabled']:
|
|
|
|
service.service_enable()
|
|
|
|
|
|
|
|
# Collect service status
|
|
|
|
if service.pattern:
|
|
|
|
service.check_ps()
|
|
|
|
service.get_service_status()
|
|
|
|
|
|
|
|
# Calculate if request will change service state
|
|
|
|
service.check_service_changed()
|
|
|
|
|
|
|
|
# Modify service state if necessary
|
|
|
|
(rc, out, err) = service.modify_service_state()
|
2012-05-03 12:36:06 +02:00
|
|
|
|
2012-04-27 04:43:36 +02:00
|
|
|
if rc != 0:
|
2012-11-23 21:03:17 +01:00
|
|
|
if err:
|
|
|
|
module.fail_json(msg=err)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=out)
|
2012-02-26 02:27:11 +01:00
|
|
|
|
2012-11-23 21:03:17 +01:00
|
|
|
result['changed'] = service.changed
|
|
|
|
if service.module.params['enabled']:
|
|
|
|
result['enabled'] = service.module.params['enabled']
|
|
|
|
if service.state:
|
|
|
|
result['state'] = service.state
|
2012-10-31 03:06:11 +01:00
|
|
|
|
2012-08-11 18:35:58 +02:00
|
|
|
module.exit_json(**result)
|
2012-08-07 02:07:02 +02:00
|
|
|
|
|
|
|
# this is magic, see lib/ansible/module_common.py
|
|
|
|
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
|
2012-04-27 04:43:36 +02:00
|
|
|
|
2012-07-30 11:18:24 +02:00
|
|
|
main()
|