|
| 1 | +--- |
| 2 | +- name: Create |
| 3 | + hosts: localhost |
| 4 | + connection: local |
| 5 | + gather_facts: false |
| 6 | + no_log: "{{ molecule_no_log }}" |
| 7 | + vars: |
| 8 | + ssh_port: 22 |
| 9 | + ssh_user: root |
| 10 | + ssh_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key" |
| 11 | + tasks: |
| 12 | + - name: Create SSH key |
| 13 | + user: |
| 14 | + name: "{{ lookup('env', 'USER') }}" |
| 15 | + generate_ssh_key: true |
| 16 | + ssh_key_file: "{{ ssh_path }}" |
| 17 | + force: true |
| 18 | + register: generated_ssh_key |
| 19 | + |
| 20 | + - name: Register the SSH key name |
| 21 | + set_fact: |
| 22 | + ssh_key_name: "molecule-generated-{{ 12345 | random | to_uuid }}" |
| 23 | + |
| 24 | + - name: Register SSH key for test instance(s) |
| 25 | + hcloud_ssh_key: |
| 26 | + name: "{{ ssh_key_name }}" |
| 27 | + public_key: "{{ generated_ssh_key.ssh_public_key }}" |
| 28 | + state: present |
| 29 | + |
| 30 | + - name: Create molecule instance(s) |
| 31 | + hcloud_server: |
| 32 | + name: "{{ item.name }}" |
| 33 | + server_type: "{{ item.server_type }}" |
| 34 | + ssh_keys: |
| 35 | + - "{{ ssh_key_name }}" |
| 36 | + image: "{{ item.image }}" |
| 37 | + location: "{{ item.location | default(omit) }}" |
| 38 | + datacenter: "{{ item.datacenter | default(omit) }}" |
| 39 | + user_data: "{{ item.user_data | default(omit) }}" |
| 40 | + api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" |
| 41 | + state: present |
| 42 | + register: server |
| 43 | + loop: "{{ molecule_yml.platforms }}" |
| 44 | + async: 7200 |
| 45 | + poll: 0 |
| 46 | + |
| 47 | + - name: Wait for instance(s) creation to complete |
| 48 | + async_status: |
| 49 | + jid: "{{ item.ansible_job_id | string }}" |
| 50 | + register: hetzner_jobs |
| 51 | + until: hetzner_jobs.finished |
| 52 | + retries: 300 |
| 53 | + loop: "{{ server.results }}" |
| 54 | + |
| 55 | + - name: Create volume(s) |
| 56 | + hcloud_volume: |
| 57 | + name: "{{ item.name }}" |
| 58 | + server: "{{ item.name }}" |
| 59 | + location: "{{ item.location | default(omit) }}" |
| 60 | + size: "{{ item.volume_size | default(10) }}" |
| 61 | + api_token: "{{ lookup('env', 'HCLOUD_TOKEN') }}" |
| 62 | + state: "present" |
| 63 | + loop: "{{ molecule_yml.platforms }}" |
| 64 | + when: item.volume | default(False) | bool |
| 65 | + register: volumes |
| 66 | + async: 7200 |
| 67 | + poll: 0 |
| 68 | + |
| 69 | + - name: Wait for volume(s) creation to complete |
| 70 | + async_status: |
| 71 | + jid: "{{ item.ansible_job_id | string }}" |
| 72 | + register: hetzner_volumes |
| 73 | + until: hetzner_volumes.finished |
| 74 | + retries: 300 |
| 75 | + when: volumes.changed |
| 76 | + loop: "{{ volumes.results }}" |
| 77 | + |
| 78 | + # Mandatory configuration for Molecule to function. |
| 79 | + |
| 80 | + - name: Populate instance config dict |
| 81 | + set_fact: |
| 82 | + instance_conf_dict: |
| 83 | + { |
| 84 | + "instance": "{{ item.hcloud_server.name }}", |
| 85 | + "ssh_key_name": "{{ ssh_key_name }}", |
| 86 | + "address": "{{ item.hcloud_server.ipv4_address }}", |
| 87 | + "user": "{{ ssh_user }}", |
| 88 | + "port": "{{ ssh_port }}", |
| 89 | + "identity_file": "{{ ssh_path }}", |
| 90 | + "volume": "{{ item.item.item.volume | default(False) | bool }}", |
| 91 | + "from_snapshot": "{{ item.item.item.from_snapshot | default(False) | bool }}", |
| 92 | + } |
| 93 | + loop: "{{ hetzner_jobs.results }}" |
| 94 | + register: instance_config_dict |
| 95 | + when: server.changed | bool |
| 96 | + |
| 97 | + - name: Convert instance config dict to a list |
| 98 | + set_fact: |
| 99 | + instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" |
| 100 | + when: server.changed | bool |
| 101 | + |
| 102 | + - name: Dump instance config |
| 103 | + copy: |
| 104 | + content: | |
| 105 | + # Molecule managed |
| 106 | +
|
| 107 | + {{ instance_conf | to_nice_yaml(indent=2) }} |
| 108 | + dest: "{{ molecule_instance_config }}" |
| 109 | + when: server.changed | bool |
| 110 | + |
| 111 | + - name: Wait for SSH |
| 112 | + wait_for: |
| 113 | + port: "{{ ssh_port }}" |
| 114 | + host: "{{ item.address }}" |
| 115 | + search_regex: SSH |
| 116 | + delay: 10 |
| 117 | + loop: "{{ lookup('file', molecule_instance_config) | from_yaml }}" |
| 118 | + |
| 119 | + - name: Wait for VM to settle down |
| 120 | + pause: |
| 121 | + seconds: 30 |
0 commit comments