-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
aws_ec2 fix hostnames SUMMARY aws_ec2- fix hostnames bugs Should fix #582 and #583 Fix returned hosts when hostnames: - tag:Tag1,Tag2 Fix returned hosts when hostnames: - tag:Tag1 - tag:Tag2 Fix returned hosts when hostnames: - tag:Tag1=Test1,Tag2=Test2 Given and EC2 instance with the following tags tags: Tag1: Test1 Tag2: Test2 Instead of only returning "aws_ec2": { "hosts": [ "Test1" ] }, "tag_Name_instance_02": { "hosts": [ "Test1" ] }, "tag_Tag1_Test1": { "hosts": [ "Test1" ] }, "tag_Tag2_Test2": { "hosts": [ "Test1" ] } It returns now { "_meta": { "hostvars": { "Test1": { "ami_launch_index": 0, "ansible_host": "10.210.0.101", ...}, "Test2": { "ami_launch_index": 0, "ansible_host": "10.210.0.101", ...}, }, "all": { "children": [ "aws_ec2", "tag_Name_instance_02", "tag_Tag1_Test1", "tag_Tag2_Test2", "ungrouped" ] }, "aws_ec2": { "hosts": [ "Test1", "Test2" ] }, "tag_Name_instance_02": { "hosts": [ "Test1", "Test2" ] }, "tag_Tag1_Test1": { "hosts": [ "Test1", "Test2" ] }, "tag_Tag2_Test2": { "hosts": [ "Test1", "Test2" ] } } ISSUE TYPE Bugfix Pull Request COMPONENT NAME aws_ec2 ADDITIONAL INFORMATION Reviewed-by: Mark Chappell <None> Reviewed-by: Alina Buzachis <None> Reviewed-by: Markus Bergholz <[email protected]> (cherry picked from commit ca33389)
- Loading branch information
1 parent
1f04c2d
commit 5d8c255
Showing
8 changed files
with
208 additions
and
22 deletions.
There are no files selected for viewing
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,2 @@ | ||
bugfixes: | ||
- aws_ec2 - ensure the correct number of hosts are returned when tags as hostnames are used (https://github.com/ansible-collections/amazon.aws/pull/862). |
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
62 changes: 62 additions & 0 deletions
62
...rgets/inventory_aws_ec2/playbooks/test_populating_inventory_with_hostnames_using_tags.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,62 @@ | ||
--- | ||
- hosts: 127.0.0.1 | ||
connection: local | ||
gather_facts: false | ||
environment: "{{ ansible_test.environment }}" | ||
tasks: | ||
|
||
- module_defaults: | ||
group/aws: | ||
aws_access_key: '{{ aws_access_key }}' | ||
aws_secret_key: '{{ aws_secret_key }}' | ||
security_token: '{{ security_token | default(omit) }}' | ||
region: '{{ aws_region }}' | ||
block: | ||
|
||
# Create VPC, subnet, security group, and find image_id to create instance | ||
- include_tasks: setup.yml | ||
|
||
# Create new host | ||
- name: create a new host | ||
ec2_instance: | ||
image_id: '{{ image_id }}' | ||
name: '{{ resource_prefix }}' | ||
tags: | ||
Tag1: Test1 | ||
Tag2: Test2 | ||
instance_type: t2.micro | ||
security_groups: '{{ sg_id }}' | ||
vpc_subnet_id: '{{ subnet_id }}' | ||
wait: false | ||
register: setup_instance | ||
|
||
# refresh inventory | ||
- meta: refresh_inventory | ||
|
||
- debug: | ||
var: groups | ||
|
||
- name: assert groups and hostvars were populated with inventory | ||
assert: | ||
that: | ||
- "'aws_ec2' in groups" | ||
- groups['aws_ec2'] | length == 2 | ||
- "'Tag1_Test1' in groups['aws_ec2']" | ||
- "'Tag2_Test2' in groups['aws_ec2']" | ||
- "'Tag1_Test1' in hostvars" | ||
- "'Tag2_Test2' in hostvars" | ||
|
||
always: | ||
|
||
- name: remove ec2 instance | ||
ec2_instance: | ||
instance_type: t2.micro | ||
instance_ids: '{{ setup_instance.instance_ids }}' | ||
state: absent | ||
name: '{{ resource_prefix }}' | ||
security_groups: "{{ sg_id }}" | ||
vpc_subnet_id: "{{ subnet_id }}" | ||
ignore_errors: true | ||
when: setup_instance is defined | ||
|
||
- include_tasks: tear_down.yml |
62 changes: 62 additions & 0 deletions
62
...ventory_aws_ec2/playbooks/test_populating_inventory_with_hostnames_using_tags_classic.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,62 @@ | ||
--- | ||
- hosts: 127.0.0.1 | ||
connection: local | ||
gather_facts: false | ||
environment: "{{ ansible_test.environment }}" | ||
tasks: | ||
|
||
- module_defaults: | ||
group/aws: | ||
aws_access_key: '{{ aws_access_key }}' | ||
aws_secret_key: '{{ aws_secret_key }}' | ||
security_token: '{{ security_token | default(omit) }}' | ||
region: '{{ aws_region }}' | ||
block: | ||
|
||
# Create VPC, subnet, security group, and find image_id to create instance | ||
- include_tasks: setup.yml | ||
|
||
# Create new host | ||
- name: create a new host | ||
ec2_instance: | ||
image_id: '{{ image_id }}' | ||
name: '{{ resource_prefix }}' | ||
tags: | ||
Tag1: Test1 | ||
Tag2: Test2 | ||
instance_type: t2.micro | ||
security_groups: '{{ sg_id }}' | ||
vpc_subnet_id: '{{ subnet_id }}' | ||
wait: false | ||
register: setup_instance | ||
|
||
# refresh inventory | ||
- meta: refresh_inventory | ||
|
||
- debug: | ||
var: groups | ||
|
||
- name: assert groups and hostvars were populated with inventory | ||
assert: | ||
that: | ||
- "'aws_ec2' in groups" | ||
- groups['aws_ec2'] | length == 2 | ||
- "'Test1' in groups['aws_ec2']" | ||
- "'Test2' in groups['aws_ec2']" | ||
- "'Test1' in hostvars" | ||
- "'Test2' in hostvars" | ||
|
||
always: | ||
|
||
- name: remove ec2 instance | ||
ec2_instance: | ||
instance_type: t2.micro | ||
instance_ids: '{{ setup_instance.instance_ids }}' | ||
state: absent | ||
name: '{{ resource_prefix }}' | ||
security_groups: "{{ sg_id }}" | ||
vpc_subnet_id: "{{ subnet_id }}" | ||
ignore_errors: true | ||
when: setup_instance is defined | ||
|
||
- include_tasks: tear_down.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
21 changes: 21 additions & 0 deletions
21
...ntegration/targets/inventory_aws_ec2/templates/inventory_with_hostnames_using_tags.yml.j2
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,21 @@ | ||
plugin: amazon.aws.aws_ec2 | ||
aws_access_key_id: '{{ aws_access_key }}' | ||
aws_secret_access_key: '{{ aws_secret_key }}' | ||
{% if security_token | default(false) %} | ||
aws_security_token: '{{ security_token }}' | ||
{% endif %} | ||
regions: | ||
- '{{ aws_region }}' | ||
keyed_groups: | ||
- prefix: tag | ||
key: tags | ||
hostnames: | ||
# can also be specified using | ||
# - tag:Tag1,Tag2 | ||
# or | ||
# - tag:Tag1 | ||
# - tag:Tag2 | ||
# or | ||
- tag:Tag1=Test1,Tag2=Test2 | ||
compose: | ||
ansible_host: private_ip_address |
21 changes: 21 additions & 0 deletions
21
...on/targets/inventory_aws_ec2/templates/inventory_with_hostnames_using_tags_classic.yml.j2
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,21 @@ | ||
plugin: amazon.aws.aws_ec2 | ||
aws_access_key_id: '{{ aws_access_key }}' | ||
aws_secret_access_key: '{{ aws_secret_key }}' | ||
{% if security_token | default(false) %} | ||
aws_security_token: '{{ security_token }}' | ||
{% endif %} | ||
regions: | ||
- '{{ aws_region }}' | ||
keyed_groups: | ||
- prefix: tag | ||
key: tags | ||
hostnames: | ||
# can also be specified using | ||
# - tag:Tag1,Tag2 | ||
# or | ||
# - tag:Tag1=Test1,Tag2=Test2 | ||
# or | ||
- tag:Tag1 | ||
- tag:Tag2 | ||
compose: | ||
ansible_host: private_ip_address |
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