2019-08-06 18:59:40 +00:00
|
|
|
---
|
|
|
|
- name: Destroy
|
|
|
|
hosts: localhost
|
|
|
|
connection: local
|
|
|
|
gather_facts: false
|
2019-10-19 14:47:04 +00:00
|
|
|
no_log: "{{ molecule_no_log }}"
|
2019-08-06 18:59:40 +00:00
|
|
|
tasks:
|
2020-10-04 10:40:41 +00:00
|
|
|
- name: Check existing instance config file
|
|
|
|
stat:
|
|
|
|
path: "{{ molecule_instance_config }}"
|
|
|
|
register: cfg
|
|
|
|
|
2020-10-02 07:32:08 +00:00
|
|
|
- name: Populate the instance config
|
2020-10-03 15:12:24 +00:00
|
|
|
set_fact:
|
2020-10-19 07:06:59 +00:00
|
|
|
instance_conf: "{{ (lookup('file', molecule_instance_config) | from_yaml) if cfg.stat.exists else [] }}"
|
2019-08-06 18:59:40 +00:00
|
|
|
|
|
|
|
- name: Destroy molecule instance(s)
|
2020-10-02 07:32:08 +00:00
|
|
|
hcloud_server:
|
2019-10-19 14:47:04 +00:00
|
|
|
name: "{{ item.instance }}"
|
2020-10-02 07:32:08 +00:00
|
|
|
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
2019-08-06 18:59:40 +00:00
|
|
|
state: absent
|
|
|
|
register: server
|
2020-10-03 15:12:24 +00:00
|
|
|
loop: "{{ instance_conf }}"
|
2019-08-06 18:59:40 +00:00
|
|
|
async: 7200
|
|
|
|
poll: 0
|
|
|
|
|
|
|
|
- name: Wait for instance(s) deletion to complete
|
|
|
|
async_status:
|
|
|
|
jid: "{{ item.ansible_job_id }}"
|
2020-10-02 07:32:08 +00:00
|
|
|
register: hetzner_jobs
|
|
|
|
until: hetzner_jobs.finished
|
2019-08-06 18:59:40 +00:00
|
|
|
retries: 300
|
2020-10-03 15:12:24 +00:00
|
|
|
loop: "{{ server.results }}"
|
|
|
|
|
2020-10-04 10:40:41 +00:00
|
|
|
- pause:
|
|
|
|
seconds: 5
|
|
|
|
|
2020-10-03 15:12:24 +00:00
|
|
|
- name: Destroy volume(s)
|
|
|
|
hcloud_volume:
|
|
|
|
name: "{{ item.instance }}"
|
|
|
|
server: "{{ item.instance }}"
|
|
|
|
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
|
|
state: "absent"
|
|
|
|
register: volumes
|
|
|
|
loop: "{{ instance_conf }}"
|
|
|
|
when: item.volume | default(False) | bool
|
|
|
|
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
|
|
|
|
loop: "{{ volumes.results }}"
|
2019-10-19 14:47:04 +00:00
|
|
|
|
2020-10-02 07:32:08 +00:00
|
|
|
- name: Remove registered SSH key
|
|
|
|
hcloud_ssh_key:
|
|
|
|
name: "{{ instance_conf[0].ssh_key_name }}"
|
2019-10-19 14:47:04 +00:00
|
|
|
state: absent
|
2020-10-03 15:12:24 +00:00
|
|
|
when: (instance_conf | default([])) | length > 0
|
2019-08-06 18:59:40 +00:00
|
|
|
|
|
|
|
# Mandatory configuration for Molecule to function.
|
|
|
|
|
|
|
|
- name: Populate instance config
|
|
|
|
set_fact:
|
|
|
|
instance_conf: {}
|
|
|
|
|
|
|
|
- name: Dump instance config
|
|
|
|
copy:
|
2020-10-19 07:06:59 +00:00
|
|
|
content: |
|
|
|
|
# Molecule managed
|
|
|
|
|
2021-03-14 15:24:22 +00:00
|
|
|
{{ instance_conf | to_nice_yaml(indent=2) }}
|
2019-08-06 18:59:40 +00:00
|
|
|
dest: "{{ molecule_instance_config }}"
|
|
|
|
when: server.changed | bool
|