Visitar URL original
ec2_instance - avoid manipulating module.params by tremble · Pull Request #1337 · ansible-collections/amazon.aws · GitHub
Skip to content
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/1337-ec2_instance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_instance - updated to avoid manipulating ``module.params`` (https://github.com/ansible-collections/amazon.aws/pull/1337).
14 changes: 6 additions & 8 deletions plugins/modules/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ def build_instance_tags(params, propagate_tags_to_volumes=True):
return specs


def build_run_instance_spec(params):
def build_run_instance_spec(params, current_count=0):

spec = dict(
ClientToken=uuid.uuid4().hex,
Expand All @@ -1342,8 +1342,8 @@ def build_run_instance_spec(params):
spec['IamInstanceProfile'] = dict(Arn=determine_iam_role(params.get('iam_instance_profile')))

if params.get('exact_count'):
spec['MaxCount'] = params.get('to_launch')
spec['MinCount'] = params.get('to_launch')
spec['MaxCount'] = params.get('exact_count') - current_count
spec['MinCount'] = params.get('exact_count') - current_count

if params.get('count'):
spec['MaxCount'] = params.get('count')
Expand Down Expand Up @@ -1833,11 +1833,9 @@ def enforce_count(existing_matches, module, desired_module_state):
)

elif current_count < exact_count:
to_launch = exact_count - current_count
module.params['to_launch'] = to_launch
# launch instances
try:
ensure_present(existing_matches=existing_matches, desired_module_state=desired_module_state)
ensure_present(existing_matches=existing_matches, desired_module_state=desired_module_state, current_count=current_count)
except botocore.exceptions.ClientError as e:
module.fail_json(e, msg='Unable to launch instances')
elif current_count > exact_count:
Expand Down Expand Up @@ -1867,14 +1865,14 @@ def enforce_count(existing_matches, module, desired_module_state):
module.fail_json_aws(e, msg="Failed to enforce instance count")


def ensure_present(existing_matches, desired_module_state):
def ensure_present(existing_matches, desired_module_state, current_count=None):
tags = dict(module.params.get('tags') or {})
name = module.params.get('name')
if name:
tags['Name'] = name

try:
instance_spec = build_run_instance_spec(module.params)
instance_spec = build_run_instance_spec(module.params, current_count)
# If check mode is enabled,suspend 'ensure function'.
if module.check_mode:
module.exit_json(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,20 @@ def test_build_run_instance_spec_exact_count(params_object, ec2_instance):
# The "exact_count" logic relies on enforce_count doing the math to figure out how many
# instances to start/stop. The enforce_count call is responsible for ensuring that 'to_launch'
# is set and is a positive integer.
params_object['exact_count'] = sentinel.EXACT_COUNT
params_object['exact_count'] = 42
params_object['to_launch'] = sentinel.TO_LAUNCH
instance_spec = ec2_instance.build_run_instance_spec(params_object)

_assert_defaults(instance_spec, ['MaxCount', 'MinCount'])
assert 'MaxCount' in instance_spec
assert 'MinCount' in instance_spec
assert instance_spec['MaxCount'] == sentinel.TO_LAUNCH
assert instance_spec['MinCount'] == sentinel.TO_LAUNCH
assert instance_spec['MaxCount'] == 42
assert instance_spec['MinCount'] == 42

instance_spec = ec2_instance.build_run_instance_spec(params_object, 7)

_assert_defaults(instance_spec, ['MaxCount', 'MinCount'])
assert 'MaxCount' in instance_spec
assert 'MinCount' in instance_spec
assert instance_spec['MaxCount'] == 35
assert instance_spec['MinCount'] == 35