Example #1
0
    def dump(stuff):

        if context.CLIARGS['yaml']:
            import yaml
            from ansible.parsing.yaml.dumper import AnsibleDumper
            results = yaml.dump(stuff,
                                Dumper=AnsibleDumper,
                                default_flow_style=False)
        elif context.CLIARGS['toml']:
            from ansible.plugins.inventory.toml import toml_dumps, HAS_TOML
            if not HAS_TOML:
                raise AnsibleError(
                    'The python "toml" library is required when using the TOML output format'
                )
            results = toml_dumps(stuff)
        else:
            import json
            from ansible.parsing.ajson import AnsibleJSONEncoder
            results = json.dumps(stuff,
                                 cls=AnsibleJSONEncoder,
                                 sort_keys=True,
                                 indent=4,
                                 preprocess_unsafe=True)

        return results
Example #2
0
    def dump(stuff):

        if context.CLIARGS['yaml']:
            import yaml
            from ansible.parsing.yaml.dumper import AnsibleDumper
            results = yaml.dump(stuff,
                                Dumper=AnsibleDumper,
                                default_flow_style=False)
        elif context.CLIARGS['toml']:
            from ansible.plugins.inventory.toml import toml_dumps, HAS_TOML
            if not HAS_TOML:
                raise AnsibleError(
                    'The python "toml" library is required when using the TOML output format'
                )
            results = toml_dumps(stuff)
        else:
            import json
            from ansible.parsing.ajson import AnsibleJSONEncoder
            try:
                results = json.dumps(stuff,
                                     cls=AnsibleJSONEncoder,
                                     sort_keys=True,
                                     indent=4,
                                     preprocess_unsafe=True)
            except TypeError as e:
                results = json.dumps(stuff,
                                     cls=AnsibleJSONEncoder,
                                     sort_keys=False,
                                     indent=4,
                                     preprocess_unsafe=True)
                display.warning(
                    "Could not sort JSON output due to issues while sorting keys: %s"
                    % to_native(e))

        return results
Example #3
0
    def dump(stuff):

        if context.CLIARGS['yaml']:
            import yaml
            from ansible.parsing.yaml.dumper import AnsibleDumper
            results = to_text(
                yaml.dump(stuff,
                          Dumper=AnsibleDumper,
                          default_flow_style=False,
                          allow_unicode=True))
        elif context.CLIARGS['toml']:
            from ansible.plugins.inventory.toml import toml_dumps
            try:
                results = toml_dumps(stuff)
            except TypeError as e:
                raise AnsibleError(
                    'The source inventory contains a value that cannot be represented in TOML: %s'
                    % e)
            except KeyError as e:
                raise AnsibleError(
                    'The source inventory contains a non-string key (%s) which cannot be represented in TOML. '
                    'The specified key will need to be converted to a string. Be aware that if your playbooks '
                    'expect this key to be non-string, your playbooks will need to be modified to support this '
                    'change.' % e.args[0])
        else:
            import json
            from ansible.parsing.ajson import AnsibleJSONEncoder
            try:
                results = json.dumps(stuff,
                                     cls=AnsibleJSONEncoder,
                                     sort_keys=True,
                                     indent=4,
                                     preprocess_unsafe=True,
                                     ensure_ascii=False)
            except TypeError as e:
                results = json.dumps(stuff,
                                     cls=AnsibleJSONEncoder,
                                     sort_keys=False,
                                     indent=4,
                                     preprocess_unsafe=True,
                                     ensure_ascii=False)
                display.warning(
                    "Could not sort JSON output due to issues while sorting keys: %s"
                    % to_native(e))

        return results
Example #4
0
def to_toml(o):
    if not isinstance(o, MutableMapping):
        raise AnsibleFilterError('to_toml requires a dict, got %s' % type(o))
    return to_text(toml_dumps(o), errors='surrogate_or_strict')