molecule/openstack/create.yml
---
- name: Create
hosts: localhost
connection: local
gather_facts: False
no_log: "{{ not lookup('env', 'MOLECULE_DEBUG') | bool }}"
vars:
molecule_file: "{{ lookup('env', 'MOLECULE_FILE') }}"
molecule_instance_config: "{{ lookup('env', 'MOLECULE_INSTANCE_CONFIG') }}"
molecule_yml: "{{ lookup('file', molecule_file) | from_yaml }}"
user: cloud-user
ssh_port: 22
security_group_name: molecule
security_group_description: "Security group for testing Molecule"
security_group_rules:
- { proto: 'tcp', port: "{{ ssh_port }}", cidr: '0.0.0.0/0' }
- { proto: 'icmp', port: -1, cidr: '0.0.0.0/0' }
- { ethertype: 'IPv4', group: "{{ security_group.id }}" }
- { ethertype: 'IPv6', group: "{{ security_group.id }}" }
neutron_network_name: molecule
keypair_name: molecule_key
keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
nova_image: Ubuntu-16.04
nova_flavor: NO-Nano
tasks:
- include: security_groups.yml
- include: keypair.yml
- name: Gather facts about network for use with instance creation
os_networks_facts:
name: "{{ neutron_network_name }}"
- name: Create molecule instance(s)
os_server:
name: "{{ item.name }}"
image: "{{ item.image }}"
flavor: "{{ item.flavor }}"
security_groups: "{{ security_group_name }}"
key_name: "{{ keypair_name }}"
nics:
- net-id: "{{ openstack_networks[0]['id'] }}"
register: server
with_items: "{{ molecule_yml.platforms }}"
# Mandatory configuration for Molecule to function.
- name: Populate instance config dict
set_fact:
instance_conf_dict: {
'instance': "{{ item.openstack.name }}",
'address': "{{ item.openstack.accessIPv4 }}",
'user': "{{ user }}",
'port': "{{ ssh_port }}",
'identity_file': "{{ keypair_path }}", }
with_items: "{{ server.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:
# NOTE(retr0h): Workaround for Ansible 2.2.
# https://github.com/ansible/ansible/issues/20885
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
with_items: "{{ lookup('file', molecule_instance_config) | from_yaml }}"