molecule-hetznercloud/molecule_hetznercloud/playbooks/destroy.yml

80 lines
2.2 KiB
YAML
Raw Normal View History

2020-04-24 12:12:57 +00:00
---
- name: Destroy
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ molecule_no_log }}"
tasks:
- name: Populate the instance config
block:
- name: Populate instance config from file
set_fact:
2021-01-06 10:17:41 +00:00
instance_conf: "{{ lookup('file', molecule_instance_config) | from_yaml }}"
2020-04-24 12:12:57 +00:00
skip_instances: false
rescue:
- name: Populate instance config when file missing
set_fact:
instance_conf: {}
skip_instances: true
- name: Destroy molecule instance(s)
hcloud_server:
name: "{{ item.instance }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: absent
register: server
with_items: "{{ instance_conf }}"
when: not skip_instances
async: 7200
poll: 0
- name: Wait for instance(s) deletion to complete
async_status:
jid: "{{ item.ansible_job_id }}"
register: hetzner_jobs
until: hetzner_jobs.finished
retries: 300
with_items: "{{ server.results }}"
2021-01-06 10:18:53 +00:00
- name: Destroy volume(s)
hcloud_volume:
name: "{{ item.volumes.name | default(item.instance) }}"
server: "{{ item.instance }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
2021-01-06 14:21:42 +00:00
state: absent
2021-01-06 10:18:53 +00:00
register: volumes
with_items: "{{ instance_conf }}"
async: 7200
poll: 0
- name: Wait for volume(s) deletion to complete
async_status:
jid: "{{ item.ansible_job_id }}"
register: hetzner_volumes
until: hetzner_volumes.finished
retries: 300
when: volumes.changed
with_items: "{{ volumes.results }}"
2020-04-24 12:12:57 +00:00
- name: Remove registered SSH key
hcloud_ssh_key:
name: "{{ instance_conf[0].ssh_key_name }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: absent
when:
- not skip_instances
2021-01-06 14:27:53 +00:00
- instance_conf | length # must contain at least one instance
2020-04-24 12:12:57 +00:00
- name: Populate instance config
set_fact:
instance_conf: {}
- name: Dump instance config
copy:
2021-01-06 10:17:41 +00:00
content: |
# Molecule managed
{{ instance_conf | to_json | from_json | to_yaml }}
2020-04-24 12:12:57 +00:00
dest: "{{ molecule_instance_config }}"
when: server.changed | bool