From a977f3bb3ba60164baa50c556072b3e069064c3f Mon Sep 17 00:00:00 2001 From: "Qi (Keira) Zhang" Date: Tue, 9 Jan 2024 15:33:23 +0800 Subject: [PATCH] Get VM primary mac address after VM power on (#529) Signed-off-by: Qi Zhang --- common/vm_get_vm_info.yml | 3 --- common/vm_wait_primary_nic_mac.yml | 11 +++++++---- env_setup/env_setup.yml | 12 ++++++++++++ linux/deploy_vm/deploy_vm_from_iso.yml | 5 +++++ linux/deploy_vm/deploy_vm_from_ova.yml | 3 +++ windows/deploy_vm/deploy_vm_from_iso.yml | 1 - windows/deploy_vm/deploy_vm_from_ova.yml | 3 +++ 7 files changed, 30 insertions(+), 8 deletions(-) diff --git a/common/vm_get_vm_info.yml b/common/vm_get_vm_info.yml index 6c62d7c05..1636ade33 100644 --- a/common/vm_get_vm_info.yml +++ b/common/vm_get_vm_info.yml @@ -25,9 +25,6 @@ vm_hardware_version_num: "{{ vm_config.config.version.split('-')[-1] }}" vm_firmware: "{{ vm_config.config.firmware }}" -- name: "Wait for mac address is available" - include_tasks: vm_wait_primary_nic_mac.yml - - name: "Display VM's basic information" ansible.builtin.debug: msg: diff --git a/common/vm_wait_primary_nic_mac.yml b/common/vm_wait_primary_nic_mac.yml index 21e3c183c..d7448b0bb 100644 --- a/common/vm_wait_primary_nic_mac.yml +++ b/common/vm_wait_primary_nic_mac.yml @@ -41,7 +41,10 @@ - gather_network_facts.network_data['0'].mac_addr is defined - gather_network_facts.network_data['0'].mac_addr -- name: "Display VM's primary network adapter MAC address" - ansible.builtin.debug: - msg: - - "VM primary network adapter MAC address: {{ vm_primary_nic_mac }}" \ No newline at end of file +- name: "Assert VM primary network adapter has MAC address" + ansible.builtin.assert: + that: + - vm_primary_nic_mac + - vm_primary_nic_mac | ansible.utils.hwaddr + fail_msg: "VM primary network adapter has No MAC address" + success_msg: "VM primary network adapter MAC address is {{ vm_primary_nic_mac }}" diff --git a/env_setup/env_setup.yml b/env_setup/env_setup.yml index c6ad93976..57a500440 100644 --- a/env_setup/env_setup.yml +++ b/env_setup/env_setup.yml @@ -77,6 +77,18 @@ - name: "Get existing VM info" include_tasks: ../common/vm_get_vm_info.yml + + - name: "Get VM's power state" + include_tasks: ../common/vm_get_power_state.yml + + - name: "Power on VM" + include_tasks: ../common/vm_set_power_state.yml + vars: + vm_power_state_set: "powered-on" + when: vm_power_state_get == "poweredOff" + + - name: "Wait for VM primary network adapter has MAC address" + include_tasks: ../common/vm_wait_primary_nic_mac.yml when: vm_exists is defined and vm_exists - name: Set fact of the VM datastore path diff --git a/linux/deploy_vm/deploy_vm_from_iso.yml b/linux/deploy_vm/deploy_vm_from_iso.yml index 7d683efcb..185756260 100644 --- a/linux/deploy_vm/deploy_vm_from_iso.yml +++ b/linux/deploy_vm/deploy_vm_from_iso.yml @@ -201,6 +201,11 @@ vars: vm_take_screenshot_local_path: "{{ current_test_log_folder }}" +# For only ESXi managed VM, its MAC address is generated after power on +# not at VM creation +- name: "Get VM's primary network adapter MAC address" + include_tasks: ../../common/vm_wait_primary_nic_mac.yml + # VMware Photon OS kickstart file can't add shutdown action, # so here needs to shutdown it separately - name: "Shutdown VMware Photon OS VM after auto install completes" diff --git a/linux/deploy_vm/deploy_vm_from_ova.yml b/linux/deploy_vm/deploy_vm_from_ova.yml index b275e27f6..94800632a 100644 --- a/linux/deploy_vm/deploy_vm_from_ova.yml +++ b/linux/deploy_vm/deploy_vm_from_ova.yml @@ -90,6 +90,9 @@ - name: "Add a serial port for VM" include_tasks: ../../common/vm_add_serial_port.yml +- name: "Get VM's primary network adapter MAC address" + include_tasks: ../../common/vm_wait_primary_nic_mac.yml + - name: "Reconfigure VM with cloud-init" include_tasks: reconfigure_vm_with_cloudinit.yml when: ova_guest_os_type in ['photon', 'ubuntu', 'amazon'] diff --git a/windows/deploy_vm/deploy_vm_from_iso.yml b/windows/deploy_vm/deploy_vm_from_iso.yml index fa32a743c..6c40a3708 100644 --- a/windows/deploy_vm/deploy_vm_from_iso.yml +++ b/windows/deploy_vm/deploy_vm_from_iso.yml @@ -108,7 +108,6 @@ - name: "Get VM's primary network adapter MAC address after poweron VM" include_tasks: ../../common/vm_wait_primary_nic_mac.yml - when: vm_primary_nic_mac is undefined or not vm_primary_nic_mac - name: "Get VM IP address" include_tasks: ../../common/vm_get_ip.yml diff --git a/windows/deploy_vm/deploy_vm_from_ova.yml b/windows/deploy_vm/deploy_vm_from_ova.yml index a8040bd6e..3498c4592 100644 --- a/windows/deploy_vm/deploy_vm_from_ova.yml +++ b/windows/deploy_vm/deploy_vm_from_ova.yml @@ -49,6 +49,9 @@ - name: "Get VM info" include_tasks: ../../common/vm_get_vm_info.yml + +#- name: "Get VM's primary network adapter MAC address" +# include_tasks: ../../common/vm_wait_primary_nic_mac.yml # - include_tasks: ../../common/vm_get_ip_from_vmtools.yml # Copy script ConfigureRemotingForAnsible.ps1 to guest OS