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.05k stars 3.42k forks source link

UI Issue on inventory host vars #3167

Closed ahuffman closed 3 years ago

ahuffman commented 5 years ago
ISSUE TYPE
COMPONENT NAME
SUMMARY

A host's host_vars are displayed as a single-line of json instead of pretty yaml when viewing a host in an inventory.

ENVIRONMENT
STEPS TO REPRODUCE

Click on Inventories > [inventory name] > Hosts > [ hostname ] The view for host variables is toggled "in blue" to "YAML", however the variables are displayed in a single-line json format.

If I toggle to "JSON", "JSON" turns blue, and displays "pretty" json (i.e. indented and formatted). Finally, if I toggle to "YAML" again, "YAML" turns blue and displays "pretty" yaml (i.e. indented and formatted.)

EXPECTED RESULTS

Click on Inventories > [inventory name] > Hosts > [ hostname ] The host's host_vars should be displayed in pretty yaml.

ACTUAL RESULTS

The host's host_vars are displayed in a single line of json.

ADDITIONAL INFORMATION
mabashian commented 5 years ago

@ahuffman I'm not currently able to reproduce this issue in the upstream awx project:

host_vars_json

Does the above gif accurately reflect your reproduction steps? When I initially load the host form the variables are displayed in yaml as expected.

ahuffman commented 5 years ago

Yes @mabashian. Looks like it's not a problem upstream. Thanks for validating someone has corrected this already.

ahuffman commented 5 years ago

I was hoping I wouldn't have this issue still on Tower 3.5, but still seem to get it. I did some investigations by manually creating a new host in one of my inventories via the Tower UI. When I create host_vars in the UI in yaml format, with dictionaries, I see this in the host's variables key: "variables": "---\ndiscovered_facts:\n test: 1\n test2: \"3\"\n list:\n - \"1\"\n - \"2\"\n - \"3\"",.

When I take a look at the host's related.variable_data, the format seems to be in proper json format as such:

{
    "discovered_facts": {
        "test": 1,
        "test2": "3",`
        "list": [
            "1",
            "2",
            "3"
        ]
    }
}

The other host's where I have the display issue have been created by the tower_host ansible module, which utilizes the tower-cli, and have the variables added via the variables param of the module.

When I view these hosts via the Tower API, the host's variables key are in single-line json format VS. the host created via the UI where there are \n characters in the string. Once again, if I view the host's related.variable_data the variables are all in json format:

/api/v2/hosts/138/: "variables": "{'discovered_facts': {'kernel': '3.10.0-957.12.2.el7.x86_64', 'domain': 'huffnet.org', 'distribution_major_version': '7', 'swaptotal_mb': 1023, 'distribution_version': '7.6', ...

/api/v2/hosts/138/variable_data/:

{
    "discovered_facts": {
        "kernel": "3.10.0-957.12.2.el7.x86_64",
        "domain": "huffnet.org",
        "distribution_major_version": "7",
        "swaptotal_mb": 1023,
        "distribution_version": "7.6",
...

I guess I have 2 questions: 1.) Does the UI utilize the variables key or the related.variables_data key? 2.) What is the proper format for the variables key? I'm trying to determine if this is a tower-cli via tower_host module bug or what to try and rectify the display problem.

As I mentioned when I initially opened this issue, when I first look at the host's variables I see it in the single-line json format. If I click the json button I see it in "pretty" json format. If I then click the yaml button it then correctly displays the variables in "pretty" yaml format.

If you let me know what format the variables key should be in, I'm thinking I can hunt down the issue if it's coming from tower-cli, but without that knowledge I can't necessarily create a fix.

Thanks again, and appreciate the added insight into this one for me (in advance) :).

ahuffman commented 5 years ago

I was able to hunt down the proper format of the variables key. Which per the API docs state it should be in json or yaml format, but the docs do not state if we need to have this be single-line format or expanded with \n characters for "pretty/nice" format.

I'm experimenting with the tower_host module now to see if something like this will work and prevent the display issues:


- name: "Create host in tower inventory"
  tower_host:
    tower_username: "{{ tower_user }}"
    tower_password: "{{ tower_pass }}"
    tower_host: "{{ tower_url }}"
    tower_verify_ssl: "{{ tower_verify_ssl }}"
    name: "{{ new_hostname }}"
    inventory: "{{ target_inventory }}"
    enabled: True
    variables:
      discovered_facts: "{{ ansible_facts | to_nice_yaml }}"
    state: "present"
   delegate_to: "localhost"
   connection: "local"
   become: False
unlikelyzero commented 3 years ago

let's retest these examples