Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prepare_vmware_tests: use module_defaults #63209

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/docsite/rst/dev_guide/platforms/vmware_guidelines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,41 @@ VM names should be predictable
If you need to create a new VM during your test, you can use ``test_vm1``, ``test_vm2`` or ``test_vm3``. This
way it will be automatically clean up for you.

Avoid the common boiler plate code in your test playbook
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

From Ansible 2.10, the test suite uses `modules_defaults`. This module
allow us to preinitialize the following default keys of the VMware modules:

- hostname
Akasurde marked this conversation as resolved.
Show resolved Hide resolved
- username
- password
- validate_certs

For example, the following block:

.. code-block:: yaml

- name: Add a VMware vSwitch
vmware_vswitch:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: 'no'
esxi_hostname: 'esxi1'
switch_name: "boby"
state: present

should be simplified to just:

.. code-block:: yaml

- name: Add a VMware vSwitch
vmware_vswitch:
Akasurde marked this conversation as resolved.
Show resolved Hide resolved
esxi_hostname: 'esxi1'
switch_name: "boby"
state: present


Typographic convention
======================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@

- name: set state to poweroff on all VMs
vmware_guest:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
name: "{{ item.name }}"
state: poweredoff
with_items: "{{ virtual_machines + virtual_machines_in_cluster }}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
- name: Add ESXi Hosts to vCenter
vmware_host:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: '{{ dc1 }}'
cluster_name: '{{ ccr1 }}'
esxi_hostname: '{{ item }}'
esxi_username: '{{ esxi_user }}'
esxi_password: '{{ esxi_password }}'
state: present
validate_certs: no
with_items: "{{ esxi_hosts }}"

- name: Disable the Maintenance Mode
vmware_maintenancemode:
validate_certs: no
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: '{{ item }}'
state: absent
with_items: "{{ esxi_hosts }}"
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
- name: Create a category for cluster
vmware_category:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: False
category_name: '{{ cluster_category }}'
category_description: '{{ cluster_category }} description'
state: present
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
- name: Create Cluster
vmware_cluster:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: '{{ dc1 }}'
cluster_name: '{{ ccr1 }}'
validate_certs: no

- name: Enable DRS on Cluster
vmware_cluster_drs:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: '{{ dc1 }}'
cluster_name: '{{ ccr1 }}'
enable_drs: yes
validate_certs: no
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
- name: Create Content Library
vmware_content_library_manager:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
library_name: test-content-lib
library_description: 'Library created by the prepare_vmware_tests role'
library_type: local
datastore_name: '{{ ds2 }}'
validate_certs: False
state: present
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
- name: Create Datacenter
vmware_datacenter:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: '{{ dc1 }}'
state: present
validate_certs: no

- name: Create a VM folder on given Datacenter
vcenter_folder:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: '{{ dc1 }}'
folder_name: '{{ f0 }}'
folder_type: vm
state: present
validate_certs: no
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
- name: The vcenter needs a bit of time to refresh the DS list
vmware_datastore_info:
validate_certs: false
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
cluster: '{{ ccr1 }}'
register: setup_datastore_datatstore_info
failed_when: setup_datastore_datatstore_info.datastores|selectattr('type', 'equalto', 'NFS')|list|length != 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
---
- name: Create the DVSwitch
vmware_dvswitch:
validate_certs: no
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter_name: '{{ dc1 }}'
switch_name: '{{ dvswitch1 }}'
switch_version: 6.0.0
Expand All @@ -15,9 +11,6 @@
state: present
- name: Attach the hosts to the DVSwitch
vmware_dvs_host:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
esxi_hostname: "{{ item }}"
switch_name: '{{ dvswitch1 }}'
vmnics:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
---
- name: Add resource pool to vCenter
vmware_resource_pool:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter: '{{ dc1 }}'
cluster: '{{ ccr1 }}'
resource_pool: DC0_C0_RP1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
---
- name: Add a VMware vSwitchs
vmware_vswitch:
validate_certs: no
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
esxi_hostname: '{{ item }}'
switch_name: "{{ switch1 }}"
state: present
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
- name: Get Category facts
vmware_category_info:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: False
register: cat_info

- name: Get Category id for {{ cluster_category }}
Expand All @@ -16,10 +12,6 @@

- name: Create a tag for cluster
vmware_tag:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: False
category_id: '{{ cluster_category_id }}'
tag_name: '{{ cluster_tag }}'
tag_description: '{{ cluster_tag }} Description'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
---
- name: Create VMs
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
validate_certs: no
folder: '{{ item.folder }}'
name: '{{ item.name }}'
state: poweredoff
Expand All @@ -28,11 +24,7 @@

- name: Create VMs in cluster
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter: "{{ dc1 }}"
validate_certs: no
folder: '{{ item.folder }}'
cluster: '{{ item.cluster }}'
name: '{{ item.name }}'
Expand Down
27 changes: 0 additions & 27 deletions test/integration/targets/prepare_vmware_tests/tasks/teardown.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
---
- name: Remove the VM prepared by prepare_vmware_tests
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
name: "{{ item.name }}"
force: yes
state: absent
with_items: '{{ virtual_machines + virtual_machines_in_cluster }}'

- name: Remove the test_vm* VMs
vmware_guest:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: no
name: "{{ item }}"
force: yes
state: absent
Expand All @@ -26,10 +18,6 @@

- name: Remove the DVSwitch
vmware_dvswitch:
validate_certs: no
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
datacenter_name: '{{ dc1 }}'
state: absent
switch_name: '{{ item }}'
Expand All @@ -41,7 +29,6 @@

- name: Remove the vSwitches
vmware_vswitch:
validate_certs: no
hostname: '{{ item }}'
username: '{{ esxi_user }}'
password: '{{ esxi_password }}'
Expand All @@ -52,16 +39,12 @@

- name: Remove ESXi Hosts to vCenter
vmware_host:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: '{{ dc1 }}'
cluster_name: ccr1
esxi_hostname: '{{ item }}'
esxi_username: '{{ esxi_user }}'
esxi_password: '{{ esxi_password }}'
state: absent
validate_certs: no
with_items: "{{ esxi_hosts }}"
ignore_errors: yes

Expand All @@ -73,7 +56,6 @@
esxi_hostname: '{{ item }}' # Won't be necessary with https://github.com/ansible/ansible/pull/56516
datastore_name: '{{ ds1 }}'
state: absent
validate_certs: no
with_items: "{{ esxi_hosts }}"

- name: Umount NFS datastores to ESXi (2/2)
Expand All @@ -84,15 +66,10 @@
esxi_hostname: '{{ item }}' # Won't be necessary with https://github.com/ansible/ansible/pull/56516
datastore_name: '{{ ds2 }}'
state: absent
validate_certs: no
with_items: "{{ esxi_hosts }}"

- name: Delete a datastore cluster to datacenter
vmware_datastore_cluster:
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
validate_certs: no
datacenter_name: "{{ dc1 }}"
datastore_cluster_name: '{{ item }}'
state: absent
Expand All @@ -103,10 +80,6 @@

- name: Remove the datacenter
vmware_datacenter:
validate_certs: no
hostname: '{{ vcenter_hostname }}'
username: '{{ vcenter_username }}'
password: '{{ vcenter_password }}'
datacenter_name: '{{ item }}'
state: absent
when: vcsim is not defined
Expand Down
8 changes: 8 additions & 0 deletions test/lib/ansible_test/_internal/cloud/vcenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,12 @@ def get_environment_config(self):
return CloudEnvironmentConfig(
env_vars=env_vars,
ansible_vars=ansible_vars,
module_defaults={
'group/vmware': {
'hostname': env_vars['VCENTER_HOSTNAME'],
'username': env_vars['VCENTER_USERNAME'],
'password': env_vars['VCENTER_PASSWORD'],
'validate_certs': env_vars.get('VMWARE_VALIDATE_CERTS', 'no'),
},
},
)