Created
August 12, 2015 22:52
-
-
Save glaszig/1ba07d81d010b653f92d to your computer and use it in GitHub Desktop.
ansible passlib filter plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this ansible/jinja2 filter plugin allows you to use passlib's *_crypt functions | |
# until ansible 2.0 comes out - see https://github.com/ansible/ansible/issues/11244. | |
# | |
# this filter depends on passlib being installed: | |
# $ pip install passlib | |
# | |
# put this into your playbook's `filter_plugins` folder. | |
# | |
# usage example: | |
# - name: create user | |
# user: | |
# name: username | |
# password: "{{ user_password | passlib_hash('sha512', user_salt) }}" | |
from ansible import errors | |
try: | |
import passlib | |
except Exception, e: | |
raise errors.AnsibleFilterError('passlib package is not installed') | |
def passlib_hash(pw, alg = 'sha512', salt = None, rounds = None, implicit_rounds = None, relaxed = None): | |
return crypt_method(alg).encrypt(pw, salt = salt, rounds = rounds, implicit_rounds = implicit_rounds, relaxed = relaxed) | |
def crypt_method(alg): | |
return getattr(passlib.hash, alg + '_crypt') | |
class FilterModule(object): | |
def filters(self): | |
return { | |
'passlib_hash' : passlib_hash | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment