xoxys.kernel/molecule/centos7/create.yml

121 lines
3.6 KiB
YAML
Raw Normal View History

2019-11-03 23:22:18 +00:00
---
- name: Create
hosts: localhost
connection: local
gather_facts: false
no_log: "{{ molecule_no_log }}"
vars:
ssh_port: 22
2020-10-02 07:32:15 +00:00
ssh_user: root
ssh_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
2019-11-03 23:22:18 +00:00
tasks:
2020-10-02 07:32:15 +00:00
- name: Create SSH key
2019-11-03 23:22:18 +00:00
user:
name: "{{ lookup('env', 'USER') }}"
generate_ssh_key: true
2020-10-02 07:32:15 +00:00
ssh_key_file: "{{ ssh_path }}"
force: true
register: generated_ssh_key
2019-11-03 23:22:18 +00:00
2020-10-02 07:32:15 +00:00
- 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.ssh_public_key }}"
2019-11-03 23:22:18 +00:00
state: present
- name: Create molecule instance(s)
2020-10-02 07:32:15 +00:00
hcloud_server:
2019-11-03 23:22:18 +00:00
name: "{{ item.name }}"
2020-10-02 07:32:15 +00:00
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') }}"
2019-11-03 23:22:18 +00:00
state: present
register: server
2020-10-03 15:12:29 +00:00
loop: "{{ molecule_yml.platforms }}"
2019-11-03 23:22:18 +00:00
async: 7200
poll: 0
- name: Wait for instance(s) creation to complete
async_status:
jid: "{{ item.ansible_job_id }}"
2020-10-02 07:32:15 +00:00
register: hetzner_jobs
until: hetzner_jobs.finished
2019-11-03 23:22:18 +00:00
retries: 300
2020-10-03 15:12:29 +00:00
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 }}"
2019-11-03 23:22:18 +00:00
# Mandatory configuration for Molecule to function.
- name: Populate instance config dict
set_fact:
2020-10-03 15:12:29 +00:00
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 }}"
2019-11-03 23:22:18 +00:00
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_nice_yaml(indent=2) }}
2019-11-03 23:22:18 +00:00
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
loop: "{{ lookup('file', molecule_instance_config) | from_yaml }}"
2020-10-02 07:32:15 +00:00
- name: Wait for VM to settle down
pause:
seconds: 30