xoxys.zigbee2mqtt/molecule/centos7/create.yml

88 lines
2.6 KiB
YAML
Raw Normal View History

2019-08-06 18:59:40 +00:00
---
- name: Create
hosts: localhost
connection: local
gather_facts: false
2019-10-19 14:47:04 +00:00
no_log: "{{ molecule_no_log }}"
2019-08-06 18:59:40 +00:00
vars:
2019-10-19 14:47:04 +00:00
ssh_user: root
2019-08-06 18:59:40 +00:00
ssh_port: 22
keypair_name: molecule_key
keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
tasks:
2019-10-19 14:47:04 +00:00
- name: Create local keypair
user:
name: "{{ lookup('env', 'USER') }}"
generate_ssh_key: true
ssh_key_file: "{{ keypair_path }}"
register: local_keypair
2019-08-06 18:59:40 +00:00
2019-10-19 14:47:04 +00:00
- name: Create remote keypair
digital_ocean_sshkey:
2019-08-06 18:59:40 +00:00
name: "{{ keypair_name }}"
2019-10-19 14:47:04 +00:00
ssh_pub_key: "{{ local_keypair.ssh_public_key }}"
state: present
register: remote_keypair
2019-08-06 18:59:40 +00:00
- name: Create molecule instance(s)
2019-10-19 14:47:04 +00:00
digital_ocean_droplet:
name: "{{ item.name }}"
unique_name: true
region: "{{ item.region_id }}"
image: "{{ item.image_id }}"
size: "{{ item.size_id }}"
ssh_keys: "{{ remote_keypair.data.ssh_key.id }}"
2019-08-06 18:59:40 +00:00
wait: true
2019-10-19 14:47:04 +00:00
wait_timeout: 300
state: present
2019-08-06 18:59:40 +00:00
register: server
2019-10-19 14:47:04 +00:00
loop: "{{ molecule_yml.platforms }}"
2019-08-06 18:59:40 +00:00
async: 7200
poll: 0
- name: Wait for instance(s) creation to complete
async_status:
jid: "{{ item.ansible_job_id }}"
2019-10-19 14:47:04 +00:00
register: digitalocean_jobs
until: digitalocean_jobs.finished
2019-08-06 18:59:40 +00:00
retries: 300
2019-10-19 14:47:04 +00:00
loop: "{{ server.results }}"
2019-08-06 18:59:40 +00:00
# Mandatory configuration for Molecule to function.
- name: Populate instance config dict
set_fact:
instance_conf_dict: {
2019-10-19 14:47:04 +00:00
'instance': "{{ item.data.droplet.name }}",
'address': "{{ item.data.ip_address }}",
2019-08-06 18:59:40 +00:00
'user': "{{ ssh_user }}",
'port': "{{ ssh_port }}",
'identity_file': "{{ keypair_path }}",
2019-10-19 14:47:04 +00:00
'droplet_id': "{{ item.data.droplet.id }}",
'ssh_key_id': "{{ remote_keypair.data.ssh_key.id }}",
}
loop: "{{ digitalocean_jobs.results }}"
2019-08-06 18:59:40 +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: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
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
timeout: 320
2019-10-19 14:47:04 +00:00
loop: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"