|
| 1 | +.. _ansible_collections.community.vmware.docsite.vmware_ansible: |
| 2 | + |
| 3 | +**************************************** |
| 4 | +Guidelines for VMware module development |
| 5 | +**************************************** |
| 6 | + |
| 7 | +The Ansible VMware collection (on `Galaxy <https://galaxy.ansible.com/community/vmware>`_, source code `repository <https://github.com/ansible-collections/community.vmware>`_) is maintained by the VMware Working Group. For more information see the `team community page <https://github.com/ansible/community/wiki/VMware>`_. |
| 8 | + |
| 9 | +.. contents:: |
| 10 | + :local: |
| 11 | + |
| 12 | +Testing with your own infrastructure |
| 13 | +==================================== |
| 14 | + |
| 15 | +You can target a regular VMware environment. This paragraph explains step by step how you can run the test-suite yourself. |
| 16 | + |
| 17 | +Requirements |
| 18 | +------------ |
| 19 | + |
| 20 | +- 2 ESXi hosts (6.5 or 6.7) |
| 21 | + - with 2 NIC, the second ones should be available for the test |
| 22 | +- a VCSA host |
| 23 | +- a NFS server |
| 24 | +- Python dependencies: |
| 25 | + - `pyvmomi <https://github.com/vmware/pyvmomi/tree/master/pyVmomi>`_ |
| 26 | + - `requests <https://2.python-requests.org/en/master/>`_ |
| 27 | + |
| 28 | +If you want to deploy your test environment in a hypervisor, both `VMware or Libvirt <https://github.com/goneri/vmware-on-libvirt>`_ work well. |
| 29 | + |
| 30 | +NFS server configuration |
| 31 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 32 | + |
| 33 | +Your NFS server must expose the following directory structure: |
| 34 | + |
| 35 | +.. code-block:: shell |
| 36 | +
|
| 37 | + $ tree /srv/share/ |
| 38 | + /srv/share/ |
| 39 | + ├── isos |
| 40 | + │ ├── base.iso |
| 41 | + │ ├── centos.iso |
| 42 | + │ └── fedora.iso |
| 43 | + └── vms |
| 44 | + 2 directories, 3 files |
| 45 | +
|
| 46 | +On a Linux system, you can expose the directory over NFS with the following export file: |
| 47 | + |
| 48 | +.. code-block:: shell |
| 49 | +
|
| 50 | + $ cat /etc/exports |
| 51 | + /srv/share 192.168.122.0/255.255.255.0(rw,anonuid=1000,anongid=1000) |
| 52 | +
|
| 53 | +.. note:: |
| 54 | + |
| 55 | + With this configuration all the new files will be owned by the user with the UID and GID 1000/1000. |
| 56 | + Adjust the configuration to match your user's UID/GID. |
| 57 | + |
| 58 | +The service can be enabled with: |
| 59 | + |
| 60 | +.. code-block:: shell |
| 61 | +
|
| 62 | + $ sudo systemctl enable --now nfs-server |
| 63 | +
|
| 64 | +
|
| 65 | +Configure your installation |
| 66 | +--------------------------- |
| 67 | + |
| 68 | +Prepare a configuration file that describes your set-up. The file |
| 69 | +should be called :file:`test/integration/cloud-config-vcenter.ini` and based on |
| 70 | +:file:`test/lib/ansible_test/config/cloud-config-vcenter.ini.template`. For instance, if you have deployed your lab with |
| 71 | +`vmware-on-libvirt <https://github.com/goneri/vmware-on-libvirt>`_: |
| 72 | + |
| 73 | +.. code-block:: ini |
| 74 | +
|
| 75 | + [DEFAULT] |
| 76 | + vcenter_username: administrator@vsphere.local |
| 77 | + vcenter_password: !234AaAa56 |
| 78 | + vcenter_hostname: vcenter.test |
| 79 | + vmware_validate_certs: false |
| 80 | + esxi1_hostname: esxi1.test |
| 81 | + esxi1_username: root |
| 82 | + esxi1_password: root |
| 83 | + esxi2_hostname: test2.test |
| 84 | + esxi2_username: root |
| 85 | + esxi2_password: root |
| 86 | +
|
| 87 | +Using an HTTP proxy |
| 88 | +------------------- |
| 89 | +Hosting test infrastructure behind an HTTP proxy is supported. You can specify the location of the proxy server with the two extra keys: |
| 90 | + |
| 91 | +.. code-block:: ini |
| 92 | +
|
| 93 | + vmware_proxy_host: esxi1-gw.ws.testing.ansible.com |
| 94 | + vmware_proxy_port: 11153 |
| 95 | +
|
| 96 | +In addition, you may need to adjust the variables of the following `var files <https://github.com/ansible-collections/community.vmware/tree/main/tests/integration/targets/prepare_vmware_tests/vars>`_ to match the configuration of your lab. If you use vmware-on-libvirt to prepare your lab, you do not have anything to change. |
| 97 | + |
| 98 | +Run the test-suite |
| 99 | +------------------ |
| 100 | + |
| 101 | +Once your configuration is ready, you can trigger a run with the following command: |
| 102 | + |
| 103 | +.. code-block:: shell |
| 104 | +
|
| 105 | + source hacking/env-setup |
| 106 | + VMWARE_TEST_PLATFORM=static ansible-test integration --python 3.7 vmware_host_firewall_manager |
| 107 | +
|
| 108 | +``vmware_host_firewall_manager`` is the name of the module to test. |
| 109 | + |
| 110 | +``vmware_guest`` is much larger than any other test role and is rather slow. You can enable or disable some of its test playbooks in `main.yml <https://github.com/ansible-collections/community.vmware/tree/main/tests/integration/targets/vmware_guest/defaults/main.yml>`_. |
| 111 | + |
| 112 | + |
| 113 | +Unit-test |
| 114 | +========= |
| 115 | + |
| 116 | +The VMware modules have limited unit-test coverage. You can run the test suite with the |
| 117 | +following commands: |
| 118 | + |
| 119 | +.. code-block:: shell |
| 120 | +
|
| 121 | + source hacking/env-setup |
| 122 | + ansible-test units --venv --python 3.7 '.*vmware.*' |
| 123 | +
|
| 124 | +Code style and best practice |
| 125 | +============================ |
| 126 | + |
| 127 | +datacenter argument with ESXi |
| 128 | +----------------------------- |
| 129 | + |
| 130 | +The ``datacenter`` parameter should not use ``ha-datacenter`` by default. This is because the user may |
| 131 | +not realize that Ansible silently targets the wrong data center. |
| 132 | + |
| 133 | +esxi_hostname should not be mandatory |
| 134 | +------------------------------------- |
| 135 | + |
| 136 | +Depending upon the functionality provided by ESXi or vCenter, some modules can seamlessly work with both. In this case, |
| 137 | +``esxi_hostname`` parameter should be optional. |
| 138 | + |
| 139 | +.. code-block:: python |
| 140 | +
|
| 141 | + if self.is_vcenter(): |
| 142 | + esxi_hostname = module.params.get('esxi_hostname') |
| 143 | + if not esxi_hostname: |
| 144 | + self.module.fail_json("esxi_hostname parameter is mandatory") |
| 145 | + self.host = self.get_all_host_objs(cluster_name=cluster_name, esxi_host_name=esxi_hostname)[0] |
| 146 | + else: |
| 147 | + self.host = find_obj(self.content, [vim.HostSystem], None) |
| 148 | + if self.host is None: |
| 149 | + self.module.fail_json(msg="Failed to find host system.") |
| 150 | +
|
| 151 | +Example should use the fully qualified collection name (FQCN) |
| 152 | +------------------------------------------------------------- |
| 153 | + |
| 154 | +Use FQCN for examples within module documentation. For instance, you should use ``community.vmware.vmware_guest`` instead of just |
| 155 | +``vmware_guest``. |
| 156 | + |
| 157 | +This way, the examples do not depend on the ``collections`` directive of the |
| 158 | +playbook. |
| 159 | + |
| 160 | +Functional tests |
| 161 | +---------------- |
| 162 | + |
| 163 | +Writing new tests |
| 164 | +^^^^^^^^^^^^^^^^^ |
| 165 | + |
| 166 | +If you are writing a new collection of integration tests, there are a few VMware-specific things to note beyond |
| 167 | +the standard Ansible :ref:`integration testing<testing_integration>` process. |
| 168 | + |
| 169 | +The test-suite uses a set of common, pre-defined vars located `in prepare_vmware_tests <https://github.com/ansible-collections/community.vmware/tree/main/tests/integration/targets/test/integration/targets/prepare_vmware_tests/>`_ role. |
| 170 | +The resources defined there are automatically created by importing that role at the start of your test: |
| 171 | + |
| 172 | +.. code-block:: yaml |
| 173 | +
|
| 174 | + - import_role: |
| 175 | + name: prepare_vmware_tests |
| 176 | + vars: |
| 177 | + setup_datacenter: true |
| 178 | +
|
| 179 | +This will give you a ready to use cluster, datacenter, datastores, folder, switch, dvswitch, ESXi hosts, and VMs. |
| 180 | + |
| 181 | +No need to create too much resources |
| 182 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 183 | + |
| 184 | +Most of the time, it's not necessary to use ``with_items`` to create multiple resources. By avoiding it, |
| 185 | +you speed up the test execution and you simplify the clean up afterwards. |
| 186 | + |
| 187 | +VM names should be predictable |
| 188 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 189 | + |
| 190 | +If you need to create a new VM during your test, you can use ``test_vm1``, ``test_vm2`` or ``test_vm3``. This |
| 191 | +way it will be automatically clean up for you. |
| 192 | + |
| 193 | +Avoid the common boiler plate code in your test playbook |
| 194 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 195 | + |
| 196 | +From Ansible 2.10, the test suite uses `modules_defaults`. This module |
| 197 | +allow us to preinitialize the following default keys of the VMware modules: |
| 198 | + |
| 199 | +- hostname |
| 200 | +- username |
| 201 | +- password |
| 202 | +- validate_certs |
| 203 | + |
| 204 | +For example, the following block: |
| 205 | + |
| 206 | +.. code-block:: yaml |
| 207 | +
|
| 208 | + - name: Add a VMware vSwitch |
| 209 | + community.vmware.vmware_vswitch: |
| 210 | + hostname: '{{ vcenter_hostname }}' |
| 211 | + username: '{{ vcenter_username }}' |
| 212 | + password: '{{ vcenter_password }}' |
| 213 | + validate_certs: 'no' |
| 214 | + esxi_hostname: 'esxi1' |
| 215 | + switch_name: "boby" |
| 216 | + state: present |
| 217 | +
|
| 218 | +should be simplified to just: |
| 219 | + |
| 220 | +.. code-block:: yaml |
| 221 | +
|
| 222 | + - name: Add a VMware vSwitch |
| 223 | + community.vmware.vmware_vswitch: |
| 224 | + esxi_hostname: 'esxi1' |
| 225 | + switch_name: "boby" |
| 226 | + state: present |
| 227 | +
|
| 228 | +
|
| 229 | +Typographic convention |
| 230 | +====================== |
| 231 | + |
| 232 | +Nomenclature |
| 233 | +------------ |
| 234 | + |
| 235 | +We try to enforce the following rules in our documentation: |
| 236 | + |
| 237 | +- VMware, not VMWare or vmware |
| 238 | +- ESXi, not esxi or ESXI |
| 239 | +- vCenter, not vcenter or VCenter |
0 commit comments