From aab445a28fd039d55a2a1bdcbe3511bd7366ceda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Ka=C5=82u=C5=BCa?= Date: Tue, 10 Sep 2013 11:49:10 +0200 Subject: [PATCH 1/2] add 'chars' parameter to password plugin to allow custom character set It accepts comma separated list of names that are either string module attributes (ascii_letters,digits, etc) or are used literally To enter comma use two commas ',,' somewhere - preferably at the end Qoutes and double qoutes are not supported --- lib/ansible/runner/lookup_plugins/password.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/ansible/runner/lookup_plugins/password.py b/lib/ansible/runner/lookup_plugins/password.py index a09ca4071e..3d988e03c6 100644 --- a/lib/ansible/runner/lookup_plugins/password.py +++ b/lib/ansible/runner/lookup_plugins/password.py @@ -22,6 +22,7 @@ import os import errno import random from string import ascii_letters, digits +import string class LookupModule(object): @@ -52,6 +53,7 @@ class LookupModule(object): paramvals = { 'length': LookupModule.LENGTH, 'encrypt': None, + 'chars': ['ascii_letters','digits',".,:-_"], } # get non-default parameters if specified @@ -61,6 +63,11 @@ class LookupModule(object): assert(name in paramvals) if name == 'length': paramvals[name] = int(value) + elif name == 'chars': + use_chars=[] + if ",," in value: use_chars.append(',') + use_chars.extend(value.replace(',,',',').replace('"','').replace("'",'').split(',')) + paramvals['chars'] = use_chars else: paramvals[name] = value except (ValueError, AssertionError) as e: @@ -68,6 +75,7 @@ class LookupModule(object): length = paramvals['length'] encrypt = paramvals['encrypt'] + use_chars = paramvals['chars'] # get password or create it if file doesn't exist path = utils.path_dwim(self.basedir, relpath) @@ -75,7 +83,7 @@ class LookupModule(object): pathdir = os.path.dirname(path) if not os.path.isdir(pathdir): os.makedirs(pathdir) - chars = ascii_letters + digits + ".,:-_" + chars = "".join([getattr(string,c,c) for c in use_chars]) password = ''.join(random.choice(chars) for _ in range(length)) if encrypt is not None: salt = self.random_salt() From 6786773797f3a27377f97ce408262379e655f734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Ka=C5=82u=C5=BCa?= Date: Tue, 10 Sep 2013 15:00:29 +0200 Subject: [PATCH 2/2] filter out qoutes since they cause random errors --- lib/ansible/runner/lookup_plugins/password.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ansible/runner/lookup_plugins/password.py b/lib/ansible/runner/lookup_plugins/password.py index 3d988e03c6..6da0fdbe37 100644 --- a/lib/ansible/runner/lookup_plugins/password.py +++ b/lib/ansible/runner/lookup_plugins/password.py @@ -66,7 +66,7 @@ class LookupModule(object): elif name == 'chars': use_chars=[] if ",," in value: use_chars.append(',') - use_chars.extend(value.replace(',,',',').replace('"','').replace("'",'').split(',')) + use_chars.extend(value.replace(',,',',').split(',')) paramvals['chars'] = use_chars else: paramvals[name] = value @@ -83,7 +83,7 @@ class LookupModule(object): pathdir = os.path.dirname(path) if not os.path.isdir(pathdir): os.makedirs(pathdir) - chars = "".join([getattr(string,c,c) for c in use_chars]) + chars = "".join([getattr(string,c,c) for c in use_chars]).replace('"','').replace("'",'') password = ''.join(random.choice(chars) for _ in range(length)) if encrypt is not None: salt = self.random_salt()