Ansible-Bootstrap/roles/cleanup/tasks/main.yml

79 lines
2.3 KiB
YAML
Raw Normal View History

2024-07-11 22:20:45 +02:00
---
2024-03-19 23:02:50 +01:00
- name: Setup Cleanup
when: hypervisor == "proxmox"
delegate_to: localhost
become: false
block:
- name: Cleanup Setup Disks
community.general.proxmox_disk:
api_host: "{{ hypervisor_url }}"
api_user: "{{ hypervisor_username }}"
api_password: "{{ hypervisor_password }}"
name: "{{ hostname }}"
vmid: "{{ vm_id }}"
disk: "{{ item }}"
state: absent
loop:
- ide0
2024-10-30 00:29:46 +01:00
- ide2
2024-03-19 23:02:50 +01:00
- name: Remove CD-ROM from VM in vCenter
when: hypervisor == "vmware"
delegate_to: localhost
2024-10-28 18:56:00 +01:00
failed_when: false
2024-07-11 22:20:45 +02:00
community.vmware.vmware_guest:
2024-03-19 23:02:50 +01:00
hostname: "{{ hypervisor_url }}"
username: "{{ hypervisor_username }}"
password: "{{ hypervisor_password }}"
2024-07-11 22:09:58 +02:00
validate_certs: false
2024-03-19 23:02:50 +01:00
datacenter: "{{ hypervisor_cluster }}"
name: "{{ hostname }}"
cdrom:
- controller_number: 0
2024-10-30 00:29:46 +01:00
unit_number: 1
2024-07-11 22:20:45 +02:00
controller_type: sata
2024-03-19 23:02:50 +01:00
type: iso
iso_path: "{{ boot_iso }}"
state: absent
- name: Remove Archiso and cloud-init disks
when: hypervisor == "libvirt"
delegate_to: localhost
become: false
block:
- name: Stop the VM
community.libvirt.virt:
name: "{{ hostname }}"
state: shutdown
- name: Remove cloud-init disk
2024-07-11 22:20:45 +02:00
ansible.builtin.file:
path: "{{ vm_path | default('/var/lib/libvirt/images/') }}{{ hostname }}-cloudinit.iso"
2024-03-19 23:02:50 +01:00
state: absent
- name: Get list of CD-ROM devices
2024-10-28 18:47:31 +01:00
ansible.builtin.shell: set -o pipefail && virsh --connect qemu:///system domblklist {{ hostname }} --details | grep 'cdrom' | awk '{print $3}'
2024-03-19 23:02:50 +01:00
changed_when: false
register: cdrom_devices
- name: Wait for VM to spin down
2024-07-11 22:20:45 +02:00
ansible.builtin.wait_for:
2024-03-19 23:02:50 +01:00
timeout: 15
- name: Remove CD-ROM devices
when: cdrom_devices.stdout_lines | length > 0
2024-07-11 22:20:45 +02:00
ansible.builtin.command: virsh --connect qemu:///system detach-disk {{ hostname }} {{ item }} --persistent
2024-10-30 00:29:46 +01:00
with_items: "{{ cdrom_devices.stdout_lines | select('ne', 'sdc') | list }}"
changed_when: result.rc == 0
register: result
2024-03-19 23:02:50 +01:00
- name: Start the VM
community.libvirt.virt:
name: "{{ hostname }}"
state: running
- name: Wait for VM to boot up
delegate_to: "{{ inventory_hostname }}"
2024-07-11 22:20:45 +02:00
ansible.builtin.wait_for_connection:
2024-07-11 22:09:58 +02:00
timeout: 300