Skip to content
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 - avoid changing module.params #1187

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/1187-ec2_instance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_instance - avoid changing ``module.params`` (https://github.com/ansible-collections/amazon.aws/pull/1187).
30 changes: 14 additions & 16 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,15 +1598,15 @@ def get_default_subnet(vpc, availability_zone=None):
return None


def ensure_instance_state(desired_module_state):
def ensure_instance_state(desired_module_state, filters):
"""
Sets return keys depending on the desired instance state
"""
results = dict()
changed = False
if desired_module_state in ('running', 'started'):
_changed, failed, instances, failure_reason = change_instance_state(
filters=module.params.get('filters'), desired_module_state=desired_module_state)
filters=filters, desired_module_state=desired_module_state)
changed |= bool(len(_changed))

if failed:
Expand All @@ -1630,8 +1630,7 @@ def ensure_instance_state(desired_module_state):
# The Ansible behaviour of issuing a stop/start has a minor impact on user billing
# This will need to be changelogged if we ever change to client.reboot_instance
_changed, failed, instances, failure_reason = change_instance_state(
filters=module.params.get('filters'),
desired_module_state='stopped',
filters=filters, desired_module_state='stopped',
)

if failed:
Expand All @@ -1642,8 +1641,7 @@ def ensure_instance_state(desired_module_state):

changed |= bool(len(_changed))
_changed, failed, instances, failure_reason = change_instance_state(
filters=module.params.get('filters'),
desired_module_state=desired_module_state,
filters=filters, desired_module_state=desired_module_state,
)
changed |= bool(len(_changed))

Expand All @@ -1662,8 +1660,7 @@ def ensure_instance_state(desired_module_state):
)
elif desired_module_state in ('stopped',):
_changed, failed, instances, failure_reason = change_instance_state(
filters=module.params.get('filters'),
desired_module_state=desired_module_state,
filters=filters, desired_module_state=desired_module_state,
)
changed |= bool(len(_changed))

Expand All @@ -1682,7 +1679,7 @@ def ensure_instance_state(desired_module_state):
)
elif desired_module_state in ('absent', 'terminated'):
terminated, terminate_failed, instances, failure_reason = change_instance_state(
filters=module.params.get('filters'),
filters=filters,
desired_module_state=desired_module_state,
)

Expand Down Expand Up @@ -1803,7 +1800,7 @@ def determine_iam_role(name_or_arn):
module.fail_json_aws(e, msg="An error occurred while searching for iam_instance_profile {0}. Please try supplying the full ARN.".format(name_or_arn))


def handle_existing(existing_matches, state):
def handle_existing(existing_matches, state, filters):
tags = module.params.get('tags')
purge_tags = module.params.get('purge_tags')
name = module.params.get('name')
Expand Down Expand Up @@ -1842,7 +1839,7 @@ def handle_existing(existing_matches, state):
changes=changes,
)

state_results = ensure_instance_state(state)
state_results = ensure_instance_state(state, filters)
alter_config_result['changed'] |= state_results.pop('changed', False)
result = {**state_results, **alter_config_result}

Expand Down Expand Up @@ -2097,14 +2094,15 @@ def main():
)
client = module.client('ec2', retry_decorator=retry_decorator)

if module.params.get('filters') is None:
module.params['filters'] = build_filters()
filters = module.params.get('filters')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write this like that: filters = module.params.get('filters') or build_filters()

Copy link
Contributor Author

@tremble tremble Oct 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or build_filters() is not the same as is None, In theory {} (which later gets converted to []) is a valid filter, the use of which the or variant would block.

if filters is None:
filters = build_filters()

existing_matches = find_instances(filters=module.params.get('filters'))
existing_matches = find_instances(filters=filters)

if state in ('terminated', 'absent'):
if existing_matches:
result = ensure_instance_state(state)
result = ensure_instance_state(state, filters)
else:
result = dict(
msg='No matching instances found',
Expand All @@ -2116,7 +2114,7 @@ def main():
for match in existing_matches:
warn_if_public_ip_assignment_changed(match)
warn_if_cpu_options_changed(match)
result = handle_existing(existing_matches, state)
result = handle_existing(existing_matches, state, filters=filters)
else:
result = ensure_present(existing_matches=existing_matches, desired_module_state=state)

Expand Down