Native ansible filters and volumes handling #10

Merged
decentral1se merged 2 commits from volumes-handling into main 2021-01-06 11:06:55 +00:00
8 changed files with 171 additions and 29 deletions
Showing only changes of commit d57f80a496 - Show all commits

View File

@ -7,6 +7,10 @@ platforms:
- name: "molecule-hetznercloud-${INSTANCE_UUID}" - name: "molecule-hetznercloud-${INSTANCE_UUID}"
server_type: cx11 server_type: cx11
image: debian-10 image: debian-10
volumes:
name: "molecule-hetznercloud-volume-${INSTANCE_UUID}"
size: 11
create: true
provisioner: provisioner:
name: ansible name: ansible
verifier: verifier:

View File

@ -33,7 +33,6 @@
server_type: "{{ item.server_type }}" server_type: "{{ item.server_type }}"
ssh_keys: ssh_keys:
- "{{ ssh_key_name }}" - "{{ ssh_key_name }}"
volumes: "{{ item.volumes | default(omit) }}"
image: "{{ item.image }}" image: "{{ item.image }}"
location: "{{ item.location | default(omit) }}" location: "{{ item.location | default(omit) }}"
datacenter: "{{ item.datacenter | default(omit) }}" datacenter: "{{ item.datacenter | default(omit) }}"
@ -53,6 +52,31 @@
retries: 300 retries: 300
with_items: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Create volume(s)
hcloud_volume:
name: "{{ item.volumes.name | default(item.name) }}"
server: "{{ item.name }}"
location: "{{ item.volumes.location | default(omit) }}"
size: "{{ item.volumes.size | default(10) }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: "present"
with_items: "{{ molecule_yml.platforms }}"
when:
- item.volumes is defined
- item.volumes.create | 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
with_items: "{{ volumes.results }}"
- name: Populate instance config dict - name: Populate instance config dict
set_fact: set_fact:
instance_conf_dict: { instance_conf_dict: {
@ -61,7 +85,8 @@
'address': "{{ item.hcloud_server.ipv4_address }}", 'address': "{{ item.hcloud_server.ipv4_address }}",
'user': "{{ ssh_user }}", 'user': "{{ ssh_user }}",
'port': "{{ ssh_port }}", 'port': "{{ ssh_port }}",
'identity_file': "{{ ssh_path }}", } 'identity_file': "{{ ssh_path }}",
'volumes': "{{ item.item.item.volumes | default({}) }}", }
with_items: "{{ hetzner_jobs.results }}" with_items: "{{ hetzner_jobs.results }}"
register: instance_config_dict register: instance_config_dict
when: server.changed | bool when: server.changed | bool

View File

@ -37,6 +37,29 @@
retries: 300 retries: 300
with_items: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Destroy volume(s)
hcloud_volume:
name: "{{ item.volumes.name | default(item.instance) }}"
server: "{{ item.instance }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: "absent"
register: volumes
with_items: "{{ instance_conf }}"
when:
- item.volumes is defined
- item.volumes.create | 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
with_items: "{{ volumes.results }}"
- name: Remove registered SSH key - name: Remove registered SSH key
hcloud_ssh_key: hcloud_ssh_key:
name: "{{ instance_conf[0].ssh_key_name }}" name: "{{ instance_conf[0].ssh_key_name }}"

View File

@ -32,7 +32,6 @@
server_type: "{{ item.server_type }}" server_type: "{{ item.server_type }}"
ssh_keys: ssh_keys:
- "{{ ssh_key_name }}" - "{{ ssh_key_name }}"
volumes: "{{ item.volumes | default(omit) }}"
image: "{{ item.image }}" image: "{{ item.image }}"
location: "{{ item.location | default(omit) }}" location: "{{ item.location | default(omit) }}"
datacenter: "{{ item.datacenter | default(omit) }}" datacenter: "{{ item.datacenter | default(omit) }}"
@ -52,17 +51,41 @@
retries: 300 retries: 300
with_items: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Create volume(s)
hcloud_volume:
name: "{{ item.volumes.name | default(item.name) }}"
server: "{{ item.name }}"
location: "{{ item.volumes.location | default(omit) }}"
size: "{{ item.volumes.size | default(10) }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: "present"
with_items: "{{ molecule_yml.platforms }}"
when:
- item.volumes is defined
- item.volumes.create | 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
with_items: "{{ volumes.results }}"
- name: Populate instance config dict - name: Populate instance config dict
set_fact: set_fact:
instance_conf_dict: instance_conf_dict: {
{ 'instance': "{{ item.hcloud_server.name }}",
"instance": "{{ item.hcloud_server.name }}", 'ssh_key_name': "{{ ssh_key_name }}",
"ssh_key_name": "{{ ssh_key_name }}", 'address': "{{ item.hcloud_server.ipv4_address }}",
"address": "{{ item.hcloud_server.ipv4_address }}", 'user': "{{ ssh_user }}",
"user": "{{ ssh_user }}", 'port': "{{ ssh_port }}",
"port": "{{ ssh_port }}", 'identity_file': "{{ ssh_path }}",
"identity_file": "{{ ssh_path }}", 'volumes': "{{ item.item.item.volumes | default({}) }}", }
}
with_items: "{{ hetzner_jobs.results }}" with_items: "{{ hetzner_jobs.results }}"
register: instance_config_dict register: instance_config_dict
when: server.changed | bool when: server.changed | bool

View File

@ -36,6 +36,29 @@
retries: 300 retries: 300
with_items: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Destroy volume(s)
hcloud_volume:
name: "{{ item.volumes.name | default(item.instance) }}"
server: "{{ item.instance }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: "absent"
register: volumes
with_items: "{{ instance_conf }}"
when:
- item.volumes is defined
- item.volumes.create | 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
with_items: "{{ volumes.results }}"
- name: Remove registered SSH key - name: Remove registered SSH key
hcloud_ssh_key: hcloud_ssh_key:
name: "{{ instance_conf[0].ssh_key_name }}" name: "{{ instance_conf[0].ssh_key_name }}"

View File

@ -23,6 +23,7 @@
hcloud_ssh_key: hcloud_ssh_key:
name: "{{ ssh_key_name }}" name: "{{ ssh_key_name }}"
public_key: "{{ generated_ssh_key.public_key }}" public_key: "{{ generated_ssh_key.public_key }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: present state: present
- name: Create molecule instance(s) - name: Create molecule instance(s)
@ -31,7 +32,6 @@
server_type: "{{ item.server_type }}" server_type: "{{ item.server_type }}"
ssh_keys: ssh_keys:
- "{{ ssh_key_name }}" - "{{ ssh_key_name }}"
volumes: "{{ item.volumes | default(omit) }}"
image: "{{ item.image }}" image: "{{ item.image }}"
location: "{{ item.location | default(omit) }}" location: "{{ item.location | default(omit) }}"
datacenter: "{{ item.datacenter | default(omit) }}" datacenter: "{{ item.datacenter | default(omit) }}"
@ -51,19 +51,41 @@
retries: 300 retries: 300
with_items: "{{ server.results }}" with_items: "{{ server.results }}"
# Mandatory configuration for Molecule to function. - name: Create volume(s)
hcloud_volume:
name: "{{ item.volumes.name | default(item.name) }}"
server: "{{ item.name }}"
location: "{{ item.volumes.location | default(omit) }}"
size: "{{ item.volumes.size | default(10) }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: "present"
with_items: "{{ molecule_yml.platforms }}"
when:
- item.volumes is defined
- item.volumes.create | 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
with_items: "{{ volumes.results }}"
- name: Populate instance config dict - name: Populate instance config dict
set_fact: set_fact:
instance_conf_dict: instance_conf_dict: {
{ 'instance': "{{ item.hcloud_server.name }}",
"instance": "{{ item.hcloud_server.name }}", 'ssh_key_name': "{{ ssh_key_name }}",
"ssh_key_name": "{{ ssh_key_name }}", 'address': "{{ item.hcloud_server.ipv4_address }}",
"address": "{{ item.hcloud_server.ipv4_address }}", 'user': "{{ ssh_user }}",
"user": "{{ ssh_user }}", 'port': "{{ ssh_port }}",
"port": "{{ ssh_port }}", 'identity_file': "{{ ssh_path }}",
"identity_file": "{{ ssh_path }}", 'volumes': "{{ item.item.item.volumes | default({}) }}", }
}
with_items: "{{ hetzner_jobs.results }}" with_items: "{{ hetzner_jobs.results }}"
register: instance_config_dict register: instance_config_dict
when: server.changed | bool when: server.changed | bool

View File

@ -36,15 +36,37 @@
retries: 300 retries: 300
with_items: "{{ server.results }}" with_items: "{{ server.results }}"
- name: Destroy volume(s)
hcloud_volume:
name: "{{ item.volumes.name | default(item.instance) }}"
server: "{{ item.instance }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: "absent"
register: volumes
with_items: "{{ instance_conf }}"
when:
- item.volumes is defined
- item.volumes.create | 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
with_items: "{{ volumes.results }}"
- name: Remove registered SSH key - name: Remove registered SSH key
hcloud_ssh_key: hcloud_ssh_key:
name: "{{ instance_conf[0].ssh_key_name }}" name: "{{ instance_conf[0].ssh_key_name }}"
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: absent state: absent
when: when:
- not skip_instances - not skip_instances
- instance_conf # must contain at least one instance - instance_conf | length # must contain at least one instance
# Mandatory configuration for Molecule to function.
- name: Populate instance config - name: Populate instance config
set_fact: set_fact:

View File

@ -10,7 +10,7 @@ def _model_platform_hetznercloud_section_data():
{ {
"name": "instance", "name": "instance",
"server_type": "", "server_type": "",
"volumes": [""], "volumes": {},
"image": "", "image": "",
"location": "", "location": "",
"datacenter": "", "datacenter": "",
@ -35,7 +35,7 @@ def _model_platforms_hetznercloud_errors_section_data():
{ {
"name": 0, "name": 0,
"server_type": 0, "server_type": 0,
"volumes": {}, "volumes": [],
"image": 0, "image": 0,
"location": 0, "location": 0,
"datacenter": 0, "datacenter": 0,
@ -57,7 +57,7 @@ def test_platforms_hetznercloud_has_errors(_config):
{ {
"name": ["must be of string type"], "name": ["must be of string type"],
"server_type": ["must be of string type"], "server_type": ["must be of string type"],
"volumes": ["must be of list type"], "volumes": ["must be of dict type"],
"image": ["must be of string type"], "image": ["must be of string type"],
"location": ["must be of string type"], "location": ["must be of string type"],
"datacenter": ["must be of string type"], "datacenter": ["must be of string type"],