mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 20:31:27 -07:00
[AWS] Support check mode in aws ec2_instance module (#46774)
* fix checkmode * Added checking test to existing test playbook * Added test for check mode
This commit is contained in:
parent
81640a2c67
commit
1cce11b39c
10 changed files with 502 additions and 0 deletions
|
@ -1112,6 +1112,11 @@ def await_instances(ids, state='OK'):
|
|||
if not module.params.get('wait', True):
|
||||
# the user asked not to wait for anything
|
||||
return
|
||||
|
||||
if module.check_mode:
|
||||
# In check mode, there is no change even if you wait.
|
||||
return
|
||||
|
||||
state_opts = {
|
||||
'OK': 'instance_status_ok',
|
||||
'STOPPED': 'instance_stopped',
|
||||
|
@ -1344,6 +1349,10 @@ def change_instance_state(filters, desired_state, ec2=None):
|
|||
for inst in instances:
|
||||
try:
|
||||
if desired_state == 'TERMINATED':
|
||||
if module.check_mode:
|
||||
changed.add(inst['InstanceId'])
|
||||
continue
|
||||
|
||||
# TODO use a client-token to prevent double-sends of these start/stop/terminate commands
|
||||
# https://docs.aws.amazon.com/AWSEC2/latest/APIReference/Run_Instance_Idempotency.html
|
||||
resp = ec2.terminate_instances(InstanceIds=[inst['InstanceId']])
|
||||
|
@ -1352,9 +1361,18 @@ def change_instance_state(filters, desired_state, ec2=None):
|
|||
if inst['State']['Name'] == 'stopping':
|
||||
unchanged.add(inst['InstanceId'])
|
||||
continue
|
||||
|
||||
if module.check_mode:
|
||||
changed.add(inst['InstanceId'])
|
||||
continue
|
||||
|
||||
resp = ec2.stop_instances(InstanceIds=[inst['InstanceId']])
|
||||
[changed.add(i['InstanceId']) for i in resp['StoppingInstances']]
|
||||
if desired_state == 'RUNNING':
|
||||
if module.check_mode:
|
||||
changed.add(inst['InstanceId'])
|
||||
continue
|
||||
|
||||
resp = ec2.start_instances(InstanceIds=[inst['InstanceId']])
|
||||
[changed.add(i['InstanceId']) for i in resp['StartingInstances']]
|
||||
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError):
|
||||
|
@ -1423,6 +1441,12 @@ def ensure_present(existing_matches, changed, ec2, state):
|
|||
)
|
||||
try:
|
||||
instance_spec = build_run_instance_spec(module.params)
|
||||
# If check mode is enabled,suspend 'ensure function'.
|
||||
if module.check_mode:
|
||||
module.exit_json(
|
||||
changed=True,
|
||||
spec=instance_spec,
|
||||
)
|
||||
instance_response = run_instances(ec2, **instance_spec)
|
||||
instances = instance_response['Instances']
|
||||
instance_ids = [i['InstanceId'] for i in instances]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue