summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Cammarata <jimi@sngx.net>2014-05-19 10:26:06 -0500
committerJames Cammarata <jimi@sngx.net>2014-05-21 14:59:51 -0500
commita54b8764fa6cbfee10ead9fc8501c2a3f471d02a (patch)
tree34f1d428404653367d7ebc84174767bb9785fa5d
parent6b4584d0044a9397fe19e73e9830343775d79cd7 (diff)
downloadansible-a54b8764fa6cbfee10ead9fc8501c2a3f471d02a.tar.gz
Reset locale to 'C' if the specified one is invalid
Fixes #7448
-rw-r--r--lib/ansible/module_utils/basic.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py
index 23c7b15800..33056ca81f 100644
--- a/lib/ansible/module_utils/basic.py
+++ b/lib/ansible/module_utils/basic.py
@@ -43,6 +43,7 @@ BOOLEANS = BOOLEANS_TRUE + BOOLEANS_FALSE
# of an ansible module. The source of this common code lives
# in lib/ansible/module_common.py
+import locale
import os
import re
import pipes
@@ -191,6 +192,10 @@ class AnsibleModule(object):
if k not in self.argument_spec:
self.argument_spec[k] = v
+ # check the locale as set by the current environment, and
+ # reset to LANG=C if it's an invalid/unavailable locale
+ self._check_locale()
+
(self.params, self.args) = self._load_params()
self._legal_inputs = ['CHECKMODE', 'NO_LOG']
@@ -561,6 +566,22 @@ class AnsibleModule(object):
kwargs['state'] = 'absent'
return kwargs
+ def _check_locale(self):
+ '''
+ Uses the locale module to test the currently set locale
+ (per the LANG and LC_CTYPE environment settings)
+ '''
+ try:
+ # setting the locale to '' uses the default locale
+ # as it would be returned by locale.getdefaultlocale()
+ locale.setlocale(locale.LC_ALL, '')
+ except locale.Error, e:
+ # fallback to the 'C' locale, which may cause unicode
+ # issues but is preferable to simply failing because
+ # of an unknown locale
+ locale.setlocale(locale.LC_ALL, 'C')
+ except Exception, e:
+ self.fail_json(msg="An unknown error was encountered while attempting to validate the locale: %s" % e)
def _handle_aliases(self):
aliases_results = {} #alias:canon