Skip to content

Commit

Permalink
Move community.aws tests from ec2 to ec2_instance (ansible-collection…
Browse files Browse the repository at this point in the history
…s#391)

* Move tests from ec2 to ec2_instance
* Make sure we wait for instances to go away
  • Loading branch information
tremble authored and ethemcemozkan committed Feb 18, 2021
1 parent 7c1a800 commit 0da43bc
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,25 @@
seconds: 10

- name: Create Linux EC2 instance
ec2:
ec2_instance:
instance_type: "{{instance_type}}"
image: "{{linux_ami_id}}"
image_id: "{{linux_ami_id}}"
wait: "yes"
count: 1
instance_profile_name: "{{role_output.iam_role.role_name}}"
instance_tags:
Name: "{{resource_prefix}}-integration-test-aws-ssm-linux"
instance_role: "{{role_output.iam_role.role_name}}"
name: "{{resource_prefix}}-integration-test-aws-ssm-linux"
user_data: |
#!/bin/sh
sudo systemctl start amazon-ssm-agent
state: present
register: linux_output

- name: Create Windows EC2 instance
ec2:
ec2_instance:
instance_type: "{{instance_type}}"
image: "{{windows_ami_id}}"
image_id: "{{windows_ami_id}}"
wait: "yes"
count: 1
instance_profile_name: "{{role_output.iam_role.role_name}}"
instance_tags:
Name: "{{resource_prefix}}-integration-test-aws-ssm-windows"
instance_role: "{{role_output.iam_role.role_name}}"
name: "{{resource_prefix}}-integration-test-aws-ssm-windows"
user_data: |
<powershell>
Invoke-WebRequest -Uri "https://amazon-ssm-us-east-1.s3.amazonaws.com/latest/windows_amd64/AmazonSSMAgentSetup.exe" -OutFile "C:\AmazonSSMAgentSetup.exe"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
linux_instance_id: {{linux_output.instance_ids[0]}}
linux_instance_id: {{ linux_output.instances[0].instance_id }}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
windows_instance_id: {{windows_output.instance_ids[0]}}
windows_instance_id: {{ windows_output.instances[0].instance_id }}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[aws_ssm]
{{linux_output.instance_ids[0]}} ansible_aws_ssm_instance_id={{linux_output.instance_ids[0]}} ansible_aws_ssm_region={{aws_region}}
{{ linux_output.instances[0].instance_id }} ansible_aws_ssm_instance_id={{ linux_output.instances[0].instance_id }} ansible_aws_ssm_region={{ aws_region }}

[aws_ssm:vars]
ansible_connection=aws_ssm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[aws_ssm]
{{windows_output.instance_ids[0]}} ansible_aws_ssm_instance_id={{windows_output.instance_ids[0]}} ansible_aws_ssm_region={{aws_region}}
{{ windows_output.instances[0].instance_id }} ansible_aws_ssm_instance_id={{ windows_output.instances[0].instance_id }} ansible_aws_ssm_region={{ aws_regioni }}

[aws_ssm:vars]
ansible_shell_type=powershell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,20 @@
when: iam_role_vars_file.stat.exists == true

- name: Terminate Windows EC2 instances that were previously launched
ec2:
ec2_instance:
instance_ids:
- "{{windows_instance_id}}"
state: absent
wait: True
ignore_errors: yes
when: ec2_windows_vars_file.stat.exists == true

- name: Terminate Linux EC2 instances that were previously launched
ec2:
ec2_instance:
instance_ids:
- "{{linux_instance_id}}"
state: absent
wait: True
ignore_errors: yes
when: ec2_linux_vars_file.stat.exists == true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource_shortprefix: 'ansible-test-{{ resource_prefix | regex_search("([0-9]+)$") }}'
lambda_role_name: '{{ resource_shortprefix }}-elb-target-lambda'
#lambda_role_name: '{{ resource_prefix }}-elb-target-lambda'
lambda_name: '{{ resource_prefix }}-elb-target-lambda'
lambda_role_name: '{{ resource_shortprefix }}-elb-target'
#lambda_role_name: '{{ resource_prefix }}-elb-target'
lambda_name: '{{ resource_prefix }}-elb-target'
elb_target_group_name: '{{ resource_shortprefix }}-elb-tg'
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,14 @@
Description: "Created by {{ resource_prefix }}"

- name: set up ec2 instance to use as a target
ec2:
group_id: "{{ sg.group_id }}"
ec2_instance:
name: "{{ resource_prefix }}-inst"
security_group: "{{ sg.group_id }}"
instance_type: t3.micro
image: "{{ ec2_ami_image }}"
image_id: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ subnet_2.subnet.id }}"
instance_tags:
Name: "{{ resource_prefix }}-inst"
exact_count: 1
count_tag:
Name: "{{ resource_prefix }}-inst"
assign_public_ip: true
network:
assign_public_ip: true
volumes: []
wait: true
ebs_optimized: false
Expand All @@ -154,6 +151,9 @@
- echo "HELLO ANSIBLE" > /var/www/html/index.html
register: ec2

- set_fact:
instance_id: "{{ ec2.instances[0].instance_id }}"

- name: create an application load balancer
elb_application_lb:
name: "{{ lb_name }}"
Expand All @@ -180,7 +180,7 @@
- name: register an instance to unused target group
elb_target:
target_group_name: "{{ tg_name }}"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: present
register: result

Expand All @@ -189,14 +189,14 @@
that:
- result.changed
- result.target_group_arn
- result.target_health_descriptions.target.id == ec2.instance_ids[0]
- result.target_health_descriptions.target.id == instance_id

# ============================================================

- name: test idempotence
elb_target:
target_group_name: "{{ tg_name }}"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: present
register: result

Expand All @@ -210,7 +210,7 @@
- name: remove an unused target
elb_target:
target_group_name: "{{ tg_name }}"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: absent
deregister_unused: true
register: result
Expand All @@ -226,7 +226,7 @@
- name: register an instance to used target group and wait until healthy
elb_target:
target_group_name: "{{ tg_name }}-used"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: present
target_status: healthy
target_status_timeout: 400
Expand All @@ -237,15 +237,15 @@
that:
- result.changed
- result.target_group_arn
- result.target_health_descriptions.target.id == ec2.instance_ids[0]
- result.target_health_descriptions.target.id == instance_id
- result.target_health_descriptions.target_health == healthy_state

# ============================================================

- name: remove a target from used target group
elb_target:
target_group_name: "{{ tg_name }}-used"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: absent
target_status: unused
target_status_timeout: 400
Expand All @@ -261,7 +261,7 @@
- name: test idempotence
elb_target:
target_group_name: "{{ tg_name }}-used"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: absent
register: result

Expand All @@ -275,7 +275,7 @@
- name: register an instance to used target group and wait until healthy again to test deregistering differently
elb_target:
target_group_name: "{{ tg_name }}-used"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: present
target_status: healthy
target_status_timeout: 400
Expand All @@ -286,13 +286,13 @@
that:
- result.changed
- result.target_group_arn
- result.target_health_descriptions.target.id == ec2.instance_ids[0]
- result.target_health_descriptions.target.id == instance_id
- result.target_health_descriptions.target_health == healthy_state

- name: start deregisteration but don't wait
elb_target:
target_group_name: "{{ tg_name }}-used"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: absent
register: result

Expand All @@ -305,7 +305,7 @@
- name: now wait for target to finish deregistering
elb_target:
target_group_name: "{{ tg_name }}-used"
target_id: "{{ ec2.instance_ids[0] }}"
target_id: "{{ instance_id }}"
state: absent
target_status: unused
target_status_timeout: 400
Expand All @@ -325,20 +325,10 @@
debug: msg="********** Tearing down elb_target test dependencies **********"

- name: remove ec2 instance
ec2:
group_id: "{{ sg.group_id }}"
instance_type: t2.micro
image: "{{ ec2_ami_image }}"
vpc_subnet_id: "{{ subnet_2.subnet.id }}"
instance_tags:
Name: "{{ resource_prefix }}-inst"
exact_count: 0
count_tag:
Name: "{{ resource_prefix }}-inst"
assign_public_ip: true
volumes: []
wait: true
ebs_optimized: false
ec2_instance:
name: "{{ resource_prefix }}-inst"
state: absent
wait: True
ignore_errors: true

- name: remove testing target groups
Expand Down Expand Up @@ -480,5 +470,6 @@
register: removed
retries: 10
until: removed is not failed
ignore_errors: true

# ============================================================
Loading

0 comments on commit 0da43bc

Please sign in to comment.