Added a cleanup feature to the ocloud.yml playbook 78/14878/5
authorArnaldo Hernandez <alhernan@redhat.com>
Wed, 3 Sep 2025 15:55:06 +0000 (15:55 +0000)
committerChristopher Wheeler <chwheele@redhat.com>
Thu, 11 Dec 2025 03:38:49 +0000 (03:38 +0000)
- Updated ocloud_infra_vm role to support cleanup of failed deployments.
- Updated README to include the new cleanup info.
- Updated ansible.cfg for compliance with ansible-core 2.16.

Issue-Id: INF-498
Change-Id: I67d8e885b6be27575ae132432ff18c58fc7bfd47
Signed-off-by: Arnaldo Hernandez <alhernan@redhat.com>
okd/README.md
okd/ansible.cfg
okd/roles/ocloud_infra_vm/defaults/main.yml
okd/roles/ocloud_infra_vm/tasks/cleanup.yml [new file with mode: 0644]
okd/roles/ocloud_infra_vm/tasks/create.yml [new file with mode: 0644]
okd/roles/ocloud_infra_vm/tasks/main.yml

index 0916566..371a446 100644 (file)
@@ -455,26 +455,65 @@ Refer to [Troubleshooting installation issues](https://docs.okd.io/latest/instal
 
 # Cleanup
 
-## VM
+This section describes how to clean up a deployment, either due to a failure or to prepare for a new deployment.
 
-To cleanup a VM-based deployment due to failure, or to prepare to redeploy, execute the following as root on the libvirt/KVM host:
+## VM-based Deployment
 
-1. Shut down and remove the virtual machine (note that the VM name may differ if the default is overridden):
+For virtual machine-based deployments, a playbook is provided to automate the cleanup process. Alternatively, manual steps can be followed.
 
-   ```
+### Automated Cleanup (Recommended)
+
+To automate the cleanup process, set the `ocloud_action` variable to `cleanup` in the `playbooks/ocloud.yml` playbook, as shown below. This will trigger the cleanup tasks within the `ocloud_infra_vm` role, which handles the destruction of VMs, networks, and associated storage.
+
+```yaml
+- name: Deploy O-Cloud
+  hosts: ocloud
+  gather_facts: false
+  vars:
+    ocloud_action: cleanup
+  roles:
+    - ocloud
+```
+
+Execute the playbook from the base directory to apply the changes:
+
+```bash
+ansible-playbook -i inventory <PATH TO YOUR INVENTORY> playbooks/ocloud.yml
+```
+
+NOTE: The playbook will prompt for confirmation before proceeding with the destructive actions.
+
+### Manual Cleanup
+
+If you prefer to perform the cleanup manually, follow these steps on the libvirt/KVM host as the root user. Note that the names for the VM, network, and disk images may differ if you have overridden the default variables.
+
+1. **Shut down and remove the virtual machine(s):**
+   This command will forcefully stop and then delete the definition of the virtual machine from libvirt.
+
+   ```bash
+   # Replace 'master-0' with the actual VM name if you changed the default
    virsh destroy master-0
    virsh undefine master-0
    ```
+   If you have multiple VMs, repeat these commands for each one.
 
-2. Disable and remove the virtual network (note that the network name may differ if the default is overridden):
+2. **Disable and remove the virtual network:**
+   This will deactivate and delete the virtual network definition.
 
-   ```
+   ```bash
+   # Replace 'ocloud' with the actual network name if you changed the default
    virsh net-destroy ocloud
    virsh net-undefine ocloud
    ```
 
-3. Remove virtual disk and boot media:
+3. **Remove virtual disk and boot media:**
+   This command deletes the disk image and the installation ISO file.
 
-   ```
+   ```bash
+   # Adjust path and names if defaults were changed
    rm /var/lib/libvirt/images/master-0*.{qcow2,iso}
    ```
+
+## Bare Metal Deployment
+
+For bare metal deployments, cleanup typically involves reprovisioning the server(s) using their baseboard management controllers (BMCs). This process is outside the scope of this automation. Refer to your server hardware documentation for instructions on how to reinstall an operating system. No cleanup is required on the Ansible deployer host for a bare metal deployment.
index 96ba841..e620cf6 100644 (file)
@@ -8,8 +8,8 @@ command_warnings = True
 ansible_managed = "This file is managed by Ansible - changes may be lost"
 retry_files_enabled = False
 forks = 8
-stdout_callback = yaml
-callback_whitelist = debug,profile_tasks
+result_format = yaml
+callbacks_enabled = debug,profile_tasks
 
 [privilege_escalation]
 become_method = sudo
index a7dea75..d2d245c 100644 (file)
@@ -1,4 +1,5 @@
 ---
+ocloud_action: "create"
 ocloud_infra_vm_cpus: 16
 ocloud_infra_vm_mem_gb: 32
 ocloud_infra_vm_disk_gb: 150
diff --git a/okd/roles/ocloud_infra_vm/tasks/cleanup.yml b/okd/roles/ocloud_infra_vm/tasks/cleanup.yml
new file mode 100644 (file)
index 0000000..e996659
--- /dev/null
@@ -0,0 +1,59 @@
+---
+- name: Check if cleanup confirmation should be bypassed
+  ansible.builtin.set_fact:
+    yes_i_know_what_im_doing: "{{ yes_i_know_what_im_doing | default(false) }}"
+
+- name: Display warning and require confirmation
+  ansible.builtin.pause:
+    prompt: |
+
+      WARNING: This playbook will perform the following destructive actions:
+
+      - Destroy and undefine the following virtual machines:
+        - {{ groups['ocloud'] | join(', ') }}
+      - Destroy and undefine the virtual network: {{ ocloud_net_name | default('ocloud') }}
+      - Delete virtual disk and boot media for the VMs from {{ ocloud_infra_vm_disk_dir }}
+
+      Type 'yes' to continue, or anything else to abort:
+  when: not yes_i_know_what_im_doing | bool
+
+- name: Destroy virtual machines
+  become: true
+  community.libvirt.virt:
+    command: destroy
+    name: "{{ item }}"
+  loop: "{{ groups['ocloud'] }}"
+  ignore_errors: true
+
+- name: Undefine virtual machines
+  become: true
+  community.libvirt.virt:
+    command: undefine
+    name: "{{ item }}"
+    force: true
+  loop: "{{ groups['ocloud'] }}"
+  ignore_errors: true
+
+- name: Destroy and undefine virtual network
+  become: true
+  community.libvirt.virt_net:
+    name: "{{ ocloud_net_name }}"
+    state: absent
+  ignore_errors: true
+  run_once: true
+
+- name: Remove virtual disk (qcow2)
+  become: true
+  ansible.builtin.file:
+    path: "{{ ocloud_infra_vm_disk_dir }}/{{ item }}-{{ ocloud_infra }}.qcow2"
+    state: absent
+  loop: "{{ groups['ocloud'] }}"
+  ignore_errors: true
+
+- name: Remove boot media (image.iso)
+  become: true
+  ansible.builtin.file:
+    path: "{{ ocloud_infra_vm_disk_dir }}/{{ item }}-{{ ocloud_infra }}-image.iso"
+    state: absent
+  loop: "{{ groups['ocloud'] }}"
+  ignore_errors: true
diff --git a/okd/roles/ocloud_infra_vm/tasks/create.yml b/okd/roles/ocloud_infra_vm/tasks/create.yml
new file mode 100644 (file)
index 0000000..a0aa2a1
--- /dev/null
@@ -0,0 +1,63 @@
+---
+- name: Define virtual network - {{ ocloud_net_name }}
+  community.libvirt.virt_net:
+    command: define
+    name: "{{ ocloud_net_name }}"
+    xml: '{{ lookup("template", "virt_net.xml.j2") }}'
+  run_once: true
+  become: true
+
+- name: Activate virtual network - {{ ocloud_net_name }}
+  community.libvirt.virt_net:
+    name: "{{ ocloud_net_name }}"
+    state: active
+  run_once: true
+  become: true
+
+- name: Configure virtual network to auto-start - {{ ocloud_net_name }}
+  community.libvirt.virt_net:
+    name: "{{ ocloud_net_name }}"
+    autostart: true
+  run_once: true
+  become: true
+
+- name: Locate qemu-kvm emulator
+  stat:
+    path: "{{ item }}"
+  register: ocloud_infra_vm_emulator_stat
+  loop:
+    - /usr/bin/qemu-kvm
+    - /usr/libexec/qemu-kvm
+
+- set_fact:
+    ocloud_infra_vm_emulator_path: "{{ item.stat.path }}"
+  when: item.stat.exists
+  loop: "{{ ocloud_infra_vm_emulator_stat.results }}"
+
+- name: Create virtual machine
+  community.libvirt.virt:
+    command: define
+    xml: '{{ lookup("template", "virt.xml.j2") }}'
+  register: ocloud_infra_vm_definition
+  become: true
+
+- name: Create VM disk
+  ansible.builtin.command:
+    cmd: "qemu-img create -f qcow2 {{ ocloud_infra_vm_disk_path }} {{ ocloud_infra_vm_disk_gb }}G"
+  when: ocloud_infra_vm_definition.changed
+  become: true
+
+- name: Copy platform boot image
+  ansible.builtin.copy:
+    src: "{{ ocloud_platform_image }}"
+    dest: "{{ ocloud_infra_vm_image }}"
+    remote_src: true
+  when: ocloud_infra_vm_definition.changed
+  become: true
+
+- name: Activate virtual machine
+  community.libvirt.virt:
+    name: "{{ inventory_hostname }}"
+    state: running
+  notify: monitor_platform_deployment
+  become: true
index a0aa2a1..360e988 100644 (file)
@@ -1,63 +1,9 @@
 ---
-- name: Define virtual network - {{ ocloud_net_name }}
-  community.libvirt.virt_net:
-    command: define
-    name: "{{ ocloud_net_name }}"
-    xml: '{{ lookup("template", "virt_net.xml.j2") }}'
-  run_once: true
-  become: true
+- name: Include cleanup tasks
+  include_tasks: cleanup.yml
+  when: ocloud_action == 'cleanup'
 
-- name: Activate virtual network - {{ ocloud_net_name }}
-  community.libvirt.virt_net:
-    name: "{{ ocloud_net_name }}"
-    state: active
-  run_once: true
-  become: true
+- name: Include create tasks
+  include_tasks: create.yml
+  when: ocloud_action == 'create'
 
-- name: Configure virtual network to auto-start - {{ ocloud_net_name }}
-  community.libvirt.virt_net:
-    name: "{{ ocloud_net_name }}"
-    autostart: true
-  run_once: true
-  become: true
-
-- name: Locate qemu-kvm emulator
-  stat:
-    path: "{{ item }}"
-  register: ocloud_infra_vm_emulator_stat
-  loop:
-    - /usr/bin/qemu-kvm
-    - /usr/libexec/qemu-kvm
-
-- set_fact:
-    ocloud_infra_vm_emulator_path: "{{ item.stat.path }}"
-  when: item.stat.exists
-  loop: "{{ ocloud_infra_vm_emulator_stat.results }}"
-
-- name: Create virtual machine
-  community.libvirt.virt:
-    command: define
-    xml: '{{ lookup("template", "virt.xml.j2") }}'
-  register: ocloud_infra_vm_definition
-  become: true
-
-- name: Create VM disk
-  ansible.builtin.command:
-    cmd: "qemu-img create -f qcow2 {{ ocloud_infra_vm_disk_path }} {{ ocloud_infra_vm_disk_gb }}G"
-  when: ocloud_infra_vm_definition.changed
-  become: true
-
-- name: Copy platform boot image
-  ansible.builtin.copy:
-    src: "{{ ocloud_platform_image }}"
-    dest: "{{ ocloud_infra_vm_image }}"
-    remote_src: true
-  when: ocloud_infra_vm_definition.changed
-  become: true
-
-- name: Activate virtual machine
-  community.libvirt.virt:
-    name: "{{ inventory_hostname }}"
-    state: running
-  notify: monitor_platform_deployment
-  become: true