diff --git a/lib/ansible/modules/cloud/vmware/vmware_guest.py b/lib/ansible/modules/cloud/vmware/vmware_guest.py index f7e99e42f3..1df2f85393 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/cloud/vmware/vmware_guest.py @@ -1198,17 +1198,24 @@ class PyVmomiHelper(PyVmomi): return root def get_resource_pool(self): - resource_pool = None - - if self.params['esxi_hostname']: + # highest priority, resource_pool given. + if self.params['resource_pool']: + resource_pool = self.select_resource_pool_by_name(self.params['resource_pool']) + # next priority, esxi hostname given. + elif self.params['esxi_hostname']: host = self.select_host() resource_pool = self.select_resource_pool_by_host(host) + # next priority, cluster given, take the root of the pool + elif self.params['cluster']: + cluster = self.cache.get_cluster(self.params['cluster']) + resource_pool = cluster.resourcePool + # fallback, pick any RP else: resource_pool = self.select_resource_pool_by_name(self.params['resource_pool']) if resource_pool is None: - self.module.fail_json(msg='Unable to find resource pool "%(resource_pool)s"' % self.params) + self.module.fail_json(msg='Unable to find resource pool, need esxi_hostname, resource_pool, or cluster') return resource_pool diff --git a/test/integration/targets/vmware_guest/tasks/create_rp_d1_c1_f0.yml b/test/integration/targets/vmware_guest/tasks/create_rp_d1_c1_f0.yml new file mode 100644 index 0000000000..161cd0a92a --- /dev/null +++ b/test/integration/targets/vmware_guest/tasks/create_rp_d1_c1_f0.yml @@ -0,0 +1,259 @@ +- name: Wait for Flask controller to come up online + wait_for: + host: "{{ vcsim }}" + port: 5000 + state: started + +- name: kill vcsim + uri: + url: http://{{ vcsim }}:5000/killall +- name: start vcsim with no folders + uri: + url: http://{{ vcsim }}:5000/spawn?datacenter=1&cluster=2&pool=2&folder=0 + register: vcsim_instance + +- name: Wait for Flask controller to come up online + wait_for: + host: "{{ vcsim }}" + port: 443 + state: started + +- name: get a list of VMS from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=VM + register: vmlist + +- name: get a list of clusters from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=CCR + register: clusts + +- name: get a list of resource pools from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=RP + register: res_pools + +- name: get a list of hosts from vcsim + uri: + url: http://{{ vcsim }}:5000/govc_find?filter=H + register: hosts + +- debug: var=vcsim_instance +- debug: var=vmlist +- debug: var=res_pools +- debug: var=clusts +- debug: var=hosts + +# Create one with the defaults +- name: create new VM with default resource pool + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + #template: "{{ item|basename }}" + guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + hardware: + num_cpus: 1 + memory_mb: 512 + disk: + - size: 0gb + type: thin + autoselect_datastore: True + state: poweredoff + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'][0] }}" + register: clone_rp_d1_c1_f0 + +- debug: var=clone_rp_d1_c1_f0 + +- name: assert that changes were made + assert: + that: + - "clone_rp_d1_c1_f0.results|map(attribute='changed')|unique|list == [true]" + +- name: delete the new VMs + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + #template: "{{ item|basename }}" + #guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + state: absent + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'][0] }}" + register: clone_rp_d1_c1_f0_delete + +- debug: var=clone_rp_d1_c1_f0_delete + +- name: assert that changes were made with deletion + assert: + that: + - "clone_rp_d1_c1_f0_delete.results|map(attribute='changed')|unique|list == [true]" + +# now create with just a cluster +- name: create new VM with default resource pool in cluster + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + #template: "{{ item|basename }}" + guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + cluster: "{{ clusts['json'][0]|basename }}" + hardware: + num_cpus: 1 + memory_mb: 512 + disk: + - size: 0gb + type: thin + autoselect_datastore: True + state: poweredoff + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'][0] }}" + register: clone_rpc_d1_c1_f0 + +- debug: var=clone_rpc_d1_c1_f0 + +- name: assert that changes were made + assert: + that: + - "clone_rpc_d1_c1_f0.results|map(attribute='changed')|unique|list == [true]" + +- name: delete the new VMs + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + #template: "{{ item|basename }}" + #guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + cluster: "{{ clusts['json'][0]|basename }}" + state: absent + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'][0] }}" + register: clone_rpc_d1_c1_f0_delete + +- debug: var=clone_rpc_d1_c1_f0_delete + +- name: assert that changes were made with deletion + assert: + that: + - "clone_rpc_d1_c1_f0_delete.results|map(attribute='changed')|unique|list == [true]" + +# now create with a specific resource pool +- name: create new VM with specific resource pool in cluster + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + #template: "{{ item|basename }}" + guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + cluster: "{{ clusts['json'][0]|basename }}" + resource_pool: "{{ res_pools['json'][2]|basename }}" + hardware: + num_cpus: 1 + memory_mb: 512 + disk: + - size: 0gb + type: thin + autoselect_datastore: True + state: poweredoff + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'][0] }}" + register: clone_rpcp_d1_c1_f0 + +- debug: var=clone_rpcp_d1_c1_f0 + +- name: assert that changes were made + assert: + that: + - "clone_rpcp_d1_c1_f0.results|map(attribute='changed')|unique|list == [true]" + +- name: delete the new VMs + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + #template: "{{ item|basename }}" + #guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + cluster: "{{ clusts['json'][0]|basename }}" + state: absent + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'][0] }}" + register: clone_rpcp_d1_c1_f0_delete + +- debug: var=clone_rpcp_d1_c1_f0_delete + +- name: assert that changes were made with deletion + assert: + that: + - "clone_rpcp_d1_c1_f0_delete.results|map(attribute='changed')|unique|list == [true]" + +# now create with a specific host +- name: create new VM with specific host + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + #template: "{{ item|basename }}" + guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + esxi_hostname: "{{ hosts['json'][0]|basename }}" + hardware: + num_cpus: 1 + memory_mb: 512 + disk: + - size: 0gb + type: thin + autoselect_datastore: True + state: poweredoff + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'][0] }}" + register: clone_rph_d1_c1_f0 + +- debug: var=clone_rph_d1_c1_f0 + +- name: assert that changes were made + assert: + that: + - "clone_rph_d1_c1_f0.results|map(attribute='changed')|unique|list == [true]" + +- name: delete the new VMs + vmware_guest: + validate_certs: False + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance['json']['username'] }}" + password: "{{ vcsim_instance['json']['password'] }}" + name: "{{ 'newvm_' + item|basename }}" + #template: "{{ item|basename }}" + #guest_id: centos64Guest + datacenter: "{{ (item|basename).split('_')[0] }}" + state: absent + folder: "{{ item|dirname }}" + with_items: "{{ vmlist['json'][0] }}" + register: clone_rph_d1_c1_f0_delete + +- debug: var=clone_rph_d1_c1_f0_delete + +- name: assert that changes were made with deletion + assert: + that: + - "clone_rph_d1_c1_f0_delete.results|map(attribute='changed')|unique|list == [true]" diff --git a/test/integration/targets/vmware_guest/tasks/main.yml b/test/integration/targets/vmware_guest/tasks/main.yml index 2e74786b43..938c5655b6 100644 --- a/test/integration/targets/vmware_guest/tasks/main.yml +++ b/test/integration/targets/vmware_guest/tasks/main.yml @@ -18,3 +18,4 @@ - include: clone_d1_c1_f0.yml - include: create_d1_c1_f0.yml - include: cdrom_d1_c1_f0.yml +- include: create_rp_d1_c1_f0.yml