mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
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
This commit is contained in:
parent
808d9596b2
commit
aab445a28f
1 changed files with 9 additions and 1 deletions
|
@ -22,6 +22,7 @@ import os
|
||||||
import errno
|
import errno
|
||||||
import random
|
import random
|
||||||
from string import ascii_letters, digits
|
from string import ascii_letters, digits
|
||||||
|
import string
|
||||||
|
|
||||||
|
|
||||||
class LookupModule(object):
|
class LookupModule(object):
|
||||||
|
@ -52,6 +53,7 @@ class LookupModule(object):
|
||||||
paramvals = {
|
paramvals = {
|
||||||
'length': LookupModule.LENGTH,
|
'length': LookupModule.LENGTH,
|
||||||
'encrypt': None,
|
'encrypt': None,
|
||||||
|
'chars': ['ascii_letters','digits',".,:-_"],
|
||||||
}
|
}
|
||||||
|
|
||||||
# get non-default parameters if specified
|
# get non-default parameters if specified
|
||||||
|
@ -61,6 +63,11 @@ class LookupModule(object):
|
||||||
assert(name in paramvals)
|
assert(name in paramvals)
|
||||||
if name == 'length':
|
if name == 'length':
|
||||||
paramvals[name] = int(value)
|
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:
|
else:
|
||||||
paramvals[name] = value
|
paramvals[name] = value
|
||||||
except (ValueError, AssertionError) as e:
|
except (ValueError, AssertionError) as e:
|
||||||
|
@ -68,6 +75,7 @@ class LookupModule(object):
|
||||||
|
|
||||||
length = paramvals['length']
|
length = paramvals['length']
|
||||||
encrypt = paramvals['encrypt']
|
encrypt = paramvals['encrypt']
|
||||||
|
use_chars = paramvals['chars']
|
||||||
|
|
||||||
# get password or create it if file doesn't exist
|
# get password or create it if file doesn't exist
|
||||||
path = utils.path_dwim(self.basedir, relpath)
|
path = utils.path_dwim(self.basedir, relpath)
|
||||||
|
@ -75,7 +83,7 @@ class LookupModule(object):
|
||||||
pathdir = os.path.dirname(path)
|
pathdir = os.path.dirname(path)
|
||||||
if not os.path.isdir(pathdir):
|
if not os.path.isdir(pathdir):
|
||||||
os.makedirs(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))
|
password = ''.join(random.choice(chars) for _ in range(length))
|
||||||
if encrypt is not None:
|
if encrypt is not None:
|
||||||
salt = self.random_salt()
|
salt = self.random_salt()
|
||||||
|
|
Loading…
Reference in a new issue