mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-29 05:41:25 -07:00
Merge pull request #4078 from mkaluza/ansible
add 'chars' parameter to password plugin to allow custom character set
This commit is contained in:
commit
288c33e286
1 changed files with 14 additions and 0 deletions
|
@ -21,6 +21,8 @@ from ansible import utils, errors
|
||||||
import os
|
import os
|
||||||
import errno
|
import errno
|
||||||
from string import ascii_letters, digits
|
from string import ascii_letters, digits
|
||||||
|
import string
|
||||||
|
import random
|
||||||
|
|
||||||
|
|
||||||
class LookupModule(object):
|
class LookupModule(object):
|
||||||
|
@ -48,6 +50,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
|
||||||
|
@ -57,6 +60,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(',,',',').split(','))
|
||||||
|
paramvals['chars'] = use_chars
|
||||||
else:
|
else:
|
||||||
paramvals[name] = value
|
paramvals[name] = value
|
||||||
except (ValueError, AssertionError) as e:
|
except (ValueError, AssertionError) as e:
|
||||||
|
@ -64,6 +72,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)
|
||||||
|
@ -71,8 +80,13 @@ 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 = ascii_letters + digits + ".,:-_"
|
||||||
password = utils.random_password(length)
|
password = utils.random_password(length)
|
||||||
|
"""
|
||||||
|
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:
|
if encrypt is not None:
|
||||||
salt = self.random_salt()
|
salt = self.random_salt()
|
||||||
content = '%s salt=%s' % (password, salt)
|
content = '%s salt=%s' % (password, salt)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue