Skip to content

Commit

Permalink
Enable image by owner and name (#1884)
Browse files Browse the repository at this point in the history
* Enable image by owner and name

Signed-off-by: Gustavo Muniz do Carmo <[email protected]>

* drop conditional onto multiple lines

Signed-off-by: Gustavo Muniz do Carmo <[email protected]>

* new feature functional testing

Signed-off-by: Gustavo Muniz do Carmo <[email protected]>

* new feature configuration examples

Signed-off-by: Gustavo Muniz do Carmo <[email protected]>

* get the latest image

Signed-off-by: Gustavo Muniz do Carmo <[email protected]>
  • Loading branch information
gustavomcarmo authored and ssbarnea committed Apr 9, 2019
1 parent 5c93b24 commit 38c9bf2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand All @@ -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

Expand Down
38 changes: 38 additions & 0 deletions molecule/driver/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]'
Expand Down
17 changes: 15 additions & 2 deletions test/resources/playbooks/ec2/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand All @@ -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

Expand Down

0 comments on commit 38c9bf2

Please sign in to comment.