--- # TODO: does not handle a non-existant cluster gracefully - name: Terminate instance(s) hosts: localhost gather_facts: no vars_files: - vars.yml tasks: - set_fact: cluster_group=tag_env-{{ cluster_id }} - add_host: name: "{{ item }}" groups: oo_hosts_to_terminate ansible_ssh_user: "{{ deployment_vars[deployment_type].ssh_user }}" ansible_sudo: "{{ deployment_vars[deployment_type].sudo }}" with_items: groups[cluster_group] | default([]) - name: Destroy VMs virt: name: '{{ item[0] }}' command: '{{ item[1] }}' uri: '{{ libvirt_uri }}' with_nested: - groups['oo_hosts_to_terminate'] - [ destroy, undefine ] - name: Delete VMs drives command: 'virsh -c {{ libvirt_uri }} vol-delete --pool {{ libvirt_storage_pool }} {{ item }}.qcow2' args: removes: '{{ libvirt_storage_pool_path }}/{{ item }}.qcow2' with_items: groups['oo_hosts_to_terminate'] - name: Delete the VM cloud-init image file: path: '{{ libvirt_storage_pool_path }}/{{ item }}_cloud-init.iso' state: absent with_items: groups['oo_hosts_to_terminate'] - name: Remove the cloud-init config directory file: path: '{{ libvirt_storage_pool_path }}/{{ item }}_configdrive/' state: absent with_items: groups['oo_hosts_to_terminate']