|
|
|
@ -15,23 +15,33 @@
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import base64
|
|
|
|
import base64
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
import os.path
|
|
|
|
import os.path
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
import types
|
|
|
|
import types
|
|
|
|
import pipes
|
|
|
|
import pipes
|
|
|
|
import glob
|
|
|
|
import glob
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import collections
|
|
|
|
import collections
|
|
|
|
|
|
|
|
import crypt
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
import string
|
|
|
|
import operator as py_operator
|
|
|
|
import operator as py_operator
|
|
|
|
from distutils.version import LooseVersion, StrictVersion
|
|
|
|
|
|
|
|
from random import SystemRandom, shuffle
|
|
|
|
from random import SystemRandom, shuffle
|
|
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
from jinja2.filters import environmentfilter
|
|
|
|
from jinja2.filters import environmentfilter
|
|
|
|
|
|
|
|
from distutils.version import LooseVersion, StrictVersion
|
|
|
|
|
|
|
|
|
|
|
|
from ansible.errors import *
|
|
|
|
from ansible import errors
|
|
|
|
from ansible.utils.hashing import md5s, checksum_s
|
|
|
|
from ansible.utils.hashing import md5s, checksum_s
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UUID_NAMESPACE_ANSIBLE = uuid.UUID('361E6D51-FAEC-444A-9079-341386DA8E2E')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to_nice_yaml(*a, **kw):
|
|
|
|
def to_nice_yaml(*a, **kw):
|
|
|
|
'''Make verbose, human readable yaml'''
|
|
|
|
'''Make verbose, human readable yaml'''
|
|
|
|
return yaml.safe_dump(*a, indent=4, allow_unicode=True, default_flow_style=False, **kw)
|
|
|
|
return yaml.safe_dump(*a, indent=4, allow_unicode=True, default_flow_style=False, **kw)
|
|
|
|
@ -42,6 +52,22 @@ def to_json(a, *args, **kw):
|
|
|
|
|
|
|
|
|
|
|
|
def to_nice_json(a, *args, **kw):
|
|
|
|
def to_nice_json(a, *args, **kw):
|
|
|
|
'''Make verbose, human readable JSON'''
|
|
|
|
'''Make verbose, human readable JSON'''
|
|
|
|
|
|
|
|
# python-2.6's json encoder is buggy (can't encode hostvars)
|
|
|
|
|
|
|
|
if sys.version_info < (2, 7):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
import simplejson
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
major = int(simplejson.__version__.split('.')[0])
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
if major >= 2:
|
|
|
|
|
|
|
|
return simplejson.dumps(a, indent=4, sort_keys=True, *args, **kw)
|
|
|
|
|
|
|
|
# Fallback to the to_json filter
|
|
|
|
|
|
|
|
return to_json(a, *args, **kw)
|
|
|
|
return json.dumps(a, indent=4, sort_keys=True, *args, **kw)
|
|
|
|
return json.dumps(a, indent=4, sort_keys=True, *args, **kw)
|
|
|
|
|
|
|
|
|
|
|
|
def failed(*a, **kw):
|
|
|
|
def failed(*a, **kw):
|
|
|
|
@ -243,6 +269,41 @@ def randomize_list(mylist):
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
return mylist
|
|
|
|
return mylist
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_hash(data, hashtype='sha1'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: # see if hash is supported
|
|
|
|
|
|
|
|
h = hashlib.new(hashtype)
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
h.update(data)
|
|
|
|
|
|
|
|
return h.hexdigest()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_encrypted_password(password, hashtype='sha512', salt=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: find a way to construct dynamically from system
|
|
|
|
|
|
|
|
cryptmethod= {
|
|
|
|
|
|
|
|
'md5': '1',
|
|
|
|
|
|
|
|
'blowfish': '2a',
|
|
|
|
|
|
|
|
'sha256': '5',
|
|
|
|
|
|
|
|
'sha512': '6',
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hastype = hashtype.lower()
|
|
|
|
|
|
|
|
if hashtype in cryptmethod:
|
|
|
|
|
|
|
|
if salt is None:
|
|
|
|
|
|
|
|
r = SystemRandom()
|
|
|
|
|
|
|
|
salt = ''.join([r.choice(string.ascii_letters + string.digits) for _ in range(16)])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
saltstring = "$%s$%s" % (cryptmethod[hashtype],salt)
|
|
|
|
|
|
|
|
encrypted = crypt.crypt(password,saltstring)
|
|
|
|
|
|
|
|
return encrypted
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def to_uuid(string):
|
|
|
|
|
|
|
|
return str(uuid.uuid5(UUID_NAMESPACE_ANSIBLE, str(string)))
|
|
|
|
|
|
|
|
|
|
|
|
class FilterModule(object):
|
|
|
|
class FilterModule(object):
|
|
|
|
''' Ansible core jinja2 filters '''
|
|
|
|
''' Ansible core jinja2 filters '''
|
|
|
|
|
|
|
|
|
|
|
|
@ -252,6 +313,9 @@ class FilterModule(object):
|
|
|
|
'b64decode': base64.b64decode,
|
|
|
|
'b64decode': base64.b64decode,
|
|
|
|
'b64encode': base64.b64encode,
|
|
|
|
'b64encode': base64.b64encode,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# uuid
|
|
|
|
|
|
|
|
'to_uuid': to_uuid,
|
|
|
|
|
|
|
|
|
|
|
|
# json
|
|
|
|
# json
|
|
|
|
'to_json': to_json,
|
|
|
|
'to_json': to_json,
|
|
|
|
'to_nice_json': to_nice_json,
|
|
|
|
'to_nice_json': to_nice_json,
|
|
|
|
@ -295,6 +359,9 @@ class FilterModule(object):
|
|
|
|
'sha1': checksum_s,
|
|
|
|
'sha1': checksum_s,
|
|
|
|
# checksum of string as used by ansible for checksuming files
|
|
|
|
# checksum of string as used by ansible for checksuming files
|
|
|
|
'checksum': checksum_s,
|
|
|
|
'checksum': checksum_s,
|
|
|
|
|
|
|
|
# generic hashing
|
|
|
|
|
|
|
|
'password_hash': get_encrypted_password,
|
|
|
|
|
|
|
|
'hash': get_hash,
|
|
|
|
|
|
|
|
|
|
|
|
# file glob
|
|
|
|
# file glob
|
|
|
|
'fileglob': fileglob,
|
|
|
|
'fileglob': fileglob,
|
|
|
|
|