From ea5e08905676beeefa70bd1eba2865f6ac3553ff Mon Sep 17 00:00:00 2001 From: Brian Coca Date: Thu, 3 Mar 2016 13:25:47 -0500 Subject: [PATCH] restore initial json parsing attempt to loader fixes issues with extra vars json strings not being parsed correctly by the yaml parser --- lib/ansible/parsing/dataloader.py | 37 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/ansible/parsing/dataloader.py b/lib/ansible/parsing/dataloader.py index cc88074f8e9..a508eacdda6 100644 --- a/lib/ansible/parsing/dataloader.py +++ b/lib/ansible/parsing/dataloader.py @@ -72,24 +72,29 @@ class DataLoader(): Creates a python datastructure from the given data, which can be either a JSON or YAML string. ''' - - # YAML parser will take JSON as it is a subset. - if isinstance(data, AnsibleUnicode): - # The PyYAML's libyaml bindings use PyUnicode_CheckExact so - # they are unable to cope with our subclass. - # Unwrap and re-wrap the unicode so we can keep track of line - # numbers - in_data = text_type(data) - else: - in_data = data + new_data = None try: - new_data = self._safe_load(in_data, file_name=file_name) - except YAMLError as yaml_exc: - self._handle_error(yaml_exc, file_name, show_content) + # we first try to load this data as JSON + new_data = json.loads(data) + except: + # must not be JSON, let the rest try + if isinstance(data, AnsibleUnicode): + # The PyYAML's libyaml bindings use PyUnicode_CheckExact so + # they are unable to cope with our subclass. + # Unwrap and re-wrap the unicode so we can keep track of line + # numbers + in_data = text_type(data) + else: + in_data = data + try: + new_data = self._safe_load(in_data, file_name=file_name) + except YAMLError as yaml_exc: + self._handle_error(yaml_exc, file_name, show_content) + + if isinstance(data, AnsibleUnicode): + new_data = AnsibleUnicode(new_data) + new_data.ansible_pos = data.ansible_pos - if isinstance(data, AnsibleUnicode): - new_data = AnsibleUnicode(new_data) - new_data.ansible_pos = data.ansible_pos return new_data def load_from_file(self, file_name):