1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

ec2_win_password: fix broken import and minor updates (#34479)

* fix broken import

* ansible.module_utils.ec2.HAS_BOTO is already used

* wait_timeout parameter: use 'int' type

* wait is always True there

* doc: use formatting function
This commit is contained in:
Pilou 2018-01-12 02:24:46 +01:00 committed by Will Thames
parent 607c66f80a
commit 00332a49d5
2 changed files with 14 additions and 17 deletions

View file

@ -16,7 +16,7 @@ DOCUMENTATION = '''
module: ec2_win_password module: ec2_win_password
short_description: gets the default administrator password for ec2 windows instances short_description: gets the default administrator password for ec2 windows instances
description: description:
- Gets the default administrator password from any EC2 Windows instance. The instance is referenced by its id (e.g. i-XXXXXXX). This module - 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. has a dependency on python-boto.
version_added: "2.0" version_added: "2.0"
author: "Rick Mendes (@rickmendes)" author: "Rick Mendes (@rickmendes)"
@ -33,7 +33,7 @@ options:
version_added: "2.0" version_added: "2.0"
description: description:
- The passphrase for the instance key pair. The key must use DES or 3DES encryption for this module to decrypt it. You can use openssl to - The passphrase for the instance key pair. The key must use DES or 3DES encryption for this module to decrypt it. You can use openssl to
convert your password protected keys if they do not use DES or 3DES. ex) openssl rsa -in current_key -out new_key -des3. convert your password protected keys if they do not use DES or 3DES. ex) C(openssl rsa -in current_key -out new_key -des3).
required: false required: false
default: null default: null
wait: wait:
@ -98,24 +98,19 @@ import datetime
import time import time
from base64 import b64decode from base64 import b64decode
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
from cryptography.hazmat.primitives.serialization import load_pem_private_key
try: try:
import boto.ec2 from cryptography.hazmat.backends import default_backend
HAS_BOTO = True from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
from cryptography.hazmat.primitives.serialization import load_pem_private_key
HAS_CRYPTOGRAPHY = True
except ImportError: except ImportError:
HAS_BOTO = False HAS_CRYPTOGRAPHY = False
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import HAS_BOTO, ec2_argument_spec, ec2_connect from ansible.module_utils.ec2 import HAS_BOTO, ec2_argument_spec, ec2_connect
from ansible.module_utils._text import to_bytes from ansible.module_utils._text import to_bytes
BACKEND = default_backend()
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = ec2_argument_spec()
argument_spec.update(dict( argument_spec.update(dict(
@ -123,7 +118,7 @@ def main():
key_file=dict(required=True, type='path'), key_file=dict(required=True, type='path'),
key_passphrase=dict(no_log=True, default=None, required=False), key_passphrase=dict(no_log=True, default=None, required=False),
wait=dict(type='bool', default=False, required=False), wait=dict(type='bool', default=False, required=False),
wait_timeout=dict(default=120, required=False), wait_timeout=dict(default=120, required=False, type='int'),
) )
) )
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)
@ -131,6 +126,9 @@ def main():
if not HAS_BOTO: if not HAS_BOTO:
module.fail_json(msg='Boto required for this module.') module.fail_json(msg='Boto required for this module.')
if not HAS_CRYPTOGRAPHY:
module.fail_json(msg='cryptography package required for this module.')
instance_id = module.params.get('instance_id') instance_id = module.params.get('instance_id')
key_file = module.params.get('key_file') key_file = module.params.get('key_file')
if module.params.get('key_passphrase') is None: if module.params.get('key_passphrase') is None:
@ -138,7 +136,7 @@ def main():
else: else:
b_key_passphrase = to_bytes(module.params.get('key_passphrase'), errors='surrogate_or_strict') b_key_passphrase = to_bytes(module.params.get('key_passphrase'), errors='surrogate_or_strict')
wait = module.params.get('wait') wait = module.params.get('wait')
wait_timeout = int(module.params.get('wait_timeout')) wait_timeout = module.params.get('wait_timeout')
ec2 = ec2_connect(module) ec2 = ec2_connect(module)
@ -149,7 +147,7 @@ def main():
while datetime.datetime.now() < end: while datetime.datetime.now() < end:
data = ec2.get_password_data(instance_id) data = ec2.get_password_data(instance_id)
decoded = b64decode(data) decoded = b64decode(data)
if wait and not decoded: if not decoded:
time.sleep(5) time.sleep(5)
else: else:
break break
@ -167,7 +165,7 @@ def main():
else: else:
try: try:
with f: with f:
key = load_pem_private_key(f.read(), b_key_passphrase, BACKEND) key = load_pem_private_key(f.read(), b_key_passphrase, default_backend())
except (ValueError, TypeError) as e: except (ValueError, TypeError) as e:
module.fail_json(msg="unable to parse key file") module.fail_json(msg="unable to parse key file")

View file

@ -1,4 +1,3 @@
lib/ansible/modules/cloud/amazon/ec2_win_password.py
lib/ansible/modules/cloud/azure/azure_rm_dnsrecordset.py lib/ansible/modules/cloud/azure/azure_rm_dnsrecordset.py
lib/ansible/modules/cloud/dimensiondata/dimensiondata_network.py lib/ansible/modules/cloud/dimensiondata/dimensiondata_network.py
lib/ansible/modules/cloud/webfaction/webfaction_app.py lib/ansible/modules/cloud/webfaction/webfaction_app.py