From e6d01ff7e302211ed01efda13fdcb1e9b3121b50 Mon Sep 17 00:00:00 2001 From: Christopher Warner Date: Wed, 17 Jan 2018 20:15:10 -0500 Subject: [PATCH] fix for ec2 module terminating instances outside of inventory -- Fixes #19427 (#26621) * Proposed fix for issue 19427 * Use string_types and to_text for evaluation, if not on empty dict. * if int, to_text and continue on --- lib/ansible/modules/cloud/amazon/ec2.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/ansible/modules/cloud/amazon/ec2.py b/lib/ansible/modules/cloud/amazon/ec2.py index 7628b4989b8..f7504dec546 100644 --- a/lib/ansible/modules/cloud/amazon/ec2.py +++ b/lib/ansible/modules/cloud/amazon/ec2.py @@ -692,6 +692,10 @@ def get_reservations(module, ec2, vpc, tags=None, state=None, zone=None): except: pass + # if not a string type, convert and make sure it's a text string + if isinstance(tags, int): + tags = to_text(tags) + # if string, we only care that a tag of that name exists if isinstance(tags, str): filters.update({"tag-key": tags}) @@ -710,6 +714,10 @@ def get_reservations(module, ec2, vpc, tags=None, state=None, zone=None): tags = _set_none_to_blank(tags) filters.update(dict(("tag:" + tn, tv) for (tn, tv) in tags.items())) + # lets check to see if the filters dict is empty, if so then stop + if not filters: + module.fail_json(msg="Filters based on tag is empty => tags: %s" % (tags)) + if state: # http://stackoverflow.com/questions/437511/what-are-the-valid-instancestates-for-the-amazon-ec2-api filters.update({'instance-state-name': state})