cleanup and add volumes handling
This commit is contained in:
parent
dc1ffaa019
commit
6e8a75e275
|
@ -2,4 +2,4 @@
|
|||
- name: Converge
|
||||
hosts: all
|
||||
roles:
|
||||
- role: "{{ cookiecutter.author }}.{{ cookiecutter.role_name }}"
|
||||
- role: {{ cookiecutter.author }}.{{ cookiecutter.role_name }}
|
||||
|
|
|
@ -34,7 +34,6 @@
|
|||
server_type: "{{ item.server_type }}"
|
||||
ssh_keys:
|
||||
- "{{ ssh_key_name }}"
|
||||
volumes: "{{ item.volumes | default(omit) }}"
|
||||
image: "{{ item.image }}"
|
||||
location: "{{ item.location | default(omit) }}"
|
||||
datacenter: "{{ item.datacenter | default(omit) }}"
|
||||
|
@ -42,7 +41,7 @@
|
|||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
||||
state: present
|
||||
register: server
|
||||
with_items: "{{ molecule_yml.platforms }}"
|
||||
loop: "{{ molecule_yml.platforms }}"
|
||||
async: 7200
|
||||
poll: 0
|
||||
|
||||
|
@ -52,20 +51,46 @@
|
|||
register: hetzner_jobs
|
||||
until: hetzner_jobs.finished
|
||||
retries: 300
|
||||
with_items: "{{ server.results }}"
|
||||
loop: "{{ server.results }}"
|
||||
|
||||
- name: Create volume(s)
|
||||
hcloud_volume:
|
||||
name: "{{ item.name }}"
|
||||
server: "{{ item.name }}"
|
||||
location: "{{ item.location | default(omit) }}"
|
||||
size: "{{ item.volume_size | default(10) }}"
|
||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
||||
state: "present"
|
||||
loop: "{{ molecule_yml.platforms }}"
|
||||
when: item.volume | default(False) | bool
|
||||
register: volumes
|
||||
async: 7200
|
||||
poll: 0
|
||||
|
||||
- name: Wait for volume(s) creation to complete
|
||||
async_status:
|
||||
jid: "{{ item.ansible_job_id }}"
|
||||
register: hetzner_volumes
|
||||
until: hetzner_volumes.finished
|
||||
retries: 300
|
||||
when: volumes.changed
|
||||
loop: "{{ volumes.results }}"
|
||||
|
||||
# Mandatory configuration for Molecule to function.
|
||||
|
||||
- name: Populate instance config dict
|
||||
set_fact:
|
||||
instance_conf_dict: {
|
||||
'instance': "{{ item.hcloud_server.name }}",
|
||||
'ssh_key_name': "{{ ssh_key_name }}",
|
||||
'address': "{{ item.hcloud_server.ipv4_address }}",
|
||||
'user': "{{ ssh_user }}",
|
||||
'port': "{{ ssh_port }}",
|
||||
'identity_file': "{{ ssh_path }}", }
|
||||
with_items: "{{ hetzner_jobs.results }}"
|
||||
instance_conf_dict:
|
||||
{
|
||||
"instance": "{{ item.hcloud_server.name }}",
|
||||
"ssh_key_name": "{{ ssh_key_name }}",
|
||||
"address": "{{ item.hcloud_server.ipv4_address }}",
|
||||
"user": "{{ ssh_user }}",
|
||||
"port": "{{ ssh_port }}",
|
||||
"identity_file": "{{ ssh_path }}",
|
||||
"volume": "{{ item.item.item.volume | default(False) | bool }}",
|
||||
}
|
||||
loop: "{{ hetzner_jobs.results }}"
|
||||
register: instance_config_dict
|
||||
when: server.changed | bool
|
||||
|
||||
|
@ -76,7 +101,10 @@
|
|||
|
||||
- name: Dump instance config
|
||||
copy:
|
||||
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
|
||||
content: |
|
||||
# Molecule managed
|
||||
|
||||
{{ instance_conf | to_json | from_json | to_yaml }}
|
||||
dest: "{{ molecule_instance_config }}"
|
||||
when: server.changed | bool
|
||||
|
||||
|
@ -86,7 +114,7 @@
|
|||
host: "{{ item.address }}"
|
||||
search_regex: SSH
|
||||
delay: 10
|
||||
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
|
||||
loop: "{{ lookup('file', molecule_instance_config) | from_yaml }}"
|
||||
|
||||
- name: Wait for VM to settle down
|
||||
pause:
|
||||
|
|
|
@ -6,17 +6,14 @@
|
|||
gather_facts: false
|
||||
no_log: "{{ molecule_no_log }}"
|
||||
tasks:
|
||||
- name: Check existing instance config file
|
||||
stat:
|
||||
path: "{{ molecule_instance_config }}"
|
||||
register: cfg
|
||||
|
||||
- name: Populate the instance config
|
||||
block:
|
||||
- name: Populate instance config from file
|
||||
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
|
||||
set_fact:
|
||||
instance_conf: "{{ (lookup('file', molecule_instance_config) | from_yaml) if cfg.stat.exists else [] }}"
|
||||
|
||||
- name: Destroy molecule instance(s)
|
||||
hcloud_server:
|
||||
|
@ -24,8 +21,7 @@
|
|||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
||||
state: absent
|
||||
register: server
|
||||
with_items: "{{ instance_conf }}"
|
||||
when: not skip_instances
|
||||
loop: "{{ instance_conf }}"
|
||||
async: 7200
|
||||
poll: 0
|
||||
|
||||
|
@ -35,15 +31,37 @@
|
|||
register: hetzner_jobs
|
||||
until: hetzner_jobs.finished
|
||||
retries: 300
|
||||
with_items: "{{ server.results }}"
|
||||
loop: "{{ server.results }}"
|
||||
|
||||
- pause:
|
||||
seconds: 5
|
||||
|
||||
- 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 }}"
|
||||
|
||||
- name: Remove registered SSH key
|
||||
hcloud_ssh_key:
|
||||
name: "{{ instance_conf[0].ssh_key_name }}"
|
||||
state: absent
|
||||
when:
|
||||
- not skip_instances
|
||||
- (instance_conf | default([])) | length > 0 # must contain at least one instance
|
||||
when: (instance_conf | default([])) | length > 0
|
||||
|
||||
# Mandatory configuration for Molecule to function.
|
||||
|
||||
|
@ -53,7 +71,10 @@
|
|||
|
||||
- name: Dump instance config
|
||||
copy:
|
||||
content: "{{ instance_conf | molecule_to_yaml | molecule_header }}"
|
||||
content: |
|
||||
# Molecule managed
|
||||
|
||||
{{ instance_conf | to_json | from_json | to_yaml }}
|
||||
dest: "{{ molecule_instance_config }}"
|
||||
when: server.changed | bool
|
||||
{% endraw %}
|
||||
|
|
|
@ -3,7 +3,8 @@ dependency:
|
|||
name: galaxy
|
||||
options:
|
||||
role-file: molecule/requirements.yml
|
||||
requirements-file: molecule/requirements.yml
|
||||
env:
|
||||
ANSIBLE_GALAXY_DISPLAY_PROGRESS: "false"
|
||||
driver:
|
||||
name: delegated
|
||||
platforms:
|
||||
|
|
Loading…
Reference in New Issue