diff --git a/changelogs/fragments/759-ec2_win_password.yml b/changelogs/fragments/759-ec2_win_password.yml new file mode 100644 index 00000000000..a13df0dd63e --- /dev/null +++ b/changelogs/fragments/759-ec2_win_password.yml @@ -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). diff --git a/plugins/modules/ec2_win_password.py b/plugins/modules/ec2_win_password.py index 3ed0afb79d4..00bd603ed97 100644 --- a/plugins/modules/ec2_win_password.py +++ b/plugins/modules/ec2_win_password.py @@ -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. @@ -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(): @@ -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') @@ -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: @@ -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.')