From 38c9bf232bbea27a4f340479496e129f3be18f43 Mon Sep 17 00:00:00 2001 From: Gustavo Muniz do Carmo Date: Tue, 9 Apr 2019 13:09:38 +0100 Subject: [PATCH] Enable image by owner and name (#1884) * Enable image by owner and name Signed-off-by: Gustavo Muniz do Carmo * drop conditional onto multiple lines Signed-off-by: Gustavo Muniz do Carmo * new feature functional testing Signed-off-by: Gustavo Muniz do Carmo * new feature configuration examples Signed-off-by: Gustavo Muniz do Carmo * get the latest image Signed-off-by: Gustavo Muniz do Carmo --- .../{{cookiecutter.scenario_name}}/create.yml | 17 ++++++++- molecule/driver/ec2.py | 38 +++++++++++++++++++ test/resources/playbooks/ec2/create.yml | 17 ++++++++- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/molecule/cookiecutter/scenario/driver/ec2/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/create.yml b/molecule/cookiecutter/scenario/driver/ec2/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/create.yml index 789d9f6dbe..f22c5d8b61 100644 --- a/molecule/cookiecutter/scenario/driver/ec2/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/create.yml +++ b/molecule/cookiecutter/scenario/driver/ec2/{{cookiecutter.molecule_directory}}/{{cookiecutter.scenario_name}}/create.yml @@ -59,10 +59,21 @@ mode: 0600 when: keypair.changed + - name: Get the ec2 ami(s) by owner and name, if image not set + ec2_ami_facts: + owners: "{{ item.image_owner }}" + filters: + name: "{{ item.image_name }}" + loop: "{{ molecule_yml.platforms }}" + when: item.image is not defined + register: ami_facts + - name: Create molecule instance(s) ec2: key_name: "{{ keypair_name }}" - image: "{{ item.image }}" + image: "{{ item.image + if item.image is defined + else (ami_facts.results[index].images | sort(attribute='creation_date', reverse=True))[0].image_id }}" instance_type: "{{ item.instance_type }}" vpc_subnet_id: "{{ item.vpc_subnet_id }}" group: "{{ security_group_name }}" @@ -74,7 +85,9 @@ count_tag: instance: "{{ item.name }}" register: server - with_items: "{{ molecule_yml.platforms }}" + loop: "{{ molecule_yml.platforms }}" + loop_control: + index_var: index async: 7200 poll: 0 diff --git a/molecule/driver/ec2.py b/molecule/driver/ec2.py index c836e8446f..a7b81b24a0 100644 --- a/molecule/driver/ec2.py +++ b/molecule/driver/ec2.py @@ -43,6 +43,44 @@ class EC2(base.Base): platforms: - name: instance + Some configuration examples: + + .. code-block:: yaml + + driver: + name: ec2 + platforms: + - name: instance + image: ami-0311dc90a352b25f4 + instance_type: t2.micro + vpc_subnet_id: subnet-1cb17175 + + If you don't know the AMI code or want to avoid hardcoding it: + + .. code-block:: yaml + + driver: + name: ec2 + platforms: + - name: instance + image_owner: 099720109477 + image_name: ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-20190320 + instance_type: t2.micro + vpc_subnet_id: subnet-1cb17175 + + Use wildcards for getting the latest image. For example, the latest Ubuntu bionic image: + + .. code-block:: yaml + + driver: + name: ec2 + platforms: + - name: instance + image_owner: 099720109477 + image_name: ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-* + instance_type: t2.micro + vpc_subnet_id: subnet-1cb17175 + .. code-block:: bash $ pip install 'molecule[ec2]' diff --git a/test/resources/playbooks/ec2/create.yml b/test/resources/playbooks/ec2/create.yml index 688b0e38d8..fe4879c9b2 100644 --- a/test/resources/playbooks/ec2/create.yml +++ b/test/resources/playbooks/ec2/create.yml @@ -66,10 +66,21 @@ mode: 0600 when: keypair.changed + - name: Get the ec2 ami(s) by owner and name, if image not set + ec2_ami_facts: + owners: "{{ item.image_owner }}" + filters: + name: "{{ item.image_name }}" + loop: "{{ molecule_yml.platforms }}" + when: item.image is not defined + register: ami_facts + - name: Create molecule instance(s) ec2: key_name: "{{ keypair_name }}" - image: "{{ item.image }}" + image: "{{ item.image + if item.image is defined + else (ami_facts.results[index].images | sort(attribute='creation_date', reverse=True))[0].image_id }}" instance_type: "{{ item.instance_type }}" vpc_subnet_id: "{{ item.vpc_subnet_id }}" group: "{{ security_group_name }}" @@ -81,7 +92,9 @@ count_tag: instance: "{{ item.name }}" register: server - with_items: "{{ molecule_yml.platforms }}" + loop: "{{ molecule_yml.platforms }}" + loop_control: + index_var: index async: 7200 poll: 0