mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Add pattern option to service module
Adds ability to check service status based on pattern. The pattern is a simple string. If a pattern is provided, the output of ps is checked first.
This commit is contained in:
parent
fe923b9394
commit
18f0302de8
1 changed files with 33 additions and 3 deletions
|
@ -18,9 +18,12 @@
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import platform
|
||||||
|
|
||||||
SERVICE = None
|
SERVICE = None
|
||||||
CHKCONFIG = None
|
CHKCONFIG = None
|
||||||
INITCTL = None
|
INITCTL = None
|
||||||
|
PS_OPTIONS = 'auxww'
|
||||||
|
|
||||||
def _find_binaries(m):
|
def _find_binaries(m):
|
||||||
# list of possible paths for service/chkconfig binaries
|
# list of possible paths for service/chkconfig binaries
|
||||||
|
@ -56,14 +59,33 @@ def _find_binaries(m):
|
||||||
else:
|
else:
|
||||||
INITCTL = None
|
INITCTL = None
|
||||||
|
|
||||||
def _get_service_status(name):
|
def _get_service_status(name, pattern):
|
||||||
rc, status_stdout, status_stderr = _run("%s %s status" % (SERVICE, name))
|
rc, status_stdout, status_stderr = _run("%s %s status" % (SERVICE, name))
|
||||||
|
|
||||||
# set the running state to None because we don't know it yet
|
# set the running state to None because we don't know it yet
|
||||||
running = None
|
running = None
|
||||||
|
|
||||||
|
# If pattern is provided, search for that
|
||||||
|
# before checking initctl, service output, and other tricks
|
||||||
|
if pattern is not None:
|
||||||
|
psbin = '/bin/ps'
|
||||||
|
if not os.path.exists(psbin):
|
||||||
|
if os.path.exists('/usr/bin/ps'):
|
||||||
|
psbin = '/usr/bin/ps'
|
||||||
|
else:
|
||||||
|
psbin = None
|
||||||
|
if psbin is not None:
|
||||||
|
(rc, psout, pserr) = _run('%s %s' % (psbin, PS_OPTIONS))
|
||||||
|
# If rc is 0, set running as appropriate
|
||||||
|
# If ps command fails, fall back to other means.
|
||||||
|
if rc == 0:
|
||||||
|
if pattern in psout:
|
||||||
|
running = True
|
||||||
|
else:
|
||||||
|
running = False
|
||||||
|
|
||||||
# Check if we got upstart on the system and then the job state
|
# Check if we got upstart on the system and then the job state
|
||||||
if INITCTL != None:
|
if INITCTL != None and running is None:
|
||||||
# check the job status by upstart response
|
# check the job status by upstart response
|
||||||
initctl_rc, initctl_status_stdout, initctl_status_stderr = _run("%s status %s" % (INITCTL, name))
|
initctl_rc, initctl_status_stdout, initctl_status_stderr = _run("%s status %s" % (INITCTL, name))
|
||||||
if initctl_status_stdout.find("stop/waiting") != -1:
|
if initctl_status_stdout.find("stop/waiting") != -1:
|
||||||
|
@ -134,21 +156,29 @@ def main():
|
||||||
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']),
|
||||||
|
pattern = dict(required=False, default=None),
|
||||||
enabled = dict(choices=BOOLEANS)
|
enabled = dict(choices=BOOLEANS)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
name = module.params['name']
|
name = module.params['name']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
pattern = module.params['pattern']
|
||||||
enable = module.boolean(module.params.get('enabled', None))
|
enable = module.boolean(module.params.get('enabled', None))
|
||||||
|
|
||||||
|
# Set PS options here if 'ps auxww' will not work on
|
||||||
|
# target platform
|
||||||
|
if platform.system() == 'SunOS':
|
||||||
|
global PS_OPTIONS
|
||||||
|
PS_OPTIONS = '-ef'
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# find binaries locations on minion
|
# find binaries locations on minion
|
||||||
_find_binaries(module)
|
_find_binaries(module)
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# get service status
|
# get service status
|
||||||
running = _get_service_status(name)
|
running = _get_service_status(name, pattern)
|
||||||
|
|
||||||
# ===========================================
|
# ===========================================
|
||||||
# Some common variables
|
# Some common variables
|
||||||
|
|
Loading…
Reference in a new issue