From 03e213a27279e44599b65440f7ffb4722ef870c9 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 2 Feb 2015 10:23:25 -0800 Subject: [PATCH] Change the v2 filter plugins into a symlink to v1 as the plugins are the same for both --- v2/ansible/modules/core | 2 +- v2/ansible/plugins/filter/__init__.py | 21 -------- v2/ansible/plugins/filter/core.py | 75 +++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/v2/ansible/modules/core b/v2/ansible/modules/core index be744ce5e78..1394920cd3e 160000 --- a/v2/ansible/modules/core +++ b/v2/ansible/modules/core @@ -1 +1 @@ -Subproject commit be744ce5e78f217eb34c4d369847f1e174da9ae6 +Subproject commit 1394920cd3e440f5806463d0c1cfbe4a4b94f423 diff --git a/v2/ansible/plugins/filter/__init__.py b/v2/ansible/plugins/filter/__init__.py index 785fc459921..e69de29bb2d 100644 --- a/v2/ansible/plugins/filter/__init__.py +++ b/v2/ansible/plugins/filter/__init__.py @@ -1,21 +0,0 @@ -# (c) 2012-2014, Michael DeHaan -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . - -# Make coding more python3-ish -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - diff --git a/v2/ansible/plugins/filter/core.py b/v2/ansible/plugins/filter/core.py index 2ba1ecd693c..f14364b441e 100644 --- a/v2/ansible/plugins/filter/core.py +++ b/v2/ansible/plugins/filter/core.py @@ -15,23 +15,33 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see . +import sys import base64 import json import os.path -import yaml import types import pipes import glob import re import collections +import crypt +import hashlib +import string import operator as py_operator -from distutils.version import LooseVersion, StrictVersion from random import SystemRandom, shuffle -from jinja2.filters import environmentfilter +import uuid -from ansible.errors import * +import yaml +from jinja2.filters import environmentfilter +from distutils.version import LooseVersion, StrictVersion + +from ansible import errors from ansible.utils.hashing import md5s, checksum_s + +UUID_NAMESPACE_ANSIBLE = uuid.UUID('361E6D51-FAEC-444A-9079-341386DA8E2E') + + def to_nice_yaml(*a, **kw): '''Make verbose, human readable yaml''' 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): '''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) def failed(*a, **kw): @@ -243,6 +269,41 @@ def randomize_list(mylist): pass 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): ''' Ansible core jinja2 filters ''' @@ -252,6 +313,9 @@ class FilterModule(object): 'b64decode': base64.b64decode, 'b64encode': base64.b64encode, + # uuid + 'to_uuid': to_uuid, + # json 'to_json': to_json, 'to_nice_json': to_nice_json, @@ -295,6 +359,9 @@ class FilterModule(object): 'sha1': checksum_s, # checksum of string as used by ansible for checksuming files 'checksum': checksum_s, + # generic hashing + 'password_hash': get_encrypted_password, + 'hash': get_hash, # file glob 'fileglob': fileglob,