diff options
author | Björn Andersson <ba@sanitarium.se> | 2015-01-21 06:09:22 +0800 |
---|---|---|
committer | Björn Andersson <ba@sanitarium.se> | 2015-01-21 08:07:17 +0800 |
commit | 65e4f2b2bc917031df899fe09ada2916899db06b (patch) | |
tree | 4ee05fb534bdef4312b2c18b103225907fb3c5e7 | |
parent | 2a04663255c3a06dea430c57d9db2b46d1e81ffd (diff) | |
download | ansible-65e4f2b2bc917031df899fe09ada2916899db06b.tar.gz |
Add filter to turn a string into a UUID
This filter was made because I needed to create idempotent UUIDs when
installing the agent for Go (http://go.cd), which uses UUIds to
distinguish the agents from each other.
It uses a newly created Ansible namespace to distinguish UUIDs created
by Ansible from any other source. The new namespace is a random one
created by uuidgen on OSX.
-rw-r--r-- | docsite/rst/playbooks_variables.rst | 4 | ||||
-rw-r--r-- | lib/ansible/runner/filter_plugins/core.py | 9 | ||||
-rw-r--r-- | test/units/TestFilters.py | 5 |
3 files changed, 18 insertions, 0 deletions
diff --git a/docsite/rst/playbooks_variables.rst b/docsite/rst/playbooks_variables.rst index ed57783177..2e3d67373e 100644 --- a/docsite/rst/playbooks_variables.rst +++ b/docsite/rst/playbooks_variables.rst @@ -409,6 +409,10 @@ To work with Base64 encoded strings:: {{ encoded | b64decode }} {{ decoded | b64encode }} +To create a UUID from a string (new in version 1.9):: + + {{ hostname | to_uuid }} + To cast values as certain types, such as when you input a string as "True" from a vars_prompt and the system doesn't know it is a boolean value:: diff --git a/lib/ansible/runner/filter_plugins/core.py b/lib/ansible/runner/filter_plugins/core.py index 98e9d0ef42..9330bbc539 100644 --- a/lib/ansible/runner/filter_plugins/core.py +++ b/lib/ansible/runner/filter_plugins/core.py @@ -29,6 +29,7 @@ import hashlib import string import operator as py_operator from random import SystemRandom, shuffle +import uuid import yaml from jinja2.filters import environmentfilter @@ -38,6 +39,9 @@ from ansible import errors from ansible.utils 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) @@ -297,6 +301,8 @@ def get_encrypted_password(password, hashtype='sha512', salt=None): return None +def to_uuid(string): + return str(uuid.uuid5(UUID_NAMESPACE_ANSIBLE, str(string))) class FilterModule(object): ''' Ansible core jinja2 filters ''' @@ -307,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, diff --git a/test/units/TestFilters.py b/test/units/TestFilters.py index 7d921a7e37..d15147b098 100644 --- a/test/units/TestFilters.py +++ b/test/units/TestFilters.py @@ -131,6 +131,11 @@ class TestFilters(unittest.TestCase): 'a\\1') assert a == 'ansible' + def test_to_uuid(self): + a = ansible.runner.filter_plugins.core.to_uuid('example.com') + + assert a == 'ae780c3a-a3ab-53c2-bfb4-098da300b3fe' + #def test_filters(self): # this test is pretty low level using a playbook, hence I am disabling it for now -- MPD. |