Skip to content

Commit

Permalink
Merge pull request ansible-collections#759 from tremble/boto3/ec2_win…
Browse files Browse the repository at this point in the history
…_password

ec2_win_password - migrate to boto3

SUMMARY
Migrate ec2_win_password to boto3
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
ec2_win_password
ADDITIONAL INFORMATION
While there are no tests for this module, there's only a single boto call to update
boto2 documentation of call: http://boto.cloudhackers.com/en/latest/ref/ec2.html#boto.ec2.connection.EC2Connection.get_password_data
boto3 documentation of call: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.get_password_data

Reviewed-by: Rick Mendes <None>
Reviewed-by: None <None>
Reviewed-by: Alina Buzachis <None>
  • Loading branch information
ansible-zuul[bot] authored Oct 15, 2021
2 parents 9a509b6 + 7748178 commit 921dd83
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions ec2_win_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
---
module: ec2_win_password
version_added: 1.0.0
short_description: Gets the default administrator password for ec2 windows instances
short_description: Gets the default administrator password for EC2 Windows instances
description:
- Gets the default administrator password from any EC2 Windows instance. The instance is referenced by its id (e.g. C(i-XXXXXXX)).
- This module has a dependency on python-boto.
author: "Rick Mendes (@rickmendes)"
options:
instance_id:
Expand Down Expand Up @@ -55,10 +54,6 @@
requirements:
- cryptography
- boto >= 2.49.0
notes:
- As of Ansible 2.4, this module requires the python cryptography module rather than the
older pycrypto module.
'''

EXAMPLES = '''
Expand Down Expand Up @@ -110,11 +105,15 @@
except ImportError:
HAS_CRYPTOGRAPHY = False

try:
import botocore
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils._text import to_bytes

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import HAS_BOTO
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import ec2_connect
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry


def setup_module_object():
Expand All @@ -126,10 +125,19 @@ def setup_module_object():
wait=dict(type='bool', default=False, required=False),
wait_timeout=dict(default=120, required=False, type='int'),
)
module = AnsibleAWSModule(argument_spec=argument_spec)
mutually_exclusive = [['key_file', 'key_data']]
module = AnsibleAWSModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive)
return module


def _get_password(module, client, instance_id):
try:
data = client.get_password_data(aws_retry=True, InstanceId=instance_id)['PasswordData']
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg='Failed to get password data')
return data


def ec2_win_password(module):
instance_id = module.params.get('instance_id')
key_file = module.params.get('key_file')
Expand All @@ -144,21 +152,21 @@ def ec2_win_password(module):
wait = module.params.get('wait')
wait_timeout = module.params.get('wait_timeout')

ec2 = ec2_connect(module)
client = module.client('ec2', retry_decorator=AWSRetry.jittered_backoff())

if wait:
start = datetime.datetime.now()
end = start + datetime.timedelta(seconds=wait_timeout)

while datetime.datetime.now() < end:
data = ec2.get_password_data(instance_id)
data = _get_password(module, client, instance_id)
decoded = b64decode(data)
if not decoded:
time.sleep(5)
else:
break
else:
data = ec2.get_password_data(instance_id)
data = _get_password(module, client, instance_id)
decoded = b64decode(data)

if wait and datetime.datetime.now() >= end:
Expand Down Expand Up @@ -198,9 +206,6 @@ def ec2_win_password(module):
def main():
module = setup_module_object()

if not HAS_BOTO:
module.fail_json(msg='Boto required for this module.')

if not HAS_CRYPTOGRAPHY:
module.fail_json(msg='cryptography package required for this module.')

Expand Down

0 comments on commit 921dd83

Please sign in to comment.