From 79799f681903141e815e4c44b035590124549f12 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Thu, 6 Mar 2014 21:42:01 -0500 Subject: [PATCH] Allow any file that can be in YAML to also be in JSON. This is primarily done to support non-visual editors better. --- CHANGELOG.md | 1 + lib/ansible/utils/__init__.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35934bc541a..c36e6524ac0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Major features/changes: * The deprecated legacy variable templating system has been finally removed. Use {{ foo }} always not $foo or ${foo}. * Role dependencies are now tracked across multiple plays, making common roles easier to include in dependencies without any special variable tricks. +* Any data file can also be JSON. Use sparingly -- with great power comes great responsibility. Starting file with "{" or "[" denotes JSON. New Modules: diff --git a/lib/ansible/utils/__init__.py b/lib/ansible/utils/__init__.py index c3e777e4d65..1d09cfde990 100644 --- a/lib/ansible/utils/__init__.py +++ b/lib/ansible/utils/__init__.py @@ -42,6 +42,7 @@ import traceback import getpass import sys import textwrap +import json #import vault from vault import VaultLib @@ -351,7 +352,16 @@ def smush_ds(data): return data def parse_yaml(data): - ''' convert a yaml string to a data structure ''' + ''' convert a yaml string to a data structure. Also supports JSON, ssssssh!!!''' + + data = data.lstrip() + if data.startswith("{") or data.startswith("["): + # since the line starts with { or [ we can infer this is a JSON document. + loaded = json.loads(data) + else: + # else this is pretty sure to be a YAML document + loaded = yaml.safe_load(data) + return smush_ds(yaml.safe_load(data)) def process_common_errors(msg, probline, column):