xoxys.kernel/molecule/centos7/destroy.yml

58 lines
1.6 KiB
YAML
Raw Normal View History

2019-11-03 23:22:18 +00:00
---
- name: Destroy
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ molecule_no_log }}"
tasks:
2020-10-02 07:32:15 +00:00
- name: Populate the instance config
block:
- name: Populate instance config from file
2019-11-03 23:22:18 +00:00
set_fact:
instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
skip_instances: false
rescue:
- name: Populate instance config when file missing
set_fact:
instance_conf: {}
skip_instances: true
- name: Destroy molecule instance(s)
2020-10-02 07:32:15 +00:00
hcloud_server:
2019-11-03 23:22:18 +00:00
name: "{{ item.instance }}"
2020-10-02 07:32:15 +00:00
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
2019-11-03 23:22:18 +00:00
state: absent
register: server
2020-10-02 07:32:15 +00:00
with_items: "{{ instance_conf }}"
2019-11-03 23:22:18 +00:00
when: not skip_instances
async: 7200
poll: 0
- name: Wait for instance(s) deletion to complete
async_status:
jid: "{{ item.ansible_job_id }}"
2020-10-02 07:32:15 +00:00
register: hetzner_jobs
until: hetzner_jobs.finished
2019-11-03 23:22:18 +00:00
retries: 300
2020-10-02 07:32:15 +00:00
with_items: "{{ server.results }}"
2019-11-03 23:22:18 +00:00
2020-10-02 07:32:15 +00:00
- name: Remove registered SSH key
hcloud_ssh_key:
name: "{{ instance_conf[0].ssh_key_name }}"
2019-11-03 23:22:18 +00:00
state: absent
2020-10-02 07:32:15 +00:00
when:
- not skip_instances
- (instance_conf | default([])) | length > 0 # must contain at least one instance
2019-11-03 23:22:18 +00:00
# Mandatory configuration for Molecule to function.
- name: Populate instance config
set_fact:
instance_conf: {}
- name: Dump instance config
copy:
content: "{{ instance_conf | molecule_to_yaml | molecule_header }}"
dest: "{{ molecule_instance_config }}"
when: server.changed | bool