From 6b5291d68f150c629e9958bb6e910b529b0d8cef Mon Sep 17 00:00:00 2001 From: James Cammarata Date: Wed, 2 Aug 2017 16:11:55 -0500 Subject: [PATCH] Fix incorrect use of datetime in letsencrypt Also correctly sets the locale for run_command executions using the module provided method for setting the locale instead of the locale module in python. --- lib/ansible/modules/web_infrastructure/letsencrypt.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/ansible/modules/web_infrastructure/letsencrypt.py b/lib/ansible/modules/web_infrastructure/letsencrypt.py index b7db6fd91f9..8771a98f719 100644 --- a/lib/ansible/modules/web_infrastructure/letsencrypt.py +++ b/lib/ansible/modules/web_infrastructure/letsencrypt.py @@ -159,7 +159,6 @@ import binascii import copy import hashlib import json -import locale import os import re import shutil @@ -215,12 +214,12 @@ def get_cert_days(module, cert_file): _, out, _ = module.run_command(openssl_cert_cmd, check_rc=True) try: not_after_str = re.search(r"\s+Not After\s*:\s+(.*)", out.decode('utf8')).group(1) - not_after = datetime.datetime.fromtimestamp(time.mktime(time.strptime(not_after_str, '%b %d %H:%M:%S %Y %Z'))) + not_after = datetime.fromtimestamp(time.mktime(time.strptime(not_after_str, '%b %d %H:%M:%S %Y %Z'))) except AttributeError: module.fail_json(msg="No 'Not after' date found in {0}".format(cert_file)) except ValueError: module.fail_json(msg="Failed to parse 'Not after' date of {0}".format(cert_file)) - now = datetime.datetime.utcnow() + now = datetime.utcnow() return (not_after - now).days @@ -789,7 +788,7 @@ def main(): ) # AnsibleModule() changes the locale, so change it back to C because we rely on time.strptime() when parsing certificate dates. - locale.setlocale(locale.LC_ALL, "C") + module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C', LC_CTYPE='C') cert_days = get_cert_days(module, module.params['dest']) if cert_days < module.params['remaining_days']: