ansible-collections / community.vmware

Ansible Collection for VMware
GNU General Public License v3.0
344 stars 336 forks source link

vmware_guest module no longer converts VM to a template via is_template option and also does not customise #1693

Open deepakramanath opened 1 year ago

deepakramanath commented 1 year ago
SUMMARY

Up until Ansible 2.9.x version, the module vmware_guest could be used to set network properties on a VM deployed using an live ISO file and subsequently use the same module to convert to a vmware template. While the parameters have not changed in the recent Ansible, the module does not work as intended. This is such an inconvenient thing to happen provided the module parameters are still supported.

ISSUE TYPE

Bug Report

COMPONENT NAME

community.vmware.vmware_guest

ANSIBLE VERSION
ansible [core 2.13.3]
  config file = /home/ansible/gateway-build/black/ansible.cfg
  configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /home/ansible/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.14 (main, Jan  9 2023, 00:00:00) [GCC 11.3.1 20220421 (Red Hat 11.3.1-2)]
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
# /usr/lib/python3.9/site-packages/ansible_collections

Collection       Version
--------------   --------
community.vmware 2.8.0
CONFIGURATION
DEFAULT_ASK_PASS(/home/ansible/ansible.cfg) = False
DEFAULT_HOST_LIST(/home/ansible/ansible.cfg) = ['/home/ansible/inventory']
DEFAULT_REMOTE_PORT(/home/ansible/ansible.cfg) = 22
DEFAULT_REMOTE_USER(/home/ansible/ansible.cfg) = ansible
DEFAULT_ROLES_PATH(/home/ansible/ansible.cfg) = ['/home/ansible/roles']
DEFAULT_STDOUT_CALLBACK(/home/ansible/ansible.cfg) = default
DEFAULT_TIMEOUT(/home/ansible/ansible.cfg) = 240
DISPLAY_SKIPPED_HOSTS(/home/ansible/ansible.cfg) = True
HOST_KEY_CHECKING(/home/ansible/ansible.cfg) = False

CALLBACK:
========

default:
_______
display_skipped_hosts(/home/ansible/ansible.cfg) = True

CONNECTION:
==========

paramiko_ssh:
____________
host_key_checking(/home/ansible/ansible.cfg) = False
remote_user(/home/ansible/ansible.cfg) = ansible

ssh:
___
host_key_checking(/home/ansible/ansible.cfg) = False
port(/home/ansible/ansible.cfg) = 22
remote_user(/home/ansible/ansible.cfg) = ansible
timeout(/home/ansible/ansible.cfg) = 240
OS / ENVIRONMENT
NAME="Red Hat Enterprise Linux"
VERSION="9.1 (Plow)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="9.1"
PLATFORM_ID="platform:el9"
PRETTY_NAME="Red Hat Enterprise Linux 9.1 (Plow)"
ANSI_COLOR="0;31"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:redhat:enterprise_linux:9::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/9/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"

REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 9"
REDHAT_BUGZILLA_PRODUCT_VERSION=9.1
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.1"
STEPS TO REPRODUCE
- name: ISO minimal install
  community.vmware.vmware_guest:
    hostname: "{{ vcenter_host }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: no
    folder: "/{{ vcenter_datacenter}}/vm/{{ template_dir }}/"
    name: "{{ redhat_template }}"
    is_template: no
    state: poweredon
    guest_id: "{{ redhat_guest_id }}"
    cluster: "{{ vcenter_cluster }}"
    datacenter: "{{ vcenter_datacenter }}"
    disk:
    - size_mb: "{{ vmware_virtual_disk_size }}"
      type: "{{ redhat_disk_type }}"
      datastore: "{{ vcenter_datastore }}"
    hardware:
      memory_mb: "{{ redhat_memory }}"
      num_cpus: "{{ redhat_CPU }}"
      scsi: "{{ redhat_CPU_scsi }}"
    networks:
    - name: "{{ vcenter_network }}"
      type: static
      device_type: vmxnet3
    customization:
      existing_vm: yes
    wait_for_customization: false
    cdrom:
      type: iso
      iso_path: "[{{ vcenter_datastore }}] /{{ iso_dir }}/{{ redhat_iso_name }}"
  delegate_to: localhost
    - create_minimal_iso

- name: Wait for the Redhat minimal VM to deploy
  ansible.builtin.wait_for:
    timeout: 600
  delegate_to: localhost

- name: Power down the Redhat minimal VM for customization
  community.vmware.vmware_guest_powerstate:
    hostname: "{{ vcenter_host }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: no
    name: "{{ redhat_template }}"
    state: powered-off
  delegate_to: localhost

- name: Customise VM (Change network properties)
  community.vmware.vmware_guest:
    hostname: "{{ vcenter_host }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: no
    folder: "/{{ vcenter_datacenter}}/vm/{{ template_dir }}/"
    name: "{{ redhat_template }}"
    cluster: "{{ vcenter_cluster }}"
    datacenter: "{{ vcenter_datacenter }}"
    state: present
    disk:
    - size_mb: "{{ vmware_virtual_disk_size }}"
      type: "{{ redhat_disk_type }}"
      datastore: "{{ vcenter_datastore }}"
    networks:
    - name: "{{ vcenter_network }}"
      type: static
      ip: "{{ ansible_host }}"
      netmask: "{{ redhat_template_netmask }}"
      gateway: "{{ redhat_template_gateway }}"
      start_connected: yes
      device_type: vmxnet3
    wait_for_ip_address: true
    customization:
      existing_vm: yes
  delegate_to: localhost

- name: Template the VM
  community.vmware.vmware_guest:
    hostname: "{{ vcenter_host }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    name: "{{ datastore_facts_redhat_templates.instance.hw_name }}"
    validate_certs: no
    folder: "/{{ vcenter_datacenter}}/vm/{{ template_dir }}/"
    is_template: true
  delegate_to: localhost
EXPECTED RESULTS

I expect the module to work, particularly the customisation part and the conversion of VM to a template.

ACTUAL RESULTS
This is the output of customisation

fatal: [vcenter_mgmt -> localhost({{ }})]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "advanced_settings": [],
            "annotation": null,
            "cdrom": [],
            "cluster": "Mgmt_Cluster",
            "convert": null,
            "customization": {
                "autologon": null,
                "autologoncount": null,
                "dns_servers": null,
                "dns_suffix": null,
                "domain": null,
                "domainadmin": null,
                "domainadminpassword": null,
                "existing_vm": true,
                "fullname": null,
                "hostname": null,
                "hwclockUTC": null,
                "joindomain": null,
                "joinworkgroup": null,
                "orgname": null,
                "password": null,
                "productid": null,
                "runonce": null,
                "timezone": null
            },
            "customization_spec": null,
            "customvalues": [],
            "datacenter": "Datacenter",
            "datastore": null,
            "delete_from_inventory": false,
            "disk": [
                {
                    "autoselect_datastore": null,
                    "controller_number": null,
                    "controller_type": null,
                    "datastore": "Datastore",
                    "disk_mode": null,
                    "filename": null,
                    "size": null,
                    "size_gb": null,
                    "size_kb": null,
                    "size_mb": 93696,
                    "size_tb": null,
                    "type": "thin",
                    "unit_number": null
                }
            ],
            "esxi_hostname": null,
            "folder": "/Datacenter/vm/Templates/",
            "force": false,
            "guest_id": null,
            "hardware": {
                "boot_firmware": null,
                "cpu_limit": null,
                "cpu_reservation": null,
                "hotadd_cpu": null,
                "hotadd_memory": null,
                "hotremove_cpu": null,
                "iommu": null,
                "max_connections": null,
                "mem_limit": null,
                "mem_reservation": null,
                "memory_mb": null,
                "memory_reservation_lock": null,
                "nested_virt": null,
                "num_cpu_cores_per_socket": null,
                "num_cpus": null,
                "scsi": null,
                "secure_boot": null,
                "version": null,
                "virt_based_security": null
            },
            "hostname": "10.10.10.10",
            "is_template": false,
            "linked_clone": false,
            "name": "RedhatTemplate",
            "name_match": "first",
            "networks": [
                {
                    "device_type": "vmxnet3",
                    "gateway": "10.10.10.1",
                    "ip": "10.10.10.20",
                    "name": "Network",
                    "netmask": "255.255.255.0",
                    "start_connected": true,
                    "type": "static"
                }
            ],
            "nvdimm": {
                "label": null,
                "size_mb": 1024,
                "state": null
            },
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": 443,
            "proxy_host": null,
            "proxy_port": null,
            "resource_pool": null,
            "snapshot_src": null,
            "state": "present",
            "state_change_timeout": 0,
            "template": null,
            "use_instance_uuid": false,
            "username": "user@vsphere.local",
            "uuid": null,
            "validate_certs": false,
            "vapp_properties": [],
            "wait_for_customization": false,
            "wait_for_customization_timeout": 3600,
            "wait_for_ip_address": true,
            "wait_for_ip_address_timeout": 300
        }
    },
    "msg": "guest_id attribute is mandatory for VM creation"

This is the output of template creation task

fatal: [vcenter_mgmt -> localhost({{ }})]: FAILED! => {
    "changed": false,
    "invocation": {
        "module_args": {
            "advanced_settings": [],
            "annotation": null,
            "cdrom": [],
            "cluster": null,
            "convert": null,
            "customization": {
                "autologon": null,
                "autologoncount": null,
                "dns_servers": null,
                "dns_suffix": null,
                "domain": null,
                "domainadmin": null,
                "domainadminpassword": null,
                "existing_vm": null,
                "fullname": null,
                "hostname": null,
                "hwclockUTC": null,
                "joindomain": null,
                "joinworkgroup": null,
                "orgname": null,
                "password": null,
                "productid": null,
                "runonce": null,
                "timezone": null
            },
            "customization_spec": null,
            "customvalues": [],
            "datacenter": "ha-datacenter",
            "datastore": null,
            "delete_from_inventory": false,
            "disk": [],
            "esxi_hostname": null,
            "folder": "/Datacenter/vm/Templates/",
            "force": false,
            "guest_id": null,
            "hardware": {
                "boot_firmware": null,
                "cpu_limit": null,
                "cpu_reservation": null,
                "hotadd_cpu": null,
                "hotadd_memory": null,
                "hotremove_cpu": null,
                "iommu": null,
                "max_connections": null,
                "mem_limit": null,
                "mem_reservation": null,
                "memory_mb": null,
                "memory_reservation_lock": null,
                "nested_virt": null,
                "num_cpu_cores_per_socket": null,
                "num_cpus": null,
                "scsi": null,
                "secure_boot": null,
                "version": null,
                "virt_based_security": null
            },
            "hostname": "10.10.10.20",
            "is_template": true,
            "linked_clone": false,
            "name": "RedhatTemplate",
            "name_match": "first",
            "networks": [],
            "nvdimm": {
                "label": null,
                "size_mb": 1024,
                "state": null
            },
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "port": 443,
            "proxy_host": null,
            "proxy_port": null,
            "resource_pool": null,
            "snapshot_src": null,
            "state": "present",
            "state_change_timeout": 0,
            "template": null,
            "use_instance_uuid": false,
            "username": "user@vsphere.local",
            "uuid": null,
            "validate_certs": false,
            "vapp_properties": [],
            "wait_for_customization": false,
            "wait_for_customization_timeout": 3600,
            "wait_for_ip_address": false,
            "wait_for_ip_address_timeout": 300
        }
    },
    "msg": "No datacenter named ha-datacenter was found"
}
deepakramanath commented 1 year ago

Adding to the above bug report, the official documentation, vmware-guest-module, says, the following for is_template parameter

Flag the instance as a template.

This will mark the given virtual machine as template.

Note, this may need to be done in a dedicated task invocation that is not making any other changes. For example, user cannot change the state from powered-on to powered-off AND save as template in the same task.

See [community.vmware.vmware_guest](https://docs.ansible.com/ansible/latest/collections/community/vmware/vmware_guest_module.html#ansible-collections-community-vmware-vmware-guest-module) source for more details.

Choices:

false ← (default)

true
mariolenz commented 1 year ago
# /usr/lib/python3.9/site-packages/ansible_collections
Collection          Version
---------------------  -------------
community.general  5.5.0

I think the issue / bug report template says something like:

Paste verbatim output from "ansible-galaxy collection list <namespace>.<collection>"  between the quotes
for example: ansible-galaxy collection list community.general

The template is used by several collections / repositories, so there's just an example. Since this is the community.vmware collection, it would be much more important to know this version; the community.general version isn't really helpful here ;-)

deepakramanath commented 1 year ago

Sorry. I have modified the original bug report to include the correct community.vmware module version

Ardneliahs commented 2 months ago

Facing the same problem, cannot turn a vm into a template.