Enabled unsafe and vault in JSON (#38759)

* allow to load json marked as unsafe or vault

 * centralized json code/decode, add vault support
 * use generics to allow for more varied inputs
 * allow inventory to dump vault w/o decrypting
 * override simplejson also
 * add entry for unsafe also
 * load vaulted and unsafe json, support unvaulting if secrets provided
This commit is contained in:
Brian Coca 2018-05-09 14:01:51 -04:00 committed by GitHub
parent ff16e993be
commit cbb6a7f4e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 99 additions and 56 deletions

View file

@ -13,10 +13,10 @@ from yaml import YAMLError
from ansible.errors import AnsibleParserError
from ansible.errors.yaml_strings import YAML_SYNTAX_ERROR
from ansible.module_utils.six import text_type
from ansible.module_utils._text import to_native
from ansible.parsing.yaml.loader import AnsibleLoader
from ansible.parsing.yaml.objects import AnsibleBaseYAMLObject
from ansible.parsing.ajson import AnsibleJSONDecoder
__all__ = ('from_yaml',)
@ -62,9 +62,12 @@ def from_yaml(data, file_name='<string>', show_content=True, vault_secrets=None)
new_data = None
try:
# we first try to load this data as JSON. Fixes issues with extra vars json strings not
# being parsed correctly by the yaml parser
new_data = json.loads(data)
# in case we have to deal with vaults
AnsibleJSONDecoder.set_secrets(vault_secrets)
# we first try to load this data as JSON.
# Fixes issues with extra vars json strings not being parsed correctly by the yaml parser
new_data = json.loads(data, cls=AnsibleJSONDecoder)
except Exception:
# must not be JSON, let the rest try
try: