From de6b800fa7c6b91c07a7f8d12d66b4248e9599a4 Mon Sep 17 00:00:00 2001 From: Denzil Phillips Date: Wed, 5 Mar 2025 08:53:40 -0500 Subject: [PATCH 1/4] Add user confirmation and warning prompts to replace_vm playbook --- playbooks/utils/replace_vm_host.yml | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/playbooks/utils/replace_vm_host.yml b/playbooks/utils/replace_vm_host.yml index 5b5b111bf1..ece96869f4 100644 --- a/playbooks/utils/replace_vm_host.yml +++ b/playbooks/utils/replace_vm_host.yml @@ -16,6 +16,42 @@ - ../../group_vars/vsphere/{{ runtime_env | default('staging') }}.yml tasks: + # Informational Warning: Backup Reminder + - name: Inform user to backup VM before replacement + ansible.builtin.debug: + msg: "Warning: You are about to replace the VM. Ensure you have backups before proceeding." + + # Critical Check: Ensure VM exists + - name: Check if the VM exists + community.vmware.vmware_vm_info: + hostname: "{{ vcenter_hostname }}" + username: "{{ vcenter_username }}" + password: "{{ vcenter_password }}" + validate_certs: false + vm_name: "{{ replacement_vm }}" + register: old_vm_info + failed_when: old_vm_info.virtual_machines | length == 0 + msg: "Critical: The VM {{ replacement_vm }} was not found. Aborting the replacement process." + + # User Confirmation: Pause to confirm the replacement + - name: Confirm with user before proceeding + pause: + prompt: "This operation will replace the VM. Are you sure you want to proceed? (yes/no)" + register: user_response + + # Abort if user cancels the operation + - name: Fail if user cancels the operation + fail: + msg: "Aborted by user. VM replacement cancelled." + when: user_response.user_input != 'yes' + + # Proceed with VM replacement if user confirms + - name: Proceed with VM replacement + ansible.builtin.debug: + msg: "Proceeding with VM replacement..." + when: user_response.user_input == 'yes' + + # Gather info on the VM to replace - name: Gather info on VM to replace community.vmware.vmware_vm_info: hostname: "{{ vcenter_hostname }}" @@ -29,10 +65,12 @@ vm_name: "{{ replacement_vm }}" register: old_vm_info + # Print out old VM information - name: Print out old VM information ansible.builtin.debug: var: old_vm_info + # Set VM network based on the IP address (existing logic) - name: set the vm network to the private network if the IP starts with 172.20 ansible.builtin.set_fact: vm_network: "VM Network - ip4-library-servers" @@ -43,10 +81,12 @@ vm_network: "Virtual Machine Network" when: old_vm_info.virtual_machines[0].ip_address is match("128.112.*") + # Print out warning message about the VM replacement - name: Print out warning ansible.builtin.debug: msg: Ansible will now move and power off the current {{ old_vm_info.virtual_machines[0].guest_name }} VM, then create a replacement in the {{ vm_network }} network. + # Move old VM to the ToBeDeleted folder - name: Move old VM to the ToBeDeleted folder community.vmware.vmware_guest_move: hostname: "{{ vcenter_hostname }}" @@ -57,6 +97,7 @@ dest_folder: "{{ vm_delete_me_folder }}" uuid: "{{ old_vm_info.virtual_machines[0].uuid }}" + # Power off old VM using UUID - name: Power off old VM using UUID community.vmware.vmware_guest_powerstate: hostname: "{{ vcenter_hostname }}" @@ -67,6 +108,7 @@ uuid: "{{ old_vm_info.virtual_machines[0].uuid }}" state: powered-off + # Create a new virtual machine from a template - name: Create a new virtual machine from a template community.vmware.vmware_guest: hostname: "{{ vcenter_hostname }}" From 1b34cb8bf2d52058ee8bc3b2aa651bd55b739f97 Mon Sep 17 00:00:00 2001 From: Denzil Phillips Date: Wed, 5 Mar 2025 13:37:28 -0500 Subject: [PATCH 2/4] Add warnings, checks, and user confirmation to replace_vm_host playbook --- playbooks/utils/replace_vm_host.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/playbooks/utils/replace_vm_host.yml b/playbooks/utils/replace_vm_host.yml index ece96869f4..39d50c079f 100644 --- a/playbooks/utils/replace_vm_host.yml +++ b/playbooks/utils/replace_vm_host.yml @@ -30,8 +30,12 @@ validate_certs: false vm_name: "{{ replacement_vm }}" register: old_vm_info - failed_when: old_vm_info.virtual_machines | length == 0 + + # Fail if the VM does not exist + - name: Fail if the VM does not exist + fail: msg: "Critical: The VM {{ replacement_vm }} was not found. Aborting the replacement process." + when: old_vm_info.virtual_machines | length == 0 # User Confirmation: Pause to confirm the replacement - name: Confirm with user before proceeding From 4e8919ac17ee658194fd276f139a3d0b0dda8d1a Mon Sep 17 00:00:00 2001 From: Denzil Phillips Date: Wed, 5 Mar 2025 13:52:11 -0500 Subject: [PATCH 3/4] Add warnings, checks, and user confirmation to replace_vm_host playbook --- playbooks/utils/replace_vm_host.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/playbooks/utils/replace_vm_host.yml b/playbooks/utils/replace_vm_host.yml index 39d50c079f..00ea735df2 100644 --- a/playbooks/utils/replace_vm_host.yml +++ b/playbooks/utils/replace_vm_host.yml @@ -31,7 +31,6 @@ vm_name: "{{ replacement_vm }}" register: old_vm_info - # Fail if the VM does not exist - name: Fail if the VM does not exist fail: msg: "Critical: The VM {{ replacement_vm }} was not found. Aborting the replacement process." From 64290612ba13da93f58f325ceb00bbc778c6f54d Mon Sep 17 00:00:00 2001 From: Denzil Phillips Date: Wed, 5 Mar 2025 14:00:22 -0500 Subject: [PATCH 4/4] Add warnings, checks, and user confirmation to replace_vm_host playbook --- playbooks/utils/replace_vm_host.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playbooks/utils/replace_vm_host.yml b/playbooks/utils/replace_vm_host.yml index 00ea735df2..627b76c977 100644 --- a/playbooks/utils/replace_vm_host.yml +++ b/playbooks/utils/replace_vm_host.yml @@ -33,7 +33,7 @@ - name: Fail if the VM does not exist fail: - msg: "Critical: The VM {{ replacement_vm }} was not found. Aborting the replacement process." + msg: "Critical: The VM {{ replacement_vm }} was not found. Aborting the replacement process." when: old_vm_info.virtual_machines | length == 0 # User Confirmation: Pause to confirm the replacement