Merge pull request #33 from hetznercloud/remove-duplicated-playbooks
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Remove duplicated playbook code
This commit is contained in:
commit
22467fd1f4
@ -1,158 +0,0 @@
|
|||||||
---
|
|
||||||
{% raw -%}
|
|
||||||
- name: Create
|
|
||||||
hosts: localhost
|
|
||||||
connection: local
|
|
||||||
gather_facts: false
|
|
||||||
no_log: "{{ molecule_no_log }}"
|
|
||||||
vars:
|
|
||||||
ssh_port: 22
|
|
||||||
ssh_user: root
|
|
||||||
ssh_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
|
|
||||||
tasks:
|
|
||||||
- name: Create SSH key
|
|
||||||
openssh_keypair:
|
|
||||||
path: "{{ ssh_path }}"
|
|
||||||
force: true
|
|
||||||
register: generated_ssh_key
|
|
||||||
|
|
||||||
- name: Register the SSH key name
|
|
||||||
set_fact:
|
|
||||||
ssh_key_name: "molecule-generated-{{ 12345 | random | to_uuid }}"
|
|
||||||
|
|
||||||
- name: Register SSH key for test instance(s)
|
|
||||||
hcloud_ssh_key:
|
|
||||||
name: "{{ ssh_key_name }}"
|
|
||||||
public_key: "{{ generated_ssh_key.public_key }}"
|
|
||||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
||||||
state: present
|
|
||||||
|
|
||||||
- name: Create molecule instance(s)
|
|
||||||
hcloud_server:
|
|
||||||
name: "{{ item.name }}"
|
|
||||||
server_type: "{{ item.server_type }}"
|
|
||||||
ssh_keys:
|
|
||||||
- "{{ ssh_key_name }}"
|
|
||||||
image: "{{ item.image }}"
|
|
||||||
location: "{{ item.location | default(omit) }}"
|
|
||||||
datacenter: "{{ item.datacenter | default(omit) }}"
|
|
||||||
user_data: "{{ item.user_data | default(omit) }}"
|
|
||||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
||||||
state: present
|
|
||||||
register: server
|
|
||||||
with_items: "{{ molecule_yml.platforms }}"
|
|
||||||
async: 7200
|
|
||||||
poll: 0
|
|
||||||
|
|
||||||
- name: Wait for instance(s) creation to complete
|
|
||||||
async_status:
|
|
||||||
jid: "{{ item.ansible_job_id }}"
|
|
||||||
register: hetzner_jobs
|
|
||||||
until: hetzner_jobs.finished
|
|
||||||
retries: 300
|
|
||||||
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: Create private network(s)
|
|
||||||
hcloud_network:
|
|
||||||
name: "{{ item.name }}"
|
|
||||||
ip_range: "{{ item.ip_range | default(omit) }}"
|
|
||||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
||||||
state: "present"
|
|
||||||
loop: "{{ molecule_yml.platforms|molecule_get_hetznercloud_networks('networks') }}"
|
|
||||||
register: networks
|
|
||||||
async: 7200
|
|
||||||
poll: 0
|
|
||||||
|
|
||||||
- name: Wait for network(s) creation to complete
|
|
||||||
async_status:
|
|
||||||
jid: "{{ item.ansible_job_id }}"
|
|
||||||
register: hetzner_networks
|
|
||||||
until: hetzner_networks.finished
|
|
||||||
retries: 300
|
|
||||||
when:
|
|
||||||
- networks is defined
|
|
||||||
- networks.changed
|
|
||||||
with_items: "{{ networks.results }}"
|
|
||||||
|
|
||||||
- name: Create private subnetwork(s)
|
|
||||||
hcloud_subnetwork:
|
|
||||||
network: "{{ item.network_name }}"
|
|
||||||
ip_range: "{{ item.ip|ipaddr('network/prefix') }}"
|
|
||||||
network_zone: "{{ item.network_zone | default('eu-central') }}"
|
|
||||||
type: "{{ item.type | default('cloud') }}"
|
|
||||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
||||||
state: "present"
|
|
||||||
loop: "{{ molecule_yml.platforms|molecule_get_hetznercloud_networks('subnetworks') }}"
|
|
||||||
register: subnetworks
|
|
||||||
|
|
||||||
- name: Attach Server to Subnetwork(s)
|
|
||||||
hcloud_server_network:
|
|
||||||
network: "{{ item.network_name }}"
|
|
||||||
server: "{{ item.server_name }}"
|
|
||||||
ip: "{{ item.ip|ipaddr('address') }}"
|
|
||||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
||||||
state: "present"
|
|
||||||
loop: "{{ molecule_yml.platforms|molecule_get_hetznercloud_networks('subnetworks') }}"
|
|
||||||
|
|
||||||
- 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 }}",
|
|
||||||
'volumes': "{{ item.item.item.volumes | default({}) }}",
|
|
||||||
'networks': "{{ item.item.item.networks | default({}) | dict2items(key_name='name', value_name='data') }}", }
|
|
||||||
with_items: "{{ hetzner_jobs.results }}"
|
|
||||||
register: instance_config_dict
|
|
||||||
when: server.changed | bool
|
|
||||||
|
|
||||||
- name: Convert instance config dict to a list
|
|
||||||
set_fact:
|
|
||||||
instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
|
|
||||||
when: server.changed | bool
|
|
||||||
|
|
||||||
- name: Dump instance config
|
|
||||||
copy:
|
|
||||||
content: |
|
|
||||||
# Molecule managed
|
|
||||||
|
|
||||||
{{ instance_conf | to_json | from_json | to_yaml }}
|
|
||||||
dest: "{{ molecule_instance_config }}"
|
|
||||||
when: server.changed | bool
|
|
||||||
|
|
||||||
- name: Wait for SSH
|
|
||||||
wait_for:
|
|
||||||
port: "{{ ssh_port }}"
|
|
||||||
host: "{{ item.address }}"
|
|
||||||
search_regex: SSH
|
|
||||||
delay: 10
|
|
||||||
with_items: "{{ lookup('file', molecule_instance_config) | from_yaml }}"
|
|
||||||
{%- endraw %}
|
|
@ -1,92 +0,0 @@
|
|||||||
---
|
|
||||||
{% raw -%}
|
|
||||||
- name: Destroy
|
|
||||||
hosts: localhost
|
|
||||||
connection: local
|
|
||||||
gather_facts: false
|
|
||||||
no_log: "{{ molecule_no_log }}"
|
|
||||||
tasks:
|
|
||||||
- name: Populate the instance config
|
|
||||||
block:
|
|
||||||
- name: Populate instance config from file
|
|
||||||
set_fact:
|
|
||||||
instance_conf: "{{ lookup('file', molecule_instance_config) | 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)
|
|
||||||
hcloud_server:
|
|
||||||
name: "{{ item.instance }}"
|
|
||||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
||||||
state: absent
|
|
||||||
register: server
|
|
||||||
with_items: "{{ instance_conf }}"
|
|
||||||
when: not skip_instances
|
|
||||||
async: 7200
|
|
||||||
poll: 0
|
|
||||||
|
|
||||||
- name: Wait for instance(s) deletion to complete
|
|
||||||
async_status:
|
|
||||||
jid: "{{ item.ansible_job_id }}"
|
|
||||||
register: hetzner_jobs
|
|
||||||
until: hetzner_jobs.finished
|
|
||||||
retries: 300
|
|
||||||
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: Destroy network(s)
|
|
||||||
hcloud_network:
|
|
||||||
name: "{{ item.1.name }}"
|
|
||||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
||||||
state: absent
|
|
||||||
register: networks
|
|
||||||
loop: "{{ instance_conf|subelements('networks', skip_missing=True) }}"
|
|
||||||
|
|
||||||
- name: Remove registered SSH key
|
|
||||||
hcloud_ssh_key:
|
|
||||||
name: "{{ instance_conf[0].ssh_key_name }}"
|
|
||||||
api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}"
|
|
||||||
state: absent
|
|
||||||
when:
|
|
||||||
- not skip_instances
|
|
||||||
- instance_conf | length # must contain at least one instance
|
|
||||||
|
|
||||||
- name: Populate instance config
|
|
||||||
set_fact:
|
|
||||||
instance_conf: {}
|
|
||||||
|
|
||||||
- name: Dump instance config
|
|
||||||
copy:
|
|
||||||
content: |
|
|
||||||
# Molecule managed
|
|
||||||
|
|
||||||
{{ instance_conf | to_json | from_json | to_yaml }}
|
|
||||||
dest: "{{ molecule_instance_config }}"
|
|
||||||
when: server.changed | bool
|
|
||||||
{%- endraw %}
|
|
Loading…
Reference in New Issue
Block a user