diff options
author | Brian Coca <bcoca@ansible.com> | 2015-05-26 13:39:08 -0400 |
---|---|---|
committer | Brian Coca <bcoca@ansible.com> | 2015-05-26 13:39:08 -0400 |
commit | 0c9dd0e6a4a29e6f5a144cbae2f790562d585a70 (patch) | |
tree | 20f152ecbb3a6d9b0bcd5cdf78399029e8937b8e | |
parent | ce4cc0282a0b0581bddecf5dc956cc436f715d43 (diff) | |
parent | cf0e8d62d255481dc63a337408d35666991d0236 (diff) | |
download | ansible-modules-core-0c9dd0e6a4a29e6f5a144cbae2f790562d585a70.tar.gz |
Merge pull request #751 from j2sol/service_must_exist
Add service option to avoid failure on missing service
-rw-r--r-- | system/service.py | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/system/service.py b/system/service.py index 14c0026d..41c00ea9 100644 --- a/system/service.py +++ b/system/service.py @@ -72,6 +72,14 @@ options: description: - Additional arguments provided on the command line aliases: [ 'args' ] + must_exist: + required: false + default: true + version_added: "1.9" + description: + - Avoid a module failure if the named service does not exist. Useful + for opportunistically starting/stopping/restarting a list of + potential services. ''' EXAMPLES = ''' @@ -95,6 +103,9 @@ EXAMPLES = ''' # Example action to restart network service for interface eth0 - service: name=network state=restarted args=eth0 + +# Example action to restart nova-compute if it exists +- service: name=nova-compute state=restarted must_exist=no ''' import platform @@ -468,7 +479,11 @@ class LinuxService(Service): self.enable_cmd = location['chkconfig'] if self.enable_cmd is None: - self.module.fail_json(msg="no service or tool found for: %s" % self.name) + if self.module.params['must_exist']: + self.module.fail_json(msg="no service or tool found for: %s" % self.name) + else: + # exiting without change on non-existent service + self.module.exit_json(changed=False, exists=False) # If no service control tool selected yet, try to see if 'service' is available if self.svc_cmd is None and location.get('service', False): @@ -476,7 +491,11 @@ class LinuxService(Service): # couldn't find anything yet if self.svc_cmd is None and not self.svc_initscript: - self.module.fail_json(msg='cannot find \'service\' binary or init script for service, possible typo in service name?, aborting') + if self.module.params['must_exist']: + self.module.fail_json(msg='cannot find \'service\' binary or init script for service, possible typo in service name?, aborting') + else: + # exiting without change on non-existent service + self.module.exit_json(changed=False, exists=False) if location.get('initctl', False): self.svc_initctl = location['initctl'] @@ -1400,6 +1419,7 @@ def main(): enabled = dict(type='bool'), runlevel = dict(required=False, default='default'), arguments = dict(aliases=['args'], default=''), + must_exist = dict(type='bool', default=True), ), supports_check_mode=True ) |