add option to attach volumes
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Robert Kaussow 2020-10-03 16:30:02 +02:00
parent 6df88914e2
commit 15d3b1f929
No known key found for this signature in database
GPG Key ID: 65362AE74AF98B61
8 changed files with 126 additions and 32 deletions

View File

@ -6,7 +6,7 @@
- name: lv_test - name: lv_test
group: vg_test group: vg_test
disks: disks:
- /dev/sda - /dev/sdb
fstype: ext4 fstype: ext4
size: 1g size: 1g
resizefs: True resizefs: True

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) }}"
@ -41,7 +40,7 @@
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: present state: present
register: server register: server
with_items: "{{ molecule_yml.platforms }}" loop: "{{ molecule_yml.platforms }}"
async: 7200 async: 7200
poll: 0 poll: 0
@ -51,20 +50,46 @@
register: hetzner_jobs register: hetzner_jobs
until: hetzner_jobs.finished until: hetzner_jobs.finished
retries: 300 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. # Mandatory configuration for Molecule to function.
- 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 }}", {
'ssh_key_name': "{{ ssh_key_name }}", "instance": "{{ item.hcloud_server.name }}",
'address': "{{ item.hcloud_server.ipv4_address }}", "ssh_key_name": "{{ ssh_key_name }}",
'user': "{{ ssh_user }}", "address": "{{ item.hcloud_server.ipv4_address }}",
'port': "{{ ssh_port }}", "user": "{{ ssh_user }}",
'identity_file': "{{ ssh_path }}", } "port": "{{ ssh_port }}",
with_items: "{{ hetzner_jobs.results }}" "identity_file": "{{ ssh_path }}",
"volume": "{{ item.item.item.volume | default(False) | bool }}",
}
loop: "{{ hetzner_jobs.results }}"
register: instance_config_dict register: instance_config_dict
when: server.changed | bool when: server.changed | bool
@ -85,7 +110,7 @@
host: "{{ item.address }}" host: "{{ item.address }}"
search_regex: SSH search_regex: SSH
delay: 10 delay: 10
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
- name: Wait for VM to settle down - name: Wait for VM to settle down
pause: pause:

View File

@ -23,7 +23,7 @@
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: absent state: absent
register: server register: server
with_items: "{{ instance_conf }}" loop: "{{ instance_conf }}"
when: not skip_instances when: not skip_instances
async: 7200 async: 7200
poll: 0 poll: 0
@ -34,7 +34,28 @@
register: hetzner_jobs register: hetzner_jobs
until: hetzner_jobs.finished until: hetzner_jobs.finished
retries: 300 retries: 300
with_items: "{{ server.results }}" loop: "{{ server.results }}"
- 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 - name: Remove registered SSH key
hcloud_ssh_key: hcloud_ssh_key:

View File

@ -10,6 +10,7 @@ platforms:
- name: centos7-lvm - name: centos7-lvm
image: centos-7 image: centos-7
server_type: cx11 server_type: cx11
volume: True
lint: | lint: |
/usr/local/bin/flake8 /usr/local/bin/flake8
provisioner: provisioner:

View File

@ -7,7 +7,7 @@
- name: lv_test - name: lv_test
group: vg_test group: vg_test
disks: disks:
- /dev/sda - /dev/sdb
fstype: ext4 fstype: ext4
size: 1g size: 1g
resizefs: True resizefs: True

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) }}"
@ -41,7 +40,7 @@
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: present state: present
register: server register: server
with_items: "{{ molecule_yml.platforms }}" loop: "{{ molecule_yml.platforms }}"
async: 7200 async: 7200
poll: 0 poll: 0
@ -51,20 +50,46 @@
register: hetzner_jobs register: hetzner_jobs
until: hetzner_jobs.finished until: hetzner_jobs.finished
retries: 300 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. # Mandatory configuration for Molecule to function.
- 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 }}", {
'ssh_key_name': "{{ ssh_key_name }}", "instance": "{{ item.hcloud_server.name }}",
'address': "{{ item.hcloud_server.ipv4_address }}", "ssh_key_name": "{{ ssh_key_name }}",
'user': "{{ ssh_user }}", "address": "{{ item.hcloud_server.ipv4_address }}",
'port': "{{ ssh_port }}", "user": "{{ ssh_user }}",
'identity_file': "{{ ssh_path }}", } "port": "{{ ssh_port }}",
with_items: "{{ hetzner_jobs.results }}" "identity_file": "{{ ssh_path }}",
"volume": "{{ item.item.item.volume | default(False) | bool }}",
}
loop: "{{ hetzner_jobs.results }}"
register: instance_config_dict register: instance_config_dict
when: server.changed | bool when: server.changed | bool
@ -85,7 +110,7 @@
host: "{{ item.address }}" host: "{{ item.address }}"
search_regex: SSH search_regex: SSH
delay: 10 delay: 10
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}" loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
- name: Wait for VM to settle down - name: Wait for VM to settle down
pause: pause:

View File

@ -23,7 +23,7 @@
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
state: absent state: absent
register: server register: server
with_items: "{{ instance_conf }}" loop: "{{ instance_conf }}"
when: not skip_instances when: not skip_instances
async: 7200 async: 7200
poll: 0 poll: 0
@ -34,7 +34,28 @@
register: hetzner_jobs register: hetzner_jobs
until: hetzner_jobs.finished until: hetzner_jobs.finished
retries: 300 retries: 300
with_items: "{{ server.results }}" loop: "{{ server.results }}"
- 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 - name: Remove registered SSH key
hcloud_ssh_key: hcloud_ssh_key:

View File

@ -10,6 +10,7 @@ platforms:
- name: centos8-lvm - name: centos8-lvm
image: centos-8 image: centos-8
server_type: cx11 server_type: cx11
volume: True
lint: | lint: |
/usr/local/bin/flake8 /usr/local/bin/flake8
provisioner: provisioner: