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

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

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

@ -2,22 +2,13 @@
# defaults file for ec2_instance # defaults file for ec2_instance
ec2_instance_name: '{{ resource_prefix }}-node' ec2_instance_name: '{{ resource_prefix }}-node'
ec2_instance_owner: 'integration-run-{{ resource_prefix }}' ec2_instance_owner: 'integration-run-{{ resource_prefix }}'
ec2_ami_image: ec2_instance_type: 't3.micro'
# Amazon Linux - collected 2019-05-10 ec2_ami_name: 'amzn2-ami-hvm-2.*-x86_64-gp2'
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
# We need to use ENA enabled AMIs to get EBS optimized instances. vpc_name: '{{ resource_prefix }}-vpc'
ec2_ebs_optimized_ami_image: "{{ ec2_ami_image }}" 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 - block:
set_fact: - name: New instance with an extra block device
aws_connection_info: &aws_connection_info ec2_instance:
aws_access_key: "{{ aws_access_key }}" name: "{{ resource_prefix }}-test-ebs-vols"
aws_secret_key: "{{ aws_secret_key }}" image_id: "{{ ec2_ami_image }}"
security_token: "{{ security_token }}" vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
region: "{{ aws_region }}" volumes:
no_log: true - 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 - name: Gather instance info
ec2_instance: ec2_instance_info:
name: "{{ resource_prefix }}-test-ebs-vols" filters:
image_id: "{{ ec2_ami_image[aws_region] }}" "tag:Name": "{{ resource_prefix }}-test-ebs-vols"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" register: in_test_vpc_instance
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 - assert:
ec2_instance_info: that:
filters: - in_test_vpc is not failed
"tag:Name": "{{ resource_prefix }}-test-ebs-vols" - in_test_vpc is changed
<<: *aws_connection_info - in_test_vpc_instance.instances[0].block_device_mappings[0]
register: in_test_vpc_instance - 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: - name: New instance with an extra block device(check mode)
that: ec2_instance:
- in_test_vpc is not failed name: "{{ resource_prefix }}-test-ebs-vols-checkmode"
- in_test_vpc is changed image_id: "{{ ec2_ami_image }}"
- in_test_vpc_instance.instances[0].block_device_mappings[0] vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
- in_test_vpc_instance.instances[0].block_device_mappings[1] volumes:
- in_test_vpc_instance.instances[0].block_device_mappings[1].device_name == '/dev/sdb' - 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) - name: "fact presented ec2 instance"
ec2_instance: ec2_instance_info:
name: "{{ resource_prefix }}-test-ebs-vols-checkmode" filters:
image_id: "{{ ec2_ami_image[aws_region] }}" "tag:Name": "{{ resource_prefix }}-test-ebs-vols"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" "instance-state-name": "running"
volumes: register: presented_instance_fact
- 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" - name: "fact checkmode ec2 instance"
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols" "tag:Name": "{{ resource_prefix }}-test-ebs-vols-checkmode"
"instance-state-name": "running" register: checkmode_instance_fact
<<: *aws_connection_info
register: presented_instance_fact
- name: "fact checkmode ec2 instance" - name: "Confirm whether the check mode is working normally."
ec2_instance_info: assert:
filters: that:
"tag:Name": "{{ resource_prefix }}-test-ebs-vols-checkmode" - "{{ presented_instance_fact.instances | length }} > 0"
<<: *aws_connection_info - "{{ checkmode_instance_fact.instances | length }} == 0"
register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally." - name: Terminate instances
assert: ec2_instance:
that: instance_ids: "{{ in_test_vpc.instance_ids }}"
- "{{ presented_instance_fact.instances | length }} > 0" state: absent
- "{{ checkmode_instance_fact.instances | length }} == 0"
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: - block:
- name: Make basic instance - name: Make basic instance
ec2_instance: ec2_instance:
name: "{{ resource_prefix }}-checkmode-comparison" name: "{{ resource_prefix }}-checkmode-comparison"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}" security_groups: "{{ sg.group_id }}"
instance_type: t2.micro instance_type: "{{ ec2_instance_type }}"
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
wait: false wait: false
<<: *aws_connection_info
register: basic_instance register: basic_instance
- name: Make basic instance(check mode) - name: Make basic instance(check mode)
ec2_instance: ec2_instance:
name: "{{ resource_prefix }}-checkmode-comparison-checkmode" name: "{{ resource_prefix }}-checkmode-comparison-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}" security_groups: "{{ sg.group_id }}"
instance_type: t2.micro instance_type: "{{ ec2_instance_type }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
<<: *aws_connection_info
check_mode: yes check_mode: yes
- name: fact presented ec2 instance - name: fact presented ec2 instance
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison" "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: presented_instance_fact register: presented_instance_fact
- name: fact checkmode ec2 instance - name: fact checkmode ec2 instance
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison-checkmode" "tag:Name": "{{ resource_prefix }}-checkmode-comparison-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact register: checkmode_instance_fact
- name: Confirm whether the check mode is working normally. - name: Confirm whether the check mode is working normally.
@ -54,14 +41,12 @@
name: "{{ resource_prefix }}-checkmode-comparison" name: "{{ resource_prefix }}-checkmode-comparison"
state: stopped state: stopped
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
check_mode: yes check_mode: yes
- name: fact ec2 instance - name: fact ec2 instance
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison" "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_checkmode_stopinstance_fact register: confirm_checkmode_stopinstance_fact
- name: Verify that it was not stopped. - name: Verify that it was not stopped.
@ -74,17 +59,14 @@
name: "{{ resource_prefix }}-checkmode-comparison" name: "{{ resource_prefix }}-checkmode-comparison"
state: stopped state: stopped
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
register: instance_stop register: instance_stop
until: not instance_stop.failed until: not instance_stop.failed
retries: 10 retries: 10
- name: fact stopped ec2 instance - name: fact stopped ec2 instance
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison" "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_stopinstance_fact register: confirm_stopinstance_fact
- name: Verify that it was stopped. - name: Verify that it was stopped.
@ -97,16 +79,14 @@
name: "{{ resource_prefix }}-checkmode-comparison" name: "{{ resource_prefix }}-checkmode-comparison"
state: running state: running
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
check_mode: yes check_mode: yes
- name: fact ec2 instance - name: fact ec2 instance
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison" "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_checkmode_runninginstance_fact register: confirm_checkmode_runninginstance_fact
- name: Verify that it was not running. - name: Verify that it was not running.
assert: assert:
that: that:
@ -117,13 +97,11 @@
name: "{{ resource_prefix }}-checkmode-comparison" name: "{{ resource_prefix }}-checkmode-comparison"
state: running state: running
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
- name: fact ec2 instance. - name: fact ec2 instance.
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison" "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_runninginstance_fact register: confirm_runninginstance_fact
- name: Verify that it was running. - name: Verify that it was running.
@ -136,16 +114,14 @@
name: "{{ resource_prefix }}-checkmode-comparison" name: "{{ resource_prefix }}-checkmode-comparison"
state: absent state: absent
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
check_mode: yes check_mode: yes
- name: fact ec2 instance - name: fact ec2 instance
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison" "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_checkmode_terminatedinstance_fact register: confirm_checkmode_terminatedinstance_fact
- name: Verify that it was not terminated, - name: Verify that it was not terminated,
assert: assert:
that: that:
@ -156,26 +132,24 @@
name: "{{ resource_prefix }}-checkmode-comparison" name: "{{ resource_prefix }}-checkmode-comparison"
state: absent state: absent
vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}"
<<: *aws_connection_info
- name: fact ec2 instance - name: fact ec2 instance
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-checkmode-comparison" "tag:Name": "{{ resource_prefix }}-checkmode-comparison"
<<: *aws_connection_info
register: confirm_terminatedinstance_fact register: confirm_terminatedinstance_fact
- name: Verify that it was terminated, - name: Verify that it was terminated,
assert: assert:
that: that:
- '"{{ confirm_terminatedinstance_fact.instances[0].state.name }}" == "terminated"' - '"{{ confirm_terminatedinstance_fact.instances[0].state.name }}" == "terminated"'
always: always:
- name: Terminate instance - name: Terminate instance
ec2: ec2:
instance_ids: "{{ basic_instance.instance_ids }}" instance_ids: "{{ basic_instance.instance_ids }}"
state: absent state: absent
<<: *aws_connection_info wait: false
register: removed register: removed
until: removed is not failed until: removed is not failed
ignore_errors: yes ignore_errors: yes

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

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

@ -1,34 +1,38 @@
- name: set connection information for all tasks - block:
set_fact: - name: Make EBS optimized instance in the testing subnet of the test VPC
aws_connection_info: &aws_connection_info ec2_instance:
aws_access_key: "{{ aws_access_key }}" name: "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc"
aws_secret_key: "{{ aws_secret_key }}" image_id: "{{ ec2_ami_image }}"
security_token: "{{ security_token }}" tags:
region: "{{ aws_region }}" TestId: "{{ resource_prefix }}"
no_log: true 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 - name: Get ec2 instance info
ec2_instance: ec2_instance_info:
name: "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc" filters:
image_id: "{{ ec2_ebs_optimized_ami_image[aws_region] }}" "tag:Name": "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc"
tags: register: ebs_opt_instance_info
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 - name: Assert instance is ebs_optimized
ec2_instance_info: assert:
filters: that:
"tag:Name": "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc" - "{{ ebs_opt_instance_info.instances.0.ebs_optimized }}"
<<: *aws_connection_info
register: ebs_opt_instance_info
- name: Assert instance is ebs_optimized - name: Terminate instances
assert: ec2_instance:
that: instance_ids: "{{ ebs_opt_in_vpc.instance_ids }}"
- "{{ ebs_opt_instance_info.instances.0.ebs_optimized }}" 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 - name: remove Instances
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
ec2_instance: ec2_instance:
filters:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent state: absent
<<: *aws_connection_info filters:
register: removed vpc-id: "{{ testing_vpc.vpc.id }}"
until: removed is not failed wait: yes
ignore_errors: yes ignore_errors: yes
retries: 10 retries: 10
@ -22,14 +11,12 @@
ec2_eni_info: ec2_eni_info:
filters: filters:
vpc-id: "{{ testing_vpc.vpc.id }}" vpc-id: "{{ testing_vpc.vpc.id }}"
<<: *aws_connection_info
register: enis register: enis
- name: delete all ENIs - name: delete all ENIs
ec2_eni: ec2_eni:
eni_id: "{{ item.id }}" eni_id: "{{ item.id }}"
state: absent state: absent
<<: *aws_connection_info
until: removed is not failed until: removed is not failed
with_items: "{{ enis.network_interfaces }}" with_items: "{{ enis.network_interfaces }}"
ignore_errors: yes ignore_errors: yes
@ -41,7 +28,6 @@
description: a security group for ansible tests description: a security group for ansible tests
vpc_id: "{{ testing_vpc.vpc.id }}" vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent state: absent
<<: *aws_connection_info
register: removed register: removed
until: removed is not failed until: removed is not failed
ignore_errors: yes ignore_errors: yes
@ -59,7 +45,6 @@
subnets: subnets:
- "{{ testing_subnet_a.subnet.id }}" - "{{ testing_subnet_a.subnet.id }}"
- "{{ testing_subnet_b.subnet.id }}" - "{{ testing_subnet_b.subnet.id }}"
<<: *aws_connection_info
register: removed register: removed
until: removed is not failed until: removed is not failed
ignore_errors: yes ignore_errors: yes
@ -69,7 +54,6 @@
ec2_vpc_igw: ec2_vpc_igw:
vpc_id: "{{ testing_vpc.vpc.id }}" vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent state: absent
<<: *aws_connection_info
register: removed register: removed
until: removed is not failed until: removed is not failed
ignore_errors: yes ignore_errors: yes
@ -79,8 +63,7 @@
ec2_vpc_subnet: ec2_vpc_subnet:
state: absent state: absent
vpc_id: "{{ testing_vpc.vpc.id }}" vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.32.0/24 cidr: "{{ subnet_a_cidr }}"
<<: *aws_connection_info
register: removed register: removed
until: removed is not failed until: removed is not failed
ignore_errors: yes ignore_errors: yes
@ -90,8 +73,7 @@
ec2_vpc_subnet: ec2_vpc_subnet:
state: absent state: absent
vpc_id: "{{ testing_vpc.vpc.id }}" vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.33.0/24 cidr: "{{ subnet_b_cidr }}"
<<: *aws_connection_info
register: removed register: removed
until: removed is not failed until: removed is not failed
ignore_errors: yes ignore_errors: yes
@ -99,13 +81,12 @@
- name: remove the VPC - name: remove the VPC
ec2_vpc_net: ec2_vpc_net:
name: "{{ resource_prefix }}-vpc" name: "{{ vpc_name }}"
cidr_block: 10.22.32.0/23 cidr_block: "{{ vpc_cidr }}"
state: absent state: absent
tags: tags:
Name: Ansible Testing VPC Name: Ansible Testing VPC
tenancy: default tenancy: default
<<: *aws_connection_info
register: removed register: removed
until: removed is not failed until: removed is not failed
ignore_errors: yes 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: set_fact:
aws_connection_info: &aws_connection_info subnet_a_az: '{{ az_info.availability_zones[0].zone_name }}'
aws_access_key: "{{ aws_access_key }}" subnet_b_az: '{{ az_info.availability_zones[1].zone_name }}'
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
no_log: true
- name: Create VPC for use in testing - name: Create VPC for use in testing
ec2_vpc_net: ec2_vpc_net:
name: "{{ resource_prefix }}-vpc" name: "{{ vpc_name }}"
cidr_block: 10.22.32.0/23 cidr_block: "{{ vpc_cidr }}"
tags: tags:
Name: Ansible ec2_instance Testing VPC Name: Ansible ec2_instance Testing VPC
tenancy: default tenancy: default
<<: *aws_connection_info
register: testing_vpc register: testing_vpc
- name: Create internet gateway for use in testing - name: Create internet gateway for use in testing
ec2_vpc_igw: ec2_vpc_igw:
vpc_id: "{{ testing_vpc.vpc.id }}" vpc_id: "{{ testing_vpc.vpc.id }}"
state: present state: present
<<: *aws_connection_info
register: igw register: igw
- name: Create default subnet in zone A - name: Create default subnet in zone A
ec2_vpc_subnet: ec2_vpc_subnet:
state: present state: present
vpc_id: "{{ testing_vpc.vpc.id }}" vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.32.0/24 cidr: "{{ subnet_a_cidr }}"
az: "{{ aws_region }}a" az: "{{ subnet_a_az }}"
resource_tags: resource_tags:
Name: "{{ resource_prefix }}-subnet-a" Name: "{{ resource_prefix }}-subnet-a"
<<: *aws_connection_info
register: testing_subnet_a register: testing_subnet_a
- name: Create secondary subnet in zone B - name: Create secondary subnet in zone B
ec2_vpc_subnet: ec2_vpc_subnet:
state: present state: present
vpc_id: "{{ testing_vpc.vpc.id }}" vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.33.0/24 cidr: "{{ subnet_b_cidr }}"
az: "{{ aws_region }}b" az: "{{ subnet_b_az }}"
resource_tags: resource_tags:
Name: "{{ resource_prefix }}-subnet-b" Name: "{{ resource_prefix }}-subnet-b"
<<: *aws_connection_info
register: testing_subnet_b register: testing_subnet_b
- name: create routing rules - name: create routing rules
@ -57,7 +56,6 @@
subnets: subnets:
- "{{ testing_subnet_a.subnet.id }}" - "{{ testing_subnet_a.subnet.id }}"
- "{{ testing_subnet_b.subnet.id }}" - "{{ testing_subnet_b.subnet.id }}"
<<: *aws_connection_info
- name: create a security group with the vpc - name: create a security group with the vpc
ec2_group: ec2_group:
@ -73,5 +71,4 @@
from_port: 80 from_port: 80
to_port: 80 to_port: 80
cidr_ip: 0.0.0.0/0 cidr_ip: 0.0.0.0/0
<<: *aws_connection_info
register: sg register: sg

@ -1,38 +1,28 @@
- block: - 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 # Make custom ENIs and attach via the `network` parameter
- ec2_eni: - ec2_eni:
delete_on_termination: true delete_on_termination: true
subnet_id: "{{ testing_subnet_b.subnet.id }}" subnet_id: "{{ testing_subnet_b.subnet.id }}"
security_groups: security_groups:
- "{{ sg.group_id }}" - "{{ sg.group_id }}"
<<: *aws_connection_info
register: eni_a register: eni_a
- ec2_eni: - ec2_eni:
delete_on_termination: true delete_on_termination: true
subnet_id: "{{ testing_subnet_b.subnet.id }}" subnet_id: "{{ testing_subnet_b.subnet.id }}"
security_groups: security_groups:
- "{{ sg.group_id }}" - "{{ sg.group_id }}"
<<: *aws_connection_info
register: eni_b register: eni_b
- ec2_eni: - ec2_eni:
delete_on_termination: true delete_on_termination: true
subnet_id: "{{ testing_subnet_b.subnet.id }}" subnet_id: "{{ testing_subnet_b.subnet.id }}"
security_groups: security_groups:
- "{{ sg.group_id }}" - "{{ sg.group_id }}"
<<: *aws_connection_info
register: eni_c register: eni_c
- ec2_key: - ec2_key:
name: "{{ resource_prefix }}_test_key" name: "{{ resource_prefix }}_test_key"
<<: *aws_connection_info
- name: Make instance in the testing subnet created in the test VPC - name: Make instance in the testing subnet created in the test VPC
ec2_instance: ec2_instance:
@ -41,20 +31,18 @@
network: network:
interfaces: interfaces:
- id: "{{ eni_a.interface.id }}" - id: "{{ eni_a.interface.id }}"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
availability_zone: '{{ aws_region }}b' availability_zone: '{{ subnet_b_az }}'
tags: tags:
TestId: "{{ resource_prefix }}" TestId: "{{ resource_prefix }}"
instance_type: t2.micro instance_type: "{{ ec2_instance_type }}"
wait: false wait: false
<<: *aws_connection_info
register: in_test_vpc register: in_test_vpc
- name: "Gather {{ resource_prefix }}-test-eni-vpc info" - name: "Gather {{ resource_prefix }}-test-eni-vpc info"
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": '{{ resource_prefix }}-test-eni-vpc' "tag:Name": '{{ resource_prefix }}-test-eni-vpc'
<<: *aws_connection_info
register: in_test_vpc_instance register: in_test_vpc_instance
- assert: - assert:
@ -69,12 +57,11 @@
interfaces: interfaces:
- id: "{{ eni_a.interface.id }}" - id: "{{ eni_a.interface.id }}"
- id: "{{ eni_b.interface.id }}" - id: "{{ eni_b.interface.id }}"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
tags: tags:
TestId: "{{ resource_prefix }}" TestId: "{{ resource_prefix }}"
instance_type: t2.micro instance_type: "{{ ec2_instance_type }}"
wait: false wait: false
<<: *aws_connection_info
register: add_interface register: add_interface
until: add_interface is not failed until: add_interface is not failed
ignore_errors: yes ignore_errors: yes
@ -87,26 +74,23 @@
network: network:
interfaces: interfaces:
- id: "{{ eni_c.interface.id }}" - id: "{{ eni_c.interface.id }}"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
availability_zone: '{{ aws_region }}b' availability_zone: '{{ subnet_b_az }}'
tags: tags:
TestId: "{{ resource_prefix }}" TestId: "{{ resource_prefix }}"
instance_type: t2.micro instance_type: "{{ ec2_instance_type }}"
<<: *aws_connection_info
check_mode: yes check_mode: yes
- name: "fact presented ec2 instance" - name: "fact presented ec2 instance"
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-test-eni-vpc" "tag:Name": "{{ resource_prefix }}-test-eni-vpc"
<<: *aws_connection_info
register: presented_instance_fact register: presented_instance_fact
- name: "fact checkmode ec2 instance" - name: "fact checkmode ec2 instance"
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-test-eni-vpc-checkmode" "tag:Name": "{{ resource_prefix }}-test-eni-vpc-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact register: checkmode_instance_fact
- name: "Confirm existence of instance id." - name: "Confirm existence of instance id."
@ -115,27 +99,26 @@
- "{{ presented_instance_fact.instances | length }} > 0" - "{{ presented_instance_fact.instances | length }} > 0"
- "{{ checkmode_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: always:
- name: Terminate instance - name: Terminate instance
ec2_instance: ec2_instance:
instance_ids: "{{ in_test_vpc.instance_ids }}" instance_ids: "{{ in_test_vpc.instance_ids }}"
state: absent state: absent
wait: false # We can't delete the ENIs until we've deleted all the instances
<<: *aws_connection_info wait: yes
register: result register: result
ignore_errors: yes ignore_errors: yes
- ec2_key: - ec2_key:
name: "{{ resource_prefix }}_test_key" name: "{{ resource_prefix }}_test_key"
state: absent state: absent
<<: *aws_connection_info
ignore_errors: yes 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: - block:
- name: Create IAM role for test - name: Create IAM role for test
iam_role: iam_role:
@ -16,7 +7,6 @@
create_instance_profile: yes create_instance_profile: yes
managed_policy: managed_policy:
- AmazonEC2ContainerServiceRole - AmazonEC2ContainerServiceRole
<<: *aws_connection_info
register: iam_role register: iam_role
- name: Create second IAM role for test - name: Create second IAM role for test
@ -27,17 +17,19 @@
create_instance_profile: yes create_instance_profile: yes
managed_policy: managed_policy:
- AmazonEC2ContainerServiceRole - AmazonEC2ContainerServiceRole
<<: *aws_connection_info
register: iam_role_2 register: iam_role_2
- name: wait 10 seconds for roles to become available
pause:
seconds: 10
- name: Make instance with an instance_role - name: Make instance with an instance_role
ec2_instance: ec2_instance:
name: "{{ resource_prefix }}-test-instance-role" name: "{{ resource_prefix }}-test-instance-role"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}" security_groups: "{{ sg.group_id }}"
instance_type: t2.micro instance_type: "{{ ec2_instance_type }}"
instance_role: "ansible-test-sts-{{ resource_prefix }}-test-policy" instance_role: "ansible-test-sts-{{ resource_prefix }}-test-policy"
<<: *aws_connection_info
register: instance_with_role register: instance_with_role
- assert: - assert:
@ -47,25 +39,22 @@
- name: Make instance with an instance_role(check mode) - name: Make instance with an instance_role(check mode)
ec2_instance: ec2_instance:
name: "{{ resource_prefix }}-test-instance-role-checkmode" name: "{{ resource_prefix }}-test-instance-role-checkmode"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}" security_groups: "{{ sg.group_id }}"
instance_type: t2.micro instance_type: "{{ ec2_instance_type }}"
instance_role: "{{ iam_role.arn.replace(':role/', ':instance-profile/') }}" instance_role: "{{ iam_role.arn.replace(':role/', ':instance-profile/') }}"
<<: *aws_connection_info
check_mode: yes check_mode: yes
- name: "fact presented ec2 instance" - name: "fact presented ec2 instance"
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-test-instance-role" "tag:Name": "{{ resource_prefix }}-test-instance-role"
<<: *aws_connection_info
register: presented_instance_fact register: presented_instance_fact
- name: "fact checkmode ec2 instance" - name: "fact checkmode ec2 instance"
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-test-instance-role-checkmode" "tag:Name": "{{ resource_prefix }}-test-instance-role-checkmode"
<<: *aws_connection_info
register: checkmode_instance_fact register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally." - name: "Confirm whether the check mode is working normally."
@ -77,26 +66,34 @@
- name: Update instance with new instance_role - name: Update instance with new instance_role
ec2_instance: ec2_instance:
name: "{{ resource_prefix }}-test-instance-role" name: "{{ resource_prefix }}-test-instance-role"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
security_groups: "{{ sg.group_id }}" security_groups: "{{ sg.group_id }}"
instance_type: t2.micro instance_type: "{{ ec2_instance_type }}"
instance_role: "{{ iam_role_2.arn.replace(':role/', ':instance-profile/') }}" instance_role: "{{ iam_role_2.arn.replace(':role/', ':instance-profile/') }}"
<<: *aws_connection_info
register: instance_with_updated_role 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: - assert:
that: that:
- 'instance_with_updated_role.instances[0].iam_instance_profile.arn == iam_role_2.arn.replace(":role/", ":instance-profile/")' - 'updates_instance_info.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].instance_id == instance_with_role.instances[0].instance_id'
always: always:
- name: Terminate instance - name: Terminate instance
ec2: ec2:
instance_ids: "{{ instance_with_role.instance_ids }}" instance_ids: "{{ instance_with_role.instance_ids }}"
state: absent state: absent
<<: *aws_connection_info wait: no
register: removed register: removed
until: removed is not failed until: removed is not failed
ignore_errors: yes ignore_errors: yes
@ -110,7 +107,6 @@
create_instance_profile: yes create_instance_profile: yes
managed_policy: managed_policy:
- AmazonEC2ContainerServiceRole - AmazonEC2ContainerServiceRole
<<: *aws_connection_info
loop: loop:
- "ansible-test-sts-{{ resource_prefix }}-test-policy" - "ansible-test-sts-{{ resource_prefix }}-test-policy"
- "ansible-test-sts-{{ resource_prefix }}-test-policy-2" - "ansible-test-sts-{{ resource_prefix }}-test-policy-2"

@ -1,62 +1,64 @@
- name: set connection information for all tasks - block:
set_fact: - name: New instance and don't wait for it to complete
aws_connection_info: &aws_connection_info ec2_instance:
aws_access_key: "{{ aws_access_key }}" name: "{{ resource_prefix }}-test-no-wait"
aws_secret_key: "{{ aws_secret_key }}" image_id: "{{ ec2_ami_image }}"
security_token: "{{ security_token }}" vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
region: "{{ aws_region }}" tags:
no_log: true 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 - assert:
ec2_instance: that:
name: "{{ resource_prefix }}-test-no-wait" - in_test_vpc is not failed
image_id: "{{ ec2_ami_image[aws_region] }}" - in_test_vpc is changed
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" - in_test_vpc.instances is not defined
tags: - in_test_vpc.instance_ids is defined
TestId: "{{ resource_prefix }}" - in_test_vpc.instance_ids | length > 0
wait: false
instance_type: t2.micro
<<: *aws_connection_info
register: in_test_vpc
- assert: - name: New instance and don't wait for it to complete ( check mode )
that: ec2_instance:
- in_test_vpc is not failed name: "{{ resource_prefix }}-test-no-wait-checkmode"
- in_test_vpc is changed image_id: "{{ ec2_ami_image }}"
- in_test_vpc.instances is not defined vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
- in_test_vpc.instance_ids is defined tags:
- in_test_vpc.instance_ids | length > 0 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 ) - name: Facts for ec2 test instance
ec2_instance: ec2_instance_info:
name: "{{ resource_prefix }}-test-no-wait-checkmode" filters:
image_id: "{{ ec2_ami_image[aws_region] }}" "tag:Name": "{{ resource_prefix }}-test-no-wait"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" register: real_instance_fact
tags: until: real_instance_fact.instances | length > 0
TestId: "{{ resource_prefix }}" retries: 10
wait: false
instance_type: t2.micro
<<: *aws_connection_info
check_mode: yes
- name: Facts for ec2 test instance - name: Facts for checkmode ec2 test instance
ec2_instance_info: ec2_instance_info:
filters: filters:
"tag:Name": "{{ resource_prefix }}-test-no-wait" "tag:Name": "{{ resource_prefix }}-test-no-wait-checkmode"
<<: *aws_connection_info register: checkmode_instance_fact
register: real_instance_fact
until: real_instance_fact.instances | length > 0
retries: 10
- name: Facts for checkmode ec2 test instance - name: "Confirm whether the check mode is working normally."
ec2_instance_info: assert:
filters: that:
"tag:Name": "{{ resource_prefix }}-test-no-wait-checkmode" - "{{ real_instance_fact.instances | length }} > 0"
<<: *aws_connection_info - "{{ checkmode_instance_fact.instances | length }} == 0"
register: checkmode_instance_fact
- name: "Confirm whether the check mode is working normally." - name: Terminate instances
assert: ec2_instance:
that: instance_ids: "{{ in_test_vpc.instance_ids }}"
- "{{ real_instance_fact.instances | length }} > 0" state: absent
- "{{ checkmode_instance_fact.instances | length }} == 0"
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: # A Note about ec2 environment variable name preference:
# - EC2_URL -> AWS_URL # - EC2_URL -> AWS_URL
# - EC2_ACCESS_KEY -> AWS_ACCESS_KEY_ID -> AWS_ACCESS_KEY # - EC2_ACCESS_KEY -> AWS_ACCESS_KEY_ID -> AWS_ACCESS_KEY
@ -6,46 +13,31 @@
# - EC2_REGION -> AWS_REGION # - EC2_REGION -> AWS_REGION
# #
- block: - name: Wrap up all tests and setup AWS credentials
module_defaults:
- set_fact: group/aws:
virtualenv: "{{ remote_tmp_dir }}/virtualenv" aws_access_key: "{{ aws_access_key }}"
virtualenv_command: "{{ ansible_python_interpreter }} -m virtualenv" aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token | default(omit) }}"
- set_fact: region: "{{ aws_region }}"
virtualenv_interpreter: "{{ virtualenv }}/bin/python" block:
- pip: - name: Find AMI to use
name: virtualenv ec2_ami_info:
owners: 'amazon'
- pip: filters:
name: name: '{{ ec2_ami_name }}'
- 'botocore<1.10.16' register: ec2_amis
- boto3 - name: Set fact with latest AMI
- coverage
virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}"
virtualenv_site_packages: no
- include_tasks: version_fail.yml
vars: vars:
ansible_python_interpreter: "{{ virtualenv_interpreter }}" latest_ami: '{{ ec2_amis.images | sort(attribute="creation_date") | last }}'
set_fact:
- file: ec2_ami_image: '{{ latest_ami.image_id }}'
path: "{{ virtualenv }}"
state: absent
- pip:
name:
- 'botocore>=1.10.16'
- boto3
- coverage
virtualenv: "{{ virtualenv }}"
virtualenv_command: "{{ virtualenv_command }}"
virtualenv_site_packages: no
- include_tasks: version_fail_wrapper.yml
- include_tasks: env_setup.yml - include_tasks: env_setup.yml
- include_tasks: cpu_options.yml - include_tasks: cpu_options.yml
- include_tasks: termination_protection.yml - include_tasks: termination_protection.yml
- include_tasks: tags_and_vpc_settings.yml - include_tasks: tags_and_vpc_settings.yml

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

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

@ -1,23 +1,13 @@
- block: - 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) - name: create t3.nano with cpu options (fails gracefully)
ec2_instance: ec2_instance:
state: present state: present
name: "ansible-test-{{ resource_prefix | regex_search('([0-9]+)$') }}-ec2" name: "ansible-test-{{ resource_prefix | regex_search('([0-9]+)$') }}-ec2"
image_id: "{{ ec2_ami_image[aws_region] }}" image_id: "{{ ec2_ami_image }}"
instance_type: t3.nano instance_type: "t3.nano"
cpu_options: cpu_options:
core_count: 1 core_count: 1
threads_per_core: 1 threads_per_core: 1
<<: *aws_connection_info
register: ec2_instance_cpu_options_creation register: ec2_instance_cpu_options_creation
ignore_errors: yes ignore_errors: yes
@ -32,5 +22,5 @@
ec2_instance: ec2_instance:
state: absent state: absent
name: "ansible-test-{{ resource_prefix | regex_search('([0-9]+)$') }}-ec2" name: "ansible-test-{{ resource_prefix | regex_search('([0-9]+)$') }}-ec2"
<<: *aws_connection_info wait: no
ignore_errors: yes 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: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.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_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:parameter-type-not-in-doc
lib/ansible/modules/cloud/amazon/ec2_key.py validate-modules:doc-missing-type 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 lib/ansible/modules/cloud/amazon/ec2_launch_template.py validate-modules:nonexistent-parameter-documented

Loading…
Cancel
Save