diff --git a/lib/ansible/modules/extras/cloud/amazon/ec2_win_password.py b/lib/ansible/modules/extras/cloud/amazon/ec2_win_password.py index f3a687c237..30fbcc08a0 100644 --- a/lib/ansible/modules/extras/cloud/amazon/ec2_win_password.py +++ b/lib/ansible/modules/extras/cloud/amazon/ec2_win_password.py @@ -144,8 +144,11 @@ def main(): if wait and datetime.datetime.now() >= end: module.fail_json(msg = "wait for password timeout after %d seconds" % wait_timeout) - f = open(key_file, 'r') - key = RSA.importKey(f.read(), key_passphrase) + try: + f = open(key_file, 'r') + key = RSA.importKey(f.read(), key_passphrase) + finally: + f.close() cipher = PKCS1_v1_5.new(key) sentinel = 'password decryption failed!!!'