Skip to content

Commit

Permalink
ec2_instance - avoid changing module.params (ansible-collections#1187)
Browse files Browse the repository at this point in the history
ec2_instance - avoid changing module.params

SUMMARY
Update ec2_instance so that module.params isn't modified
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
ec2_instance
ADDITIONAL INFORMATION

Reviewed-by: Gonéri Le Bouder <goneri@lebouder.net>
Reviewed-by: Mark Chappell <None>
  • Loading branch information
tremble authored Oct 24, 2022
1 parent 35fd507 commit a3f19b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
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')
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

0 comments on commit a3f19b5

Please sign in to comment.