From 776374ee789c2fa258f821484896e6506a21526d Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Sat, 16 Nov 2024 19:15:12 +0100 Subject: [PATCH] [PR #9102/737717d0 backport][stable-10] launchd: Add plist option (#9137) launchd: Add plist option (#9102) This allows the module to be used with services such as com.openssh.sshd, when the name of the plist file doesn't match the service name. fixes #5932 (cherry picked from commit 737717d015f5e8ab6878662cacfa82cd39cd0e07) Co-authored-by: Alex Willmer --- changelogs/fragments/5932-launchd-plist.yml | 2 ++ plugins/modules/launchd.py | 31 +++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/5932-launchd-plist.yml diff --git a/changelogs/fragments/5932-launchd-plist.yml b/changelogs/fragments/5932-launchd-plist.yml new file mode 100644 index 0000000000..bf2530841a --- /dev/null +++ b/changelogs/fragments/5932-launchd-plist.yml @@ -0,0 +1,2 @@ +minor_changes: + - launchd - add ``plist`` option for services such as sshd, where the plist filename doesn't match the service name (https://github.com/ansible-collections/community.general/pull/9102). diff --git a/plugins/modules/launchd.py b/plugins/modules/launchd.py index a6427bdb2f..9717825c71 100644 --- a/plugins/modules/launchd.py +++ b/plugins/modules/launchd.py @@ -30,6 +30,12 @@ options: - Name of the service. type: str required: true + plist: + description: + - Name of the V(.plist) file for the service. + - Defaults to V({name}.plist). + type: str + version_added: 10.1.0 state: description: - V(started)/V(stopped) are idempotent actions that will not run @@ -100,6 +106,12 @@ EXAMPLES = r''' community.general.launchd: name: org.memcached state: unloaded + +- name: restart sshd + community.general.launchd: + name: com.openssh.sshd + plist: ssh.plist + state: restarted ''' RETURN = r''' @@ -145,25 +157,31 @@ class ServiceState: class Plist: - def __init__(self, module, service): + def __init__(self, module, service, filename=None): self.__changed = False self.__service = service + if filename is not None: + self.__filename = filename + else: + self.__filename = '%s.plist' % service state, pid, dummy, dummy = LaunchCtlList(module, self.__service).run() # Check if readPlist is available or not self.old_plistlib = hasattr(plistlib, 'readPlist') - self.__file = self.__find_service_plist(self.__service) + self.__file = self.__find_service_plist(self.__filename) if self.__file is None: - msg = 'Unable to infer the path of %s service plist file' % self.__service + msg = 'Unable to find the plist file %s for service %s' % ( + self.__filename, self.__service, + ) if pid is None and state == ServiceState.UNLOADED: msg += ' and it was not found among active services' module.fail_json(msg=msg) self.__update(module) @staticmethod - def __find_service_plist(service_name): + def __find_service_plist(filename): """Finds the plist file associated with a service""" launchd_paths = [ @@ -180,7 +198,6 @@ class Plist: except OSError: continue - filename = '%s.plist' % service_name if filename in files: return os.path.join(path, filename) return None @@ -461,6 +478,7 @@ def main(): module = AnsibleModule( argument_spec=dict( name=dict(type='str', required=True), + plist=dict(type='str'), state=dict(type='str', choices=['reloaded', 'restarted', 'started', 'stopped', 'unloaded']), enabled=dict(type='bool'), force_stop=dict(type='bool', default=False), @@ -472,6 +490,7 @@ def main(): ) service = module.params['name'] + plist_filename = module.params['plist'] action = module.params['state'] rc = 0 out = err = '' @@ -483,7 +502,7 @@ def main(): # We will tailor the plist file in case one of the options # (enabled, force_stop) was specified. - plist = Plist(module, service) + plist = Plist(module, service, plist_filename) result['changed'] = plist.is_changed() # Gather information about the service to be controlled.