ec2_instance test cleanup (#63708)

* ec2_instance/ec2_instance_info : Fixup sanity test errors

* Move ec2_instance integration tests to use aws_defaults

* Search for the AMI instead of hardcoding an AMI

* Make our VPC CIDR variable

* Remove AZ assumptions - no guarantees about specific AZs being available

* Make sure we terminate instances when we're done with them.

* Add a 10 second pause for IAM roles to become available before using them

* Wait on instance changes by default

* Switch out t2 instances for t3 they're cheaper and have more CPU available

* Pull t3.nano instance info a little earlier

* rework vpc_name and vpc_cidr a little

* Mark ec2_instance tests unsupported for now, they take too long
pull/63856/head
Mark Chappell 5 years ago committed by Sloane Hertel
parent aa671be28b
commit 145b79ef0e

@ -24,11 +24,13 @@ options:
instance_ids:
description:
- If you specify one or more instance IDs, only instances that have the specified IDs are returned.
type: list
state:
description:
- Goal state for the instances.
choices: [present, terminated, running, started, stopped, restarted, rebooted, absent]
default: present
type: str
wait:
description:
- Whether or not to wait for the desired state (use wait_timeout to customize this).
@ -38,33 +40,41 @@ options:
description:
- How long to wait (in seconds) for the instance to finish booting/terminating.
default: 600
type: int
instance_type:
description:
- Instance type to use for the instance, see U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html)
Only required when instance is not already present.
default: t2.micro
type: str
user_data:
description:
- Opaque blob of data which is made available to the ec2 instance
type: str
tower_callback:
description:
- Preconfigured user-data to enable an instance to perform a Tower callback (Linux only).
- Mutually exclusive with I(user_data).
- For Windows instances, to enable remote access via Ansible set I(tower_callback.windows) to true, and optionally set an admin password.
- If using 'windows' and 'set_password', callback to Tower will not be performed but the instance will be ready to receive winrm connections from Ansible.
type: dict
suboptions:
tower_address:
description:
- IP address or DNS name of Tower server. Must be accessible via this address from the VPC that this instance will be launched in.
type: str
job_template_id:
description:
- Either the integer ID of the Tower Job Template, or the name (name supported only for Tower 3.2+).
type: str
host_config_key:
description:
- Host configuration secret key generated by the Tower job template.
type: str
tags:
description:
- A hash/dictionary of tags to add to the new instance or to add/remove from an existing one.
type: dict
purge_tags:
description:
- Delete any tags not specified in the task that are on the instance.
@ -75,65 +85,136 @@ options:
description:
- An image to use for the instance. The M(ec2_ami_info) module may be used to retrieve images.
One of I(image) or I(image_id) are required when instance is not already present.
- Complex object containing I(image.id), I(image.ramdisk), and I(image.kernel).
- I(image.id) is the AMI ID.
- I(image.ramdisk) overrides the AMI's default ramdisk ID.
- I(image.kernel) is a string AKI to override the AMI kernel.
type: dict
suboptions:
id:
description:
- The AMI ID.
type: str
ramdisk:
description:
- Overrides the AMI's default ramdisk ID.
type: str
kernel:
description:
- a string AKI to override the AMI kernel.
image_id:
description:
- I(ami) ID to use for the instance. One of I(image) or I(image_id) are required when instance is not already present.
- This is an alias for I(image.id).
type: str
security_groups:
description:
- A list of security group IDs or names (strings). Mutually exclusive with I(security_group).
type: list
security_group:
description:
- A security group ID or name. Mutually exclusive with I(security_groups).
type: str
name:
description:
- The Name tag for the instance.
type: str
vpc_subnet_id:
description:
- The subnet ID in which to launch the instance (VPC)
If none is provided, ec2_instance will chose the default zone of the default VPC.
aliases: ['subnet_id']
type: str
network:
description:
- Either a dictionary containing the key 'interfaces' corresponding to a list of network interface IDs or
containing specifications for a single network interface.
- If specifications for a single network are given, accepted keys are assign_public_ip (bool),
private_ip_address (str), ipv6_addresses (list), source_dest_check (bool), description (str),
delete_on_termination (bool), device_index (int), groups (list of security group IDs),
private_ip_addresses (list), subnet_id (str).
- I(network.interfaces) should be a list of ENI IDs (strings) or a list of objects containing the key I(id).
- Use the ec2_eni to create ENIs with special settings.
- Use the ec2_eni module to create ENIs with special settings.
type: dict
suboptions:
interfaces:
description:
- a list of ENI IDs (strings) or a list of objects containing the key I(id).
type: list
assign_public_ip:
description:
- when true assigns a public IP address to the interface
type: bool
private_ip_address:
description:
- an IPv4 address to assign to the interface
type: str
ipv6_addresses:
description:
- a list of IPv6 addresses to assign to the network interface
type: list
source_dest_check:
description:
- controls whether source/destination checking is enabled on the interface
type: bool
description:
description:
- a description for the network interface
type: str
private_ip_addresses:
description:
- a list of IPv4 addresses to assign to the network interface
type: list
subnet_id:
description:
- the subnet to connect the network interface to
type: str
delete_on_termination:
description:
- Delete the interface when the instance it is attached to is
terminated.
type: bool
device_index:
description:
- The index of the interface to modify
type: int
groups:
description:
- a list of security group IDs to attach to the interface
type: list
volumes:
description:
- A list of block device mappings, by default this will always use the AMI root device so the volumes option is primarily for adding more storage.
- A mapping contains the (optional) keys device_name, virtual_name, ebs.volume_type, ebs.volume_size, ebs.kms_key_id,
ebs.iops, and ebs.delete_on_termination.
- For more information about each parameter, see U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_BlockDeviceMapping.html).
type: list
launch_template:
description:
- The EC2 launch template to base instance configuration on.
- I(launch_template.id) the ID or the launch template (optional if name is specified).
- I(launch_template.name) the pretty name of the launch template (optional if id is specified).
- I(launch_template.version) the specific version of the launch template to use. If unspecified, the template default is chosen.
type: dict
suboptions:
id:
description:
- the ID of the launch template (optional if name is specified).
type: str
name:
description:
- the pretty name of the launch template (optional if id is specified).
type: str
version:
description:
- the specific version of the launch template to use. If unspecified, the template default is chosen.
key_name:
description:
- Name of the SSH access key to assign to the instance - must exist in the region the instance is created.
type: str
availability_zone:
description:
- Specify an availability zone to use the default subnet it. Useful if not specifying the I(vpc_subnet_id) parameter.
- If no subnet, ENI, or availability zone is provided, the default subnet in the default VPC will be used in the first AZ (alphabetically sorted).
type: str
instance_initiated_shutdown_behavior:
description:
- Whether to stop or terminate an instance upon shutdown.
choices: ['stop', 'terminate']
type: str
tenancy:
description:
- What type of tenancy to allow an instance to use. Default is shared tenancy. Dedicated tenancy will incur additional charges.
choices: ['dedicated', 'default']
type: str
termination_protection:
description:
- Whether to enable termination protection.
@ -143,7 +224,8 @@ options:
description:
- For T2 series instances, choose whether to allow increased charges to buy CPU credits if the default pool is depleted.
- Choose I(unlimited) to enable buying additional CPU credits.
choices: [unlimited, standard]
choices: ['unlimited', 'standard']
type: str
cpu_options:
description:
- Reduce the number of vCPU exposed to the instance.
@ -151,16 +233,19 @@ options:
- See U(https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-optimize-cpu.html) for combinations available.
- Requires botocore >= 1.10.16
version_added: 2.7
type: dict
suboptions:
threads_per_core:
description:
- Select the number of threads per core to enable. Disable or Enable Intel HT.
choices: [1, 2]
required: true
type: int
core_count:
description:
- Set the number of core to enable.
required: true
type: int
detailed_monitoring:
description:
- Whether to allow detailed cloudwatch metrics to be collected, enabling more detailed alerting.
@ -175,19 +260,21 @@ options:
consists of a filter key and a filter value. See
U(https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html).
for possible filters. Filter names and values are case sensitive.
By default, instances are filtered for counting by their "Name" tag, base AMI, state (running, by default), and
- By default, instances are filtered for counting by their "Name" tag, base AMI, state (running, by default), and
subnet ID. Any queryable filter can be used. Good candidates are specific tags, SSH keys, or security groups.
default: {"tag:Name": "<provided-Name-attribute>", "subnet-id": "<provided-or-default subnet>"}
type: dict
instance_role:
description:
- The ARN or name of an EC2-enabled instance role to be used. If a name is not provided in arn format
then the ListInstanceProfiles permission must also be granted.
U(https://docs.aws.amazon.com/IAM/latest/APIReference/API_ListInstanceProfiles.html) If no full ARN is provided,
the role with a matching name will be used from the active AWS account.
type: str
placement_group:
description:
- The placement group that needs to be assigned to the instance
version_added: 2.8
type: str
extends_documentation_fragment:
- aws
@ -493,13 +580,14 @@ instances:
ipv6_addresses:
description: One or more IPv6 addresses associated with the network interface.
returned: always
type: complex
type: list
elements: dict
contains:
- ipv6_address:
description: The IPv6 address.
returned: always
type: str
sample: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
ipv6_address:
description: The IPv6 address.
returned: always
type: str
sample: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
mac_address:
description: The MAC address.
returned: always

@ -28,6 +28,7 @@ options:
- If you specify one or more instance IDs, only instances that have the specified IDs are returned.
required: false
version_added: 2.4
type: list
filters:
description:
- A dict of filters to apply. Each dict item consists of a filter key and a filter value. See
@ -35,6 +36,7 @@ options:
names and values are case sensitive.
required: false
default: {}
type: dict
extends_documentation_fragment:
- aws
@ -279,13 +281,14 @@ instances:
ipv6_addresses:
description: One or more IPv6 addresses associated with the network interface.
returned: always
type: complex
type: list
elements: dict
contains:
- ipv6_address:
description: The IPv6 address.
returned: always
type: str
sample: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
ipv6_address:
description: The IPv6 address.
returned: always
type: str
sample: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
mac_address:
description: The MAC address.
returned: always

@ -1,3 +1,4 @@
ec2_instance_info
cloud/aws
shippable/aws/group2
unstable
#shippable/aws/group2
unsupported

@ -2,22 +2,13 @@
# defaults file for ec2_instance
ec2_instance_name: '{{ resource_prefix }}-node'
ec2_instance_owner: 'integration-run-{{ resource_prefix }}'
ec2_ami_image:
# Amazon Linux - collected 2019-05-10
ap-northeast-1: ami-086063d8c95bfa211
ap-northeast-2: ami-04a1d511fd8937540
ap-south-1: ami-0d1ca7565b637e9a5
ap-southeast-1: ami-07dd66a9764fbb937
ap-southeast-2: ami-0ca7144d77a93c7a8
ca-central-1: ami-0e5e1c6b373fed93b
eu-central-1: ami-05ba799ee5fc165db
eu-west-1: ami-055ff4f3e62e8e65e
eu-west-2: ami-072edd63fd2b95ca7
sa-east-1: ami-0c0698b4027cd5931
us-east-1: ami-0771c28c5580e5716
us-east-2: ami-063225b63017efd17
us-west-1: ami-04abfbe0050a1ef1d
us-west-2: ami-027c5e2ccf2970def
ec2_instance_type: 't3.micro'
ec2_ami_name: 'amzn2-ami-hvm-2.*-x86_64-gp2'
# We need to use ENA enabled AMIs to get EBS optimized instances.
ec2_ebs_optimized_ami_image: "{{ ec2_ami_image }}"
vpc_name: '{{ resource_prefix }}-vpc'
vpc_seed: '{{ resource_prefix }}'
vpc_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.0.0/16'
subnet_a_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.32.0/24'
subnet_a_startswith: '10.{{ 256 | random(seed=vpc_seed) }}.32.'
subnet_b_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.33.0/24'
subnet_b_startswith: '10.{{ 256 | random(seed=vpc_seed) }}.33.'

@ -1,79 +1,80 @@
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- block:
- name: New instance with an extra block device
ec2_instance:
name: "{{ resource_prefix }}-test-ebs-vols"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
volumes:
- device_name: /dev/sdb
ebs:
volume_size: 20
delete_on_termination: true
volume_type: standard
tags:
TestId: "{{ resource_prefix }}"
instance_type: "{{ ec2_instance_type }}"
wait: true
register: in_test_vpc
- name: New instance with an extra block device
ec2_instance:
name: "{{ resource_prefix }}-test-ebs-vols"
image_id: "{{ ec2_ami_image[aws_region] }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
volumes:
- device_name: /dev/sdb
ebs:
volume_size: 20
delete_on_termination: true
volume_type: standard
tags:
TestId: "{{ resource_prefix }}"
instance_type: t2.micro
wait: true
<<: *aws_connection_info
register: in_test_vpc
- name: Gather instance info
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols"
register: in_test_vpc_instance
- name: Gather instance info
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols"
<<: *aws_connection_info
register: in_test_vpc_instance
- assert:
that:
- in_test_vpc is not failed
- in_test_vpc is changed
- in_test_vpc_instance.instances[0].block_device_mappings[0]
- in_test_vpc_instance.instances[0].block_device_mappings[1]
- in_test_vpc_instance.instances[0].block_device_mappings[1].device_name == '/dev/sdb'
- assert:
that:
- in_test_vpc is not failed
- in_test_vpc is changed
- in_test_vpc_instance.instances[0].block_device_mappings[0]
- in_test_vpc_instance.instances[0].block_device_mappings[1]
- in_test_vpc_instance.instances[0].block_device_mappings[1].device_name == '/dev/sdb'
- name: New instance with an extra block device(check mode)
ec2_instance:
name: "{{ resource_prefix }}-test-ebs-vols-checkmode"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
volumes:
- device_name: /dev/sdb
ebs:
volume_size: 20
delete_on_termination: true
volume_type: standard
tags:
TestId: "{{ resource_prefix }}"
instance_type: "{{ ec2_instance_type }}"
check_mode: yes
- name: New instance with an extra block device(check mode)
ec2_instance:
name: "{{ resource_prefix }}-test-ebs-vols-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
volumes:
- device_name: /dev/sdb
ebs:
volume_size: 20
delete_on_termination: true
volume_type: standard
tags:
TestId: "{{ resource_prefix }}"
instance_type: t2.micro
<<: *aws_connection_info
check_mode: yes
- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols"
"instance-state-name": "running"
register: presented_instance_fact
- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols"
"instance-state-name": "running"
<<: *aws_connection_info
register: presented_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols-checkmode"
register: checkmode_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally."
assert:
that:
- "{{ presented_instance_fact.instances | length }} > 0"
- "{{ checkmode_instance_fact.instances | length }} == 0"
- name: "Confirm whether the check mode is working normally."
assert:
that:
- "{{ presented_instance_fact.instances | length }} > 0"
- "{{ checkmode_instance_fact.instances | length }} == 0"
- name: Terminate instances
ec2_instance:
instance_ids: "{{ in_test_vpc.instance_ids }}"
state: absent
always:
- name: Terminate test-ebs-vols instances
ec2_instance:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols"
state: absent
wait: false
ignore_errors: yes

@ -1,46 +1,33 @@
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- block:
- name: Make basic instance
ec2_instance:
name: "{{ resource_prefix }}-checkmode-comparison"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
wait: false
<<: *aws_connection_info
register: basic_instance
- name: Make basic instance(check mode)
ec2_instance:
name: "{{ resource_prefix }}-checkmode-comparison-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
<<: *aws_connection_info
check_mode: yes
- name: fact presented ec2 instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: presented_instance_fact
- name: fact checkmode ec2 instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: Confirm whether the check mode is working normally.
@ -54,14 +41,12 @@
name: "{{ resource_prefix }}-checkmode-comparison"
state: stopped
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
check_mode: yes
- name: fact ec2 instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_checkmode_stopinstance_fact
- name: Verify that it was not stopped.
@ -74,17 +59,14 @@
name: "{{ resource_prefix }}-checkmode-comparison"
state: stopped
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
register: instance_stop
until: not instance_stop.failed
retries: 10
- name: fact stopped ec2 instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_stopinstance_fact
- name: Verify that it was stopped.
@ -97,16 +79,14 @@
name: "{{ resource_prefix }}-checkmode-comparison"
state: running
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
check_mode: yes
- name: fact ec2 instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_checkmode_runninginstance_fact
- name: Verify that it was not running.
assert:
that:
@ -117,13 +97,11 @@
name: "{{ resource_prefix }}-checkmode-comparison"
state: running
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
- name: fact ec2 instance.
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_runninginstance_fact
- name: Verify that it was running.
@ -136,16 +114,14 @@
name: "{{ resource_prefix }}-checkmode-comparison"
state: absent
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
check_mode: yes
- name: fact ec2 instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_checkmode_terminatedinstance_fact
- name: Verify that it was not terminated,
assert:
that:
@ -156,26 +132,24 @@
name: "{{ resource_prefix }}-checkmode-comparison"
state: absent
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
- name: fact ec2 instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_terminatedinstance_fact
- name: Verify that it was terminated,
assert:
that:
- '"{{ confirm_terminatedinstance_fact.instances[0].state.name }}" == "terminated"'
always:
- name: Terminate instance
ec2:
instance_ids: "{{ basic_instance.instance_ids }}"
state: absent
<<: *aws_connection_info
wait: false
register: removed
until: removed is not failed
ignore_errors: yes

@ -1,17 +1,8 @@
- block:
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- name: create t3.nano instance with cpu_options
ec2_instance:
name: "{{ resource_prefix }}-test-t3nano-1-threads-per-core"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
@ -20,7 +11,6 @@
core_count: 1
threads_per_core: 1
wait: false
<<: *aws_connection_info
register: instance_creation
- name: instance with cpu_options created with the right options
@ -33,7 +23,7 @@
ec2_instance:
state: present
name: "{{ resource_prefix }}-test-t3nano-1-threads-per-core"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
@ -42,20 +32,29 @@
core_count: 1
threads_per_core: 2
wait: false
<<: *aws_connection_info
register: cpu_options_update
ignore_errors: yes
- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-t3nano-1-threads-per-core"
register: presented_instance_fact
- name: modify cpu_options has no effect on existing instance
assert:
that:
- cpu_options_update is success
- cpu_options_update is not changed
- "{{ presented_instance_fact.instances | length }} > 0"
- "'{{ presented_instance_fact.instances.0.state.name }}' in ['running','pending']"
- "{{ presented_instance_fact.instances.0.cpu_options.core_count }} == 1"
- "{{ presented_instance_fact.instances.0.cpu_options.threads_per_core }} == 1"
- name: create t3.nano instance with cpu_options(check mode)
ec2_instance:
name: "{{ resource_prefix }}-test-t3nano-1-threads-per-core-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
@ -63,31 +62,18 @@
cpu_options:
core_count: 1
threads_per_core: 1
<<: *aws_connection_info
check_mode: yes
- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-t3nano-1-threads-per-core"
<<: *aws_connection_info
register: presented_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-t3nano-1-threads-per-core-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: "Confirm existence of instance id."
assert:
that:
- "{{ presented_instance_fact.instances | length }} > 0"
- "'{{ presented_instance_fact.instances.0.state.name }}' in ['running','pending']"
- "{{ checkmode_instance_fact.instances | length }} == 0"
- "{{ presented_instance_fact.instances.0.cpu_options.core_count }} == 1"
- "{{ presented_instance_fact.instances.0.cpu_options.threads_per_core }} == 1"
always:
- name: Terminate instances
@ -96,6 +82,5 @@
tag:TestId: "{{ resource_prefix }}"
state: absent
wait: false
<<: *aws_connection_info
register: result
ignore_errors: yes

@ -1,47 +1,35 @@
- block:
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- name: Make instance in a default subnet of the VPC
ec2_instance:
name: "{{ resource_prefix }}-test-default-vpc"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
wait: false
<<: *aws_connection_info
register: in_default_vpc
- name: Make instance in a default subnet of the VPC(check mode)
ec2_instance:
name: "{{ resource_prefix }}-test-default-vpc-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
check_mode: yes
- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-default-vpc"
<<: *aws_connection_info
register: presented_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-default-vpc-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally."
@ -50,17 +38,16 @@
- "{{ presented_instance_fact.instances | length }} > 0"
- "{{ checkmode_instance_fact.instances | length }} == 0"
- name: Terminate instance
ec2:
- name: Terminate instances
ec2_instance:
instance_ids: "{{ in_default_vpc.instance_ids }}"
state: absent
<<: *aws_connection_info
always:
- name: Terminate test-default-vpc instance
ec2:
ec2_instance:
filters:
"tag:Name": "{{ resource_prefix }}-test-default-vpc"
state: absent
<<: *aws_connection_info
wait: false
ignore_errors: yes

@ -1,34 +1,38 @@
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- block:
- name: Make EBS optimized instance in the testing subnet of the test VPC
ec2_instance:
name: "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
ebs_optimized: true
instance_type: t3.nano
wait: false
register: ebs_opt_in_vpc
- name: Make EBS optimized instance in the testing subnet of the test VPC
ec2_instance:
name: "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc"
image_id: "{{ ec2_ebs_optimized_ami_image[aws_region] }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
ebs_optimized: true
instance_type: t3.nano
wait: false
<<: *aws_connection_info
register: ebs_opt_in_vpc
- name: Get ec2 instance info
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc"
register: ebs_opt_instance_info
- name: Get ec2 instance info
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc"
<<: *aws_connection_info
register: ebs_opt_instance_info
- name: Assert instance is ebs_optimized
assert:
that:
- "{{ ebs_opt_instance_info.instances.0.ebs_optimized }}"
- name: Assert instance is ebs_optimized
assert:
that:
- "{{ ebs_opt_instance_info.instances.0.ebs_optimized }}"
- name: Terminate instances
ec2_instance:
instance_ids: "{{ ebs_opt_in_vpc.instance_ids }}"
state: absent
always:
- name: Terminate test-ebs-vols instances
ec2_instance:
filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc"
state: absent
wait: false
ignore_errors: yes

@ -1,20 +1,9 @@
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- name: remove any instances in the test VPC
- name: remove Instances
ec2_instance:
filters:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
<<: *aws_connection_info
register: removed
until: removed is not failed
filters:
vpc-id: "{{ testing_vpc.vpc.id }}"
wait: yes
ignore_errors: yes
retries: 10
@ -22,14 +11,12 @@
ec2_eni_info:
filters:
vpc-id: "{{ testing_vpc.vpc.id }}"
<<: *aws_connection_info
register: enis
- name: delete all ENIs
ec2_eni:
eni_id: "{{ item.id }}"
state: absent
<<: *aws_connection_info
until: removed is not failed
with_items: "{{ enis.network_interfaces }}"
ignore_errors: yes
@ -41,7 +28,6 @@
description: a security group for ansible tests
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
<<: *aws_connection_info
register: removed
until: removed is not failed
ignore_errors: yes
@ -59,7 +45,6 @@
subnets:
- "{{ testing_subnet_a.subnet.id }}"
- "{{ testing_subnet_b.subnet.id }}"
<<: *aws_connection_info
register: removed
until: removed is not failed
ignore_errors: yes
@ -69,7 +54,6 @@
ec2_vpc_igw:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
<<: *aws_connection_info
register: removed
until: removed is not failed
ignore_errors: yes
@ -79,8 +63,7 @@
ec2_vpc_subnet:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.32.0/24
<<: *aws_connection_info
cidr: "{{ subnet_a_cidr }}"
register: removed
until: removed is not failed
ignore_errors: yes
@ -90,8 +73,7 @@
ec2_vpc_subnet:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.33.0/24
<<: *aws_connection_info
cidr: "{{ subnet_b_cidr }}"
register: removed
until: removed is not failed
ignore_errors: yes
@ -99,13 +81,12 @@
- name: remove the VPC
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
cidr_block: 10.22.32.0/23
name: "{{ vpc_name }}"
cidr_block: "{{ vpc_cidr }}"
state: absent
tags:
Name: Ansible Testing VPC
tenancy: default
<<: *aws_connection_info
register: removed
until: removed is not failed
ignore_errors: yes

@ -1,49 +1,48 @@
- name: set connection information for all tasks
- name: fetch AZ availability
aws_az_info:
register: az_info
- name: Assert that we have multiple AZs available to us
assert:
that: az_info.availability_zones | length >= 2
- name: pick AZs
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
subnet_a_az: '{{ az_info.availability_zones[0].zone_name }}'
subnet_b_az: '{{ az_info.availability_zones[1].zone_name }}'
- name: Create VPC for use in testing
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
cidr_block: 10.22.32.0/23
name: "{{ vpc_name }}"
cidr_block: "{{ vpc_cidr }}"
tags:
Name: Ansible ec2_instance Testing VPC
tenancy: default
<<: *aws_connection_info
register: testing_vpc
- name: Create internet gateway for use in testing
ec2_vpc_igw:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: present
<<: *aws_connection_info
register: igw
- name: Create default subnet in zone A
ec2_vpc_subnet:
state: present
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.32.0/24
az: "{{ aws_region }}a"
cidr: "{{ subnet_a_cidr }}"
az: "{{ subnet_a_az }}"
resource_tags:
Name: "{{ resource_prefix }}-subnet-a"
<<: *aws_connection_info
register: testing_subnet_a
- name: Create secondary subnet in zone B
ec2_vpc_subnet:
state: present
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.33.0/24
az: "{{ aws_region }}b"
cidr: "{{ subnet_b_cidr }}"
az: "{{ subnet_b_az }}"
resource_tags:
Name: "{{ resource_prefix }}-subnet-b"
<<: *aws_connection_info
register: testing_subnet_b
- name: create routing rules
@ -57,7 +56,6 @@
subnets:
- "{{ testing_subnet_a.subnet.id }}"
- "{{ testing_subnet_b.subnet.id }}"
<<: *aws_connection_info
- name: create a security group with the vpc
ec2_group:
@ -73,5 +71,4 @@
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
<<: *aws_connection_info
register: sg

@ -1,38 +1,28 @@
- block:
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
# Make custom ENIs and attach via the `network` parameter
- ec2_eni:
delete_on_termination: true
subnet_id: "{{ testing_subnet_b.subnet.id }}"
security_groups:
- "{{ sg.group_id }}"
<<: *aws_connection_info
register: eni_a
- ec2_eni:
delete_on_termination: true
subnet_id: "{{ testing_subnet_b.subnet.id }}"
security_groups:
- "{{ sg.group_id }}"
<<: *aws_connection_info
register: eni_b
- ec2_eni:
delete_on_termination: true
subnet_id: "{{ testing_subnet_b.subnet.id }}"
security_groups:
- "{{ sg.group_id }}"
<<: *aws_connection_info
register: eni_c
- ec2_key:
name: "{{ resource_prefix }}_test_key"
<<: *aws_connection_info
- name: Make instance in the testing subnet created in the test VPC
ec2_instance:
@ -41,20 +31,18 @@
network:
interfaces:
- id: "{{ eni_a.interface.id }}"
image_id: "{{ ec2_ami_image[aws_region] }}"
availability_zone: '{{ aws_region }}b'
image_id: "{{ ec2_ami_image }}"
availability_zone: '{{ subnet_b_az }}'
tags:
TestId: "{{ resource_prefix }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
wait: false
<<: *aws_connection_info
register: in_test_vpc
- name: "Gather {{ resource_prefix }}-test-eni-vpc info"
ec2_instance_info:
filters:
"tag:Name": '{{ resource_prefix }}-test-eni-vpc'
<<: *aws_connection_info
register: in_test_vpc_instance
- assert:
@ -69,12 +57,11 @@
interfaces:
- id: "{{ eni_a.interface.id }}"
- id: "{{ eni_b.interface.id }}"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
wait: false
<<: *aws_connection_info
register: add_interface
until: add_interface is not failed
ignore_errors: yes
@ -87,26 +74,23 @@
network:
interfaces:
- id: "{{ eni_c.interface.id }}"
image_id: "{{ ec2_ami_image[aws_region] }}"
availability_zone: '{{ aws_region }}b'
image_id: "{{ ec2_ami_image }}"
availability_zone: '{{ subnet_b_az }}'
tags:
TestId: "{{ resource_prefix }}"
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
check_mode: yes
- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-eni-vpc"
<<: *aws_connection_info
register: presented_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-eni-vpc-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: "Confirm existence of instance id."
@ -115,27 +99,26 @@
- "{{ presented_instance_fact.instances | length }} > 0"
- "{{ checkmode_instance_fact.instances | length }} == 0"
- name: Terminate instance
ec2_instance:
filters:
tag:TestId: "{{ resource_prefix }}"
state: absent
wait: false
<<: *aws_connection_info
register: result
always:
- name: Terminate instance
ec2_instance:
instance_ids: "{{ in_test_vpc.instance_ids }}"
state: absent
wait: false
<<: *aws_connection_info
# We can't delete the ENIs until we've deleted all the instances
wait: yes
register: result
ignore_errors: yes
- ec2_key:
name: "{{ resource_prefix }}_test_key"
state: absent
<<: *aws_connection_info
ignore_errors: yes
- ec2_eni:
eni_id: '{{ item.interface.id }}'
state: absent
ignore_errors: yes
with_items:
- '{{ eni_a }}'
- '{{ eni_b }}'
- '{{ eni_c }}'

@ -1,12 +1,3 @@
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- block:
- name: Create IAM role for test
iam_role:
@ -16,7 +7,6 @@
create_instance_profile: yes
managed_policy:
- AmazonEC2ContainerServiceRole
<<: *aws_connection_info
register: iam_role
- name: Create second IAM role for test
@ -27,17 +17,19 @@
create_instance_profile: yes
managed_policy:
- AmazonEC2ContainerServiceRole
<<: *aws_connection_info
register: iam_role_2
- name: wait 10 seconds for roles to become available
pause:
seconds: 10
- name: Make instance with an instance_role
ec2_instance:
name: "{{ resource_prefix }}-test-instance-role"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
instance_role: "ansible-test-sts-{{ resource_prefix }}-test-policy"
<<: *aws_connection_info
register: instance_with_role
- assert:
@ -47,25 +39,22 @@
- name: Make instance with an instance_role(check mode)
ec2_instance:
name: "{{ resource_prefix }}-test-instance-role-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
instance_role: "{{ iam_role.arn.replace(':role/', ':instance-profile/') }}"
<<: *aws_connection_info
check_mode: yes
- name: "fact presented ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-instance-role"
<<: *aws_connection_info
register: presented_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-instance-role-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally."
@ -77,26 +66,34 @@
- name: Update instance with new instance_role
ec2_instance:
name: "{{ resource_prefix }}-test-instance-role"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
instance_role: "{{ iam_role_2.arn.replace(':role/', ':instance-profile/') }}"
<<: *aws_connection_info
register: instance_with_updated_role
until: instance_with_updated_role is not failed
retries: 10
# XXX We shouldn't need this
- name: wait 10 seconds for role update to complete
pause:
seconds: 10
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-instance-role"
register: updates_instance_info
- assert:
that:
- 'instance_with_updated_role.instances[0].iam_instance_profile.arn == iam_role_2.arn.replace(":role/", ":instance-profile/")'
- 'instance_with_updated_role.instances[0].instance_id == instance_with_role.instances[0].instance_id'
- 'updates_instance_info.instances[0].iam_instance_profile.arn == iam_role_2.arn.replace(":role/", ":instance-profile/")'
- 'updates_instance_info.instances[0].instance_id == instance_with_role.instances[0].instance_id'
always:
- name: Terminate instance
ec2:
instance_ids: "{{ instance_with_role.instance_ids }}"
state: absent
<<: *aws_connection_info
wait: no
register: removed
until: removed is not failed
ignore_errors: yes
@ -110,7 +107,6 @@
create_instance_profile: yes
managed_policy:
- AmazonEC2ContainerServiceRole
<<: *aws_connection_info
loop:
- "ansible-test-sts-{{ resource_prefix }}-test-policy"
- "ansible-test-sts-{{ resource_prefix }}-test-policy-2"

@ -1,62 +1,64 @@
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- block:
- name: New instance and don't wait for it to complete
ec2_instance:
name: "{{ resource_prefix }}-test-no-wait"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
tags:
TestId: "{{ resource_prefix }}"
wait: false
instance_type: "{{ ec2_instance_type }}"
register: in_test_vpc
- name: New instance and don't wait for it to complete
ec2_instance:
name: "{{ resource_prefix }}-test-no-wait"
image_id: "{{ ec2_ami_image[aws_region] }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
tags:
TestId: "{{ resource_prefix }}"
wait: false
instance_type: t2.micro
<<: *aws_connection_info
register: in_test_vpc
- assert:
that:
- in_test_vpc is not failed
- in_test_vpc is changed
- in_test_vpc.instances is not defined
- in_test_vpc.instance_ids is defined
- in_test_vpc.instance_ids | length > 0
- assert:
that:
- in_test_vpc is not failed
- in_test_vpc is changed
- in_test_vpc.instances is not defined
- in_test_vpc.instance_ids is defined
- in_test_vpc.instance_ids | length > 0
- name: New instance and don't wait for it to complete ( check mode )
ec2_instance:
name: "{{ resource_prefix }}-test-no-wait-checkmode"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
tags:
TestId: "{{ resource_prefix }}"
wait: false
instance_type: "{{ ec2_instance_type }}"
check_mode: yes
- name: New instance and don't wait for it to complete ( check mode )
ec2_instance:
name: "{{ resource_prefix }}-test-no-wait-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
tags:
TestId: "{{ resource_prefix }}"
wait: false
instance_type: t2.micro
<<: *aws_connection_info
check_mode: yes
- name: Facts for ec2 test instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-no-wait"
register: real_instance_fact
until: real_instance_fact.instances | length > 0
retries: 10
- name: Facts for ec2 test instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-no-wait"
<<: *aws_connection_info
register: real_instance_fact
until: real_instance_fact.instances | length > 0
retries: 10
- name: Facts for checkmode ec2 test instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-no-wait-checkmode"
register: checkmode_instance_fact
- name: Facts for checkmode ec2 test instance
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-no-wait-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally."
assert:
that:
- "{{ real_instance_fact.instances | length }} > 0"
- "{{ checkmode_instance_fact.instances | length }} == 0"
- name: "Confirm whether the check mode is working normally."
assert:
that:
- "{{ real_instance_fact.instances | length }} > 0"
- "{{ checkmode_instance_fact.instances | length }} == 0"
- name: Terminate instances
ec2_instance:
instance_ids: "{{ in_test_vpc.instance_ids }}"
state: absent
always:
- name: Terminate test-ebs-vols instances
ec2_instance:
filters:
"tag:Name": "{{ resource_prefix }}-test-no-wait-checkmode"
state: absent
wait: no
ignore_errors: yes

@ -1,4 +1,11 @@
---
# This test suite is currently marked "unsupported" because it takes too long to
# run within the 'shippable' test environment.
# If a test suite runs longer than around 15 minutes aws-terminator can start
# pulling resources created at the start of the test out from under it.
#
# ###############################################################################
#
# A Note about ec2 environment variable name preference:
# - EC2_URL -> AWS_URL
# - EC2_ACCESS_KEY -> AWS_ACCESS_KEY_ID -> AWS_ACCESS_KEY
@ -6,46 +13,31 @@
# - EC2_REGION -> AWS_REGION
#
- block:
- set_fact:
virtualenv: "{{ remote_tmp_dir }}/virtualenv"
virtualenv_command: "{{ ansible_python_interpreter }} -m virtualenv"
- set_fact:
virtualenv_interpreter: "{{ virtualenv }}/bin/python"
- pip:
name: virtualenv
- pip:
name:
- 'botocore<1.10.16'
- boto3
- coverage
virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}"
virtualenv_site_packages: no
- include_tasks: version_fail.yml
- name: Wrap up all tests and setup AWS credentials
module_defaults:
group/aws:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token | default(omit) }}"
region: "{{ aws_region }}"
block:
- name: Find AMI to use
ec2_ami_info:
owners: 'amazon'
filters:
name: '{{ ec2_ami_name }}'
register: ec2_amis
- name: Set fact with latest AMI
vars:
ansible_python_interpreter: "{{ virtualenv_interpreter }}"
- file:
path: "{{ virtualenv }}"
state: absent
- pip:
name:
- 'botocore>=1.10.16'
- boto3
- coverage
virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}"
virtualenv_site_packages: no
latest_ami: '{{ ec2_amis.images | sort(attribute="creation_date") | last }}'
set_fact:
ec2_ami_image: '{{ latest_ami.image_id }}'
- include_tasks: version_fail_wrapper.yml
- include_tasks: env_setup.yml
- include_tasks: cpu_options.yml
- include_tasks: termination_protection.yml
- include_tasks: tags_and_vpc_settings.yml

@ -1,16 +1,8 @@
- block:
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- name: Make instance in the testing subnet created in the test VPC
ec2_instance:
name: "{{ resource_prefix }}-test-basic-vpc-create"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
user_data: |
#cloud-config
package_upgrade: true
@ -22,15 +14,14 @@
network:
source_dest_check: false
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
wait: false
<<: *aws_connection_info
register: in_test_vpc
- name: Make instance in the testing subnet created in the test VPC(check mode)
ec2_instance:
name: "{{ resource_prefix }}-test-basic-vpc-create-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
user_data: |
#cloud-config
package_upgrade: true
@ -42,14 +33,13 @@
network:
source_dest_check: false
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
check_mode: yes
- name: Try to re-make the instance, hopefully this shows changed=False
ec2_instance:
name: "{{ resource_prefix }}-test-basic-vpc-create"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
user_data: |
#cloud-config
package_upgrade: true
@ -59,8 +49,7 @@
Something: else
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
register: remake_in_test_vpc
- name: "Remaking the same instance resulted in no changes"
assert:
@ -76,14 +65,12 @@
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-basic-vpc-create"
<<: *aws_connection_info
register: presented_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-basic-vpc-create-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally."
@ -95,19 +82,17 @@
- name: Alter it by adding tags
ec2_instance:
name: "{{ resource_prefix }}-test-basic-vpc-create"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
Another: thing
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
register: add_another_tag
- ec2_instance_info:
instance_ids: "{{ add_another_tag.instance_ids }}"
<<: *aws_connection_info
register: check_tags
- name: "Remaking the same instance resulted in no changes"
assert:
@ -118,19 +103,17 @@
- name: Purge a tag
ec2_instance:
name: "{{ resource_prefix }}-test-basic-vpc-create"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
purge_tags: true
tags:
TestId: "{{ resource_prefix }}"
Another: thing
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
- ec2_instance_info:
instance_ids: "{{ add_another_tag.instance_ids }}"
<<: *aws_connection_info
register: check_tags
- name: "Remaking the same instance resulted in no changes"
@ -142,7 +125,7 @@
assert:
that:
- check_tags.instances[0].public_dns_name == ""
- check_tags.instances[0].private_ip_address.startswith("10.22.33")
- check_tags.instances[0].private_ip_address.startswith(subnet_b_startswith)
- check_tags.instances[0].subnet_id == testing_subnet_b.subnet.id
- name: check that tags were applied
assert:
@ -156,7 +139,6 @@
tag:TestId: "{{ resource_prefix }}"
state: absent
wait: false
<<: *aws_connection_info
register: result
- assert:
that: result.changed
@ -168,6 +150,5 @@
tag:TestId: "{{ resource_prefix }}"
state: absent
wait: false
<<: *aws_connection_info
register: result
ignore_errors: yes

@ -1,39 +1,28 @@
- block:
- name: set connection information for all tasks
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- name: Make termination-protected instance in the testing subnet created in the test VPC
ec2_instance:
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: true
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
state: running
wait: yes
<<: *aws_connection_info
register: in_test_vpc
- name: Make termination-protected instance in the testing subnet created in the test VPC(check mode)
ec2_instance:
name: "{{ resource_prefix }}-test-protected-instance-in-vpc-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: true
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
check_mode: yes
- name: "fact presented ec2 instance"
@ -41,14 +30,12 @@
filters:
"tag:Name": "{{ resource_prefix }}-test-protected-instance-in-vpc"
"instance-state-name": "running"
<<: *aws_connection_info
register: presented_instance_fact
- name: "fact checkmode ec2 instance"
ec2_instance_info:
filters:
"tag:Name": "{{ resource_prefix }}-test-protected-instance-in-vpc-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally."
@ -62,14 +49,13 @@
ec2_instance:
state: absent
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
tags:
TestId: "{{ resource_prefix }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: true
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
register: bad_terminate
ignore_errors: yes
- name: Cannot terminate protected instance
@ -79,20 +65,18 @@
- name: Alter termination protection setting
ec2_instance:
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: false
instance_type: t2.micro
<<: *aws_connection_info
instance_type: "{{ ec2_instance_type }}"
- name: Try to terminate the instance again (should work)
ec2_instance:
name: "{{ resource_prefix }}-test-protected-instance-in-vpc"
image_id: "{{ ec2_ami_image[aws_region] }}"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
instance_type: t2.micro
instance_type: "{{ ec2_instance_type }}"
state: absent
wait: false
<<: *aws_connection_info
register: terminate_results
- assert:
that: terminate_results is not failed
@ -104,6 +88,5 @@
tag:TestId: "{{ resource_prefix }}"
state: absent
wait: false
<<: *aws_connection_info
register: result
ignore_errors: yes

@ -1,23 +1,13 @@
- block:
- name: set up aws connection info
set_fact:
aws_connection_info: &aws_connection_info
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: True
- name: create t3.nano with cpu options (fails gracefully)
ec2_instance:
state: present
name: "ansible-test-{{ resource_prefix | regex_search('([0-9]+)$') }}-ec2"
image_id: "{{ ec2_ami_image[aws_region] }}"
instance_type: t3.nano
image_id: "{{ ec2_ami_image }}"
instance_type: "t3.nano"
cpu_options:
core_count: 1
threads_per_core: 1
<<: *aws_connection_info
register: ec2_instance_cpu_options_creation
ignore_errors: yes
@ -32,5 +22,5 @@
ec2_instance:
state: absent
name: "ansible-test-{{ resource_prefix | regex_search('([0-9]+)$') }}-ec2"
<<: *aws_connection_info
wait: no
ignore_errors: yes

@ -0,0 +1,27 @@
---
- set_fact:
virtualenv: "{{ remote_tmp_dir }}/virtualenv"
virtualenv_command: "{{ ansible_python_interpreter }} -m virtualenv"
- set_fact:
virtualenv_interpreter: "{{ virtualenv }}/bin/python"
- pip:
name: virtualenv
- pip:
name:
- 'botocore<1.10.16'
- boto3
- coverage
virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}"
virtualenv_site_packages: no
- include_tasks: version_fail.yml
vars:
ansible_python_interpreter: "{{ virtualenv_interpreter }}"
- file:
path: "{{ virtualenv }}"
state: absent

@ -753,12 +753,6 @@ lib/ansible/modules/cloud/amazon/ec2_group.py validate-modules:undocumented-para
lib/ansible/modules/cloud/amazon/ec2_group.py validate-modules:parameter-type-not-in-doc
lib/ansible/modules/cloud/amazon/ec2_group.py validate-modules:doc-missing-type
lib/ansible/modules/cloud/amazon/ec2_group_info.py validate-modules:parameter-type-not-in-doc
lib/ansible/modules/cloud/amazon/ec2_instance.py validate-modules:doc-default-does-not-match-spec
lib/ansible/modules/cloud/amazon/ec2_instance.py validate-modules:parameter-type-not-in-doc
lib/ansible/modules/cloud/amazon/ec2_instance.py validate-modules:doc-missing-type
lib/ansible/modules/cloud/amazon/ec2_instance.py validate-modules:return-syntax-error
lib/ansible/modules/cloud/amazon/ec2_instance_info.py validate-modules:parameter-type-not-in-doc
lib/ansible/modules/cloud/amazon/ec2_instance_info.py validate-modules:return-syntax-error
lib/ansible/modules/cloud/amazon/ec2_key.py validate-modules:parameter-type-not-in-doc
lib/ansible/modules/cloud/amazon/ec2_key.py validate-modules:doc-missing-type
lib/ansible/modules/cloud/amazon/ec2_launch_template.py validate-modules:nonexistent-parameter-documented

Loading…
Cancel
Save