-
Notifications
You must be signed in to change notification settings - Fork 350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ec2_instance - Fix issue with launch template #2323
Merged
softwarefactory-project-zuul
merged 4 commits into
ansible-collections:main
from
abikouo:ec2_instance_with_launch_template_v1
Dec 4, 2024
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
...nts/20241004-ec2_instance-fix-issue-with-launch_template-when-there-no-default-subnet.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
minor_changes: | ||
- ec2_instance - Fix the issue when trying to run instances using launch template in an AWS environment where no default subnet is defined(https://github.com/ansible-collections/amazon.aws/issues/2321). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tests/integration/targets/ec2_instance_launch_template/aliases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
time=5m | ||
cloud/aws | ||
ec2_instance |
6 changes: 6 additions & 0 deletions
6
tests/integration/targets/ec2_instance_launch_template/meta/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
dependencies: | ||
- role: setup_ec2_facts | ||
- role: setup_ec2_instance_env | ||
vars: | ||
ec2_instance_test_name: launch-template |
112 changes: 112 additions & 0 deletions
112
tests/integration/targets/ec2_instance_launch_template/tasks/main.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
--- | ||
- module_defaults: | ||
group/aws: | ||
access_key: "{{ aws_access_key }}" | ||
secret_key: "{{ aws_secret_key }}" | ||
session_token: "{{ security_token | default(omit) }}" | ||
region: "{{ aws_region }}" | ||
block: | ||
- name: Create a Launch template | ||
community.aws.ec2_launch_template: | ||
abikouo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
template_name: "{{ resource_prefix }}-template" | ||
instance_type: t2.micro | ||
image_id: "{{ ec2_ami_id }}" | ||
block_device_mappings: | ||
- device_name: /dev/sdb | ||
ebs: | ||
volume_size: 20 | ||
delete_on_termination: true | ||
volume_type: standard | ||
network_interfaces: | ||
- device_index: 0 | ||
associate_public_ip_address: false | ||
delete_on_termination: true | ||
subnet_id: "{{ testing_subnet_a.subnet.id }}" | ||
description: "A network interface from the testing subnet a" | ||
register: _launch_template | ||
|
||
- name: Create EC2 instance using launch template (check mode) | ||
amazon.aws.ec2_instance: | ||
state: present | ||
wait: true | ||
launch_template: | ||
id: "{{ _launch_template.template.launch_template_id }}" | ||
register: _create_check | ||
check_mode: true | ||
|
||
- name: Ensure module reported change while running in check mode | ||
ansible.builtin.assert: | ||
that: | ||
- _create_check is changed | ||
- '"instance_ids" not in _create_check' | ||
|
||
- name: Create EC2 instance using launch template | ||
amazon.aws.ec2_instance: | ||
state: present | ||
launch_template: | ||
id: "{{ _launch_template.template.launch_template_id }}" | ||
wait: true | ||
register: _instance_a | ||
|
||
- name: Set instances to delete | ||
ansible.builtin.set_fact: | ||
test_instance_ids: "{{ _instance_a.instance_ids | default([]) }}" | ||
|
||
- name: Validate instance created as expected | ||
ansible.builtin.assert: | ||
that: | ||
- _instance_a is changed | ||
- '"instance_ids" in _instance_a' | ||
- '"instances" in _instance_a' | ||
- _instance_a.instances | length == 1 | ||
- _instance_a.instances[0].instance_type == 't2.micro' | ||
- _instance_a.instances[0].image_id == ec2_ami_id | ||
- _instance_a.instances[0].network_interfaces | length == 1 | ||
- _instance_a.instances[0].network_interfaces[0].subnet_id == testing_subnet_a.subnet.id | ||
- _instance_a.instances[0].network_interfaces[0].description == "A network interface from the testing subnet a" | ||
# AWS adds automatically a tag with the launch template id | ||
- '"aws:ec2launchtemplate:id" in _instance_a.instances[0].tags' | ||
- _instance_a.instances[0].tags["aws:ec2launchtemplate:id"] == _launch_template.template.launch_template_id | ||
|
||
- name: Create antoher EC2 instance using launch template and some override parameters | ||
amazon.aws.ec2_instance: | ||
state: present | ||
launch_template: | ||
id: "{{ _launch_template.template.launch_template_id }}" | ||
instance_type: t3.nano | ||
network_interfaces: | ||
- device_index: 0 | ||
assign_public_ip: false | ||
delete_on_termination: true | ||
subnet_id: "{{ testing_subnet_b.subnet.id }}" | ||
description: "A network interface from the testing subnet b" | ||
wait: true | ||
register: _instance_b | ||
|
||
- name: Set instances to delete | ||
ansible.builtin.set_fact: | ||
test_instance_ids: "{{ test_instance_ids + _instance_b.instance_ids | default([])}}" | ||
|
||
- name: Validate instance created as expected | ||
ansible.builtin.assert: | ||
that: | ||
- _instance_b is changed | ||
- '"instance_ids" in _instance_b' | ||
- _instance_a.instance_ids != _instance_b.instance_ids | ||
- _instance_b.instances | length == 1 | ||
- _instance_b.instances[0].instance_type == 't3.nano' | ||
- _instance_b.instances[0].image_id == ec2_ami_id | ||
- _instance_b.instances[0].network_interfaces | length == 1 | ||
- _instance_b.instances[0].network_interfaces[0].subnet_id == testing_subnet_b.subnet.id | ||
- _instance_b.instances[0].network_interfaces[0].description == "A network interface from the testing subnet b" | ||
# AWS adds automatically a tag with the launch template id | ||
- '"aws:ec2launchtemplate:id" in _instance_b.instances[0].tags' | ||
- _instance_b.instances[0].tags["aws:ec2launchtemplate:id"] == _launch_template.template.launch_template_id | ||
|
||
always: | ||
- name: Delete instances | ||
amazon.aws.ec2_instance: | ||
state: absent | ||
instance_ids: "{{ test_instance_ids }}" | ||
wait: true | ||
when: test_instance_ids is defined |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I won't block the PR on this, but as far as I can tell, this is the only line that actually needs to be changed to fix the bug. I'd really prefer that any refactoring should happen in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handling the refactoring in a separate PR makes sense. I'll leave the decision to @abikouo , as he is the original author of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gravesm the PR does not include any code refactoring, all changes are aimed at fixing the reported issue