mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
A basic service module with 'ensure' idempotence semantics. Playbook updated to use service
module vs command module for restarting. May be some bugs and requires the service to implement 'status' -- and probably some better error handling (i.e. return JSON with "failed" element if failed). Improvements welcome.
This commit is contained in:
parent
3c3111c1f3
commit
d308254eae
3 changed files with 87 additions and 2 deletions
|
@ -21,7 +21,7 @@
|
|||
handlers:
|
||||
- do:
|
||||
- restart apache
|
||||
- command /sbin/service httpd restart
|
||||
- service name=httpd ensure=restarted
|
||||
- do:
|
||||
- quack like a duck
|
||||
- command /bin/true
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
|
||||
import ansible.runner
|
||||
import ansible.constants as C
|
||||
import json
|
||||
import yaml
|
||||
import shlex
|
||||
|
||||
|
|
86
library/service
Executable file
86
library/service
Executable file
|
@ -0,0 +1,86 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
import simplejson as json
|
||||
import os
|
||||
import sys
|
||||
import shlex
|
||||
import subprocess
|
||||
|
||||
# ===========================================
|
||||
# convert arguments of form ensure=running name=foo
|
||||
# to a dictionary
|
||||
# FIXME: make more idiomatic
|
||||
|
||||
args = " ".join(sys.argv[1:])
|
||||
items = shlex.split(args)
|
||||
params = {}
|
||||
for x in items:
|
||||
(k, v) = x.split("=")
|
||||
params[k] = v
|
||||
|
||||
name = params['name']
|
||||
ensure = params.get('ensure','running')
|
||||
|
||||
# ===========================================
|
||||
# get service status
|
||||
|
||||
status = os.popen("/sbin/service %s status" % name).read()
|
||||
|
||||
# ===========================================
|
||||
# determine if we are going to change anything
|
||||
|
||||
running = False
|
||||
if status.find("not running") != -1:
|
||||
running = False
|
||||
elif status.find("running") != -1:
|
||||
running = True
|
||||
elif name == 'iptables' and status.find("ACCEPT") != -1:
|
||||
# iptables status command output is lame
|
||||
# TODO: lookup if we can use a return code for this instead?
|
||||
running = True
|
||||
|
||||
changed = False
|
||||
if not running and ensure == "started":
|
||||
changed = True
|
||||
elif running and ensure == "stopped":
|
||||
changed = True
|
||||
elif ensure == "restarted":
|
||||
changed = True
|
||||
|
||||
# ===========================================
|
||||
# run change commands if we need to
|
||||
|
||||
def _run(cmd):
|
||||
return subprocess.call(cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE, shell=True)
|
||||
|
||||
|
||||
rc = 0
|
||||
if changed:
|
||||
if ensure == 'started':
|
||||
rc = _run("/sbin/service %s start" % name)
|
||||
elif ensure == 'stopped':
|
||||
rc = _run("/sbin/service %s stop" % name)
|
||||
elif ensure == 'restarted':
|
||||
rc1 = _run("/sbin/service %s stop" % name)
|
||||
rc2 = _run("/sbin/service %s start" % name)
|
||||
rc = rc1 and rc2
|
||||
|
||||
if rc != 0:
|
||||
# yeah, should probably include output of failure...
|
||||
print json.dumps({
|
||||
"failed" : 1
|
||||
})
|
||||
sys.exit(1)
|
||||
|
||||
# ===============================================
|
||||
# success
|
||||
|
||||
print json.dumps({
|
||||
"changed" : changed
|
||||
})
|
||||
|
Loading…
Reference in a new issue