From 00031e454e02272d50051df4841e5f9487d27bf7 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Fri, 6 Jan 2017 15:05:10 +0100 Subject: [PATCH] Sanitize folder path from the very start (#19929) - If an absolute path is provided, ensure it starts with /vm - Also ensure there are no trailing slashes This gets rid of a few locations where the same was being done. It also fixes the cases of multiple trailing slashes, or ending up with /vm/ instead of /vm. --- .../modules/cloud/vmware/vmware_guest.py | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index 2e4772fbc6..a280a0adeb 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -532,10 +532,7 @@ class PyVmomiHelper(object): if isinstance(folder, tuple): folder = folder[1] - if inpath == '/': - thispath = '/vm' - else: - thispath = os.path.join(inpath, folder['name']) + thispath = os.path.join(inpath, folder['name']) if thispath not in self.foldermap['paths']: self.foldermap['paths'][thispath] = [] @@ -602,14 +599,9 @@ class PyVmomiHelper(object): if uuid: vm = self.content.searchIndex.FindByUuid(uuid=uuid, vmSearch=True) elif folder: - if self.params['folder'].endswith('/'): - self.params['folder'] = self.params['folder'][0:-1] - # Build the absolute folder path to pass into the search method - if self.params['folder'].startswith('/vm'): + if self.params['folder'].startswith('/'): searchpath = '%(datacenter)s%(folder)s' % self.params - elif self.params['folder'].startswith('/'): - searchpath = '%(datacenter)s/vm%(folder)s' % self.params else: # need to look for matching absolute path if not self.folders: @@ -1195,8 +1187,6 @@ class PyVmomiHelper(object): # find matching folders if self.params['folder'].startswith('/'): - if not self.params['folder'].startswith('/vm'): - self.params['folder'] = '/vm' + self.params['folder'] folders = [x for x in self.foldermap['fvim_by_path'].items() if x[0] == self.params['folder']] else: folders = [x for x in self.foldermap['fvim_by_path'].items() if x[0].endswith(self.params['folder'])] @@ -1206,7 +1196,7 @@ class PyVmomiHelper(object): self.module.fail_json(msg='No folder matched the path: %(folder)s' % self.params) elif len(folders) > 1: self.module.fail_json( - msg='too many folders matched "%s", please give the full path starting with /vm/' % self.params[ + msg='Too many folders matched "%s", please give the full path starting with /vm/' % self.params[ 'folder']) # grab the folder vim object @@ -1677,6 +1667,11 @@ def main(): result = {'failed': False, 'changed': False} + # Prepend /vm if it was missing from the folder path, also strip trailing slashes + if not module.params['folder'].startswith('/vm') and module.params['folder'].startswith('/'): + module.params['folder'] = '/vm%(folder)s' % module.params + module.params['folder'] = module.params['folder'].rstrip('/') + # Fail check, customize require template to be defined if module.params["customize"] and not module.params['template']: module.fail_json(msg="customize option is only valid when template option is defined")