From 15d3b1f929941dd773a799e8d8d27d4a91d30d99 Mon Sep 17 00:00:00 2001 From: Robert Kaussow Date: Sat, 3 Oct 2020 16:30:02 +0200 Subject: [PATCH] add option to attach volumes --- molecule/centos7/converge.yml | 2 +- molecule/centos7/create.yml | 49 ++++++++++++++++++++++++++--------- molecule/centos7/destroy.yml | 27 ++++++++++++++++--- molecule/centos7/molecule.yml | 1 + molecule/centos8/converge.yml | 2 +- molecule/centos8/create.yml | 49 ++++++++++++++++++++++++++--------- molecule/centos8/destroy.yml | 27 ++++++++++++++++--- molecule/centos8/molecule.yml | 1 + 8 files changed, 126 insertions(+), 32 deletions(-) diff --git a/molecule/centos7/converge.yml b/molecule/centos7/converge.yml index 20e0d39..114aadf 100644 --- a/molecule/centos7/converge.yml +++ b/molecule/centos7/converge.yml @@ -6,7 +6,7 @@ - name: lv_test group: vg_test disks: - - /dev/sda + - /dev/sdb fstype: ext4 size: 1g resizefs: True diff --git a/molecule/centos7/create.yml b/molecule/centos7/create.yml index 6c1d373..0117c6c 100644 --- a/molecule/centos7/create.yml +++ b/molecule/centos7/create.yml @@ -33,7 +33,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) }}" @@ -41,7 +40,7 @@ api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" state: present register: server - with_items: "{{ molecule_yml.platforms }}" + loop: "{{ molecule_yml.platforms }}" async: 7200 poll: 0 @@ -51,20 +50,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 @@ -85,7 +110,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) | molecule_from_yaml }}" - name: Wait for VM to settle down pause: diff --git a/molecule/centos7/destroy.yml b/molecule/centos7/destroy.yml index ecf2ed3..b78d10c 100644 --- a/molecule/centos7/destroy.yml +++ b/molecule/centos7/destroy.yml @@ -23,7 +23,7 @@ api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" state: absent register: server - with_items: "{{ instance_conf }}" + loop: "{{ instance_conf }}" when: not skip_instances async: 7200 poll: 0 @@ -34,7 +34,28 @@ register: hetzner_jobs until: hetzner_jobs.finished 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 hcloud_ssh_key: @@ -42,7 +63,7 @@ state: absent when: - not skip_instances - - (instance_conf | default([])) | length > 0 # must contain at least one instance + - (instance_conf | default([])) | length > 0 # must contain at least one instance # Mandatory configuration for Molecule to function. diff --git a/molecule/centos7/molecule.yml b/molecule/centos7/molecule.yml index b616790..048fdc3 100644 --- a/molecule/centos7/molecule.yml +++ b/molecule/centos7/molecule.yml @@ -10,6 +10,7 @@ platforms: - name: centos7-lvm image: centos-7 server_type: cx11 + volume: True lint: | /usr/local/bin/flake8 provisioner: diff --git a/molecule/centos8/converge.yml b/molecule/centos8/converge.yml index 1fd2959..2d96ca3 100644 --- a/molecule/centos8/converge.yml +++ b/molecule/centos8/converge.yml @@ -7,7 +7,7 @@ - name: lv_test group: vg_test disks: - - /dev/sda + - /dev/sdb fstype: ext4 size: 1g resizefs: True diff --git a/molecule/centos8/create.yml b/molecule/centos8/create.yml index 6c1d373..0117c6c 100644 --- a/molecule/centos8/create.yml +++ b/molecule/centos8/create.yml @@ -33,7 +33,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) }}" @@ -41,7 +40,7 @@ api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" state: present register: server - with_items: "{{ molecule_yml.platforms }}" + loop: "{{ molecule_yml.platforms }}" async: 7200 poll: 0 @@ -51,20 +50,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 @@ -85,7 +110,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) | molecule_from_yaml }}" - name: Wait for VM to settle down pause: diff --git a/molecule/centos8/destroy.yml b/molecule/centos8/destroy.yml index ecf2ed3..b78d10c 100644 --- a/molecule/centos8/destroy.yml +++ b/molecule/centos8/destroy.yml @@ -23,7 +23,7 @@ api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" state: absent register: server - with_items: "{{ instance_conf }}" + loop: "{{ instance_conf }}" when: not skip_instances async: 7200 poll: 0 @@ -34,7 +34,28 @@ register: hetzner_jobs until: hetzner_jobs.finished 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 hcloud_ssh_key: @@ -42,7 +63,7 @@ state: absent when: - not skip_instances - - (instance_conf | default([])) | length > 0 # must contain at least one instance + - (instance_conf | default([])) | length > 0 # must contain at least one instance # Mandatory configuration for Molecule to function. diff --git a/molecule/centos8/molecule.yml b/molecule/centos8/molecule.yml index c9f6375..7aca7d3 100644 --- a/molecule/centos8/molecule.yml +++ b/molecule/centos8/molecule.yml @@ -10,6 +10,7 @@ platforms: - name: centos8-lvm image: centos-8 server_type: cx11 + volume: True lint: | /usr/local/bin/flake8 provisioner: