Skip to content

Commit

Permalink
ec2_instance When searching for instances fall back to tag(Name) para…
Browse files Browse the repository at this point in the history
…meter if no name and no filter provided (ansible-collections#529)

ec2_instance When searching for instances fall back to tag(Name) parameter if no name and no filter provided

SUMMARY
Reported in ansible-collections#526
When the name parameter isn't set but the Name tag is set the Name tag is ignored when searching for existing instances.
The documentation states:
- By default, instances are filtered for counting by their "Name" tag, base AMI, state (running, by default), and
  subnet ID.

As such I think we should consider this a bug.
ISSUE TYPE

Bugfix Pull Request

COMPONENT NAME
ec2_instance
ADDITIONAL INFORMATION
fixes: ansible-collections#526

Reviewed-by: Alina Buzachis <None>
Reviewed-by: None <None>
  • Loading branch information
tremble authored Oct 12, 2021
1 parent a1ef229 commit 92e6797
Show file tree
Hide file tree
Showing 4 changed files with 496 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/526-ec2_instance_search_tags.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- ec2_instance - ensure that ec2_instance falls back to the tag(Name) parameter when no filter and no name parameter is passed (https://github.com/ansible-collections/amazon.aws/issues/526).
4 changes: 4 additions & 0 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,6 +1798,10 @@ def build_filters():

if module.params.get('name'):
filters['tag:Name'] = [module.params.get('name')]
elif module.params.get('tags'):
name_tag = module.params.get('tags').get('Name', None)
if name_tag:
filters['tag:Name'] = [name_tag]

if module.params.get('image_id'):
filters['image-id'] = [module.params.get('image_id')]
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/ec2_instance/inventory
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[tests]
instance_minimal
checkmode_tests
termination_protection
ebs_optimized
Expand Down
Loading

0 comments on commit 92e6797

Please sign in to comment.