ansible / awx

AWX provides a web-based user interface, REST API, and task engine built on top of Ansible. It is one of the upstream projects for Red Hat Ansible Automation Platform.
Other
14.09k stars 3.43k forks source link

Return data formatted as YAML #15618

Open markfaine opened 2 weeks ago

markfaine commented 2 weeks ago

Please confirm the following

Feature type

New Feature

Feature Summary

When retrieving data like the following:

vars = client.hosts.get(name=hostname).results[0].get_related('variable_data')

The resulting data is return as a json structure:

{
    "node_availability": "active",
    "node_labels": {
        "ansible": "true"
    },
    "node_role": "worker",
    "remote_host_enabled": "true",
    "remote_host_id": 2504,
    "remote_tower_enabled": "true",
    "remote_tower_id": 2504,
}

However, when attempting to convert it to yaml using ruamel.yaml and the 'rt' or 'safe' dumper, this results in an error:

   vars = client.hosts.get(name=hostname).results[0].get_related('variable_data')
   yaml.dump(vars, sys.stdout)
raise RepresenterError(f'cannot represent an object: {data!s}')
ruamel.yaml.representer.RepresenterError: cannot represent an object: {

The 'unsafe' dumper will dump the objects but that data is not useful as yaml output.

Select the relevant components

Steps to reproduce

   vars = client.hosts.get(name=hostname).results[0].get_related('variable_data')
   yaml.dump(vars, sys.stdout)

Current results

ruamel.yaml.representer.RepresenterError: cannot represent an object: {

Sugested feature result

It would be nice if there was a way to indicate that yaml output was desired or a utility function that could convert the objects back to regular python data structures.

Additional information

No response

markfaine commented 2 weeks ago

FYI, I still think it's a good idea but I was able to work around it by converting the entire output to a string and feeding the string to yaml.dump.