Skip to content

Commit

Permalink
ec2_win_password - migrate to boto3
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Oct 15, 2021
1 parent 9beae2c commit 6e3830c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/759-ec2_win_password.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ec2_win_password - module updated to use the boto3 AWS SDK (https://github.com/ansible-collections/community.aws/pull/759).
26 changes: 17 additions & 9 deletions plugins/modules/ec2_win_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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.
Expand Down Expand Up @@ -110,11 +109,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 @@ -130,6 +133,14 @@ def setup_module_object():
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 +155,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 +209,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 6e3830c

Please sign in to comment.