ansible / tower-cli

THIS TOOL IS NO LONGER UNDER ACTIVE DEVELOPMENT. This tool is being phased out in favor of the new official AWX CLI
https://github.com/ansible/awx/tree/devel/awxkit/awxkit/cli/docs
Apache License 2.0
363 stars 151 forks source link

Inventory variables should appear in JSON format if --format json requested #331

Open mikeycmccarthy opened 7 years ago

mikeycmccarthy commented 7 years ago

Stumbled across this one while looking into a support issue via RedHat support (01894363).

Steps to reproduce:

  1. Create an inventory, and add some variables in YAML format
  2. Query the inventory via tower-cli and observe the structure of the variables field
  3. Go back to the inventory via the UI and hit the json toggle and then save
  4. Perform step 2 again and notice the change of format of the variables field

Expected behaviour I expected that the toggle boxes were just changing a visual representation of the same underlying data. It seems that it is more than that though. Asking for --format json sounds to me like even the inventory variables should be as json.

I'm writing a playbook that can upsert specific variables in an inventory. To do this I need to read the inventory variables in as a variable, with code like this:

- hosts: localhost
  tasks:

   - name: Get inventory
     command: tower-cli inventory get 3 --format json
     register: inventory

   - name: Access inventory variable variables
     debug:
       var: ((inventory.stdout|from_json).variables|from_json).fooagain

It seems the second from_json will work if the variables are in a json form, otherwise I guess I need from_yaml (I'll test now).

It'd be nice to not need to know how the variables are saved - maybe just my opinion though!

AlanCoding commented 7 years ago

This isn't great, but unfortunately, it's something that I think should be considered on the Tower side as opposed to the tower-cli side.

One thing we can do is hit the endpoint /api/v1/inventories/N/variable_data/ instead. This will parse the data and return it as valid JSON data. You can also get YAML by passing ?format=yaml when visiting that endpoint, although converting from one to the other is so trivial that I doubt this would be useful.

I have considered that we could introduce a new command like tower-cli inventory variables which would return the variables, but it's a very narrow use case.

mikeycmccarthy commented 7 years ago

Not sure if this is also related, but the extra-vars of a job template if empty are also a blank string in the tower-cli output with --format json, and though the UI shows them as an empty JSON block (i.e. {}) in the json view. This makes it slightly more difficult to parse the extra vars of a template as json without needing to check first whether it is a blank string.

gtirloni commented 5 years ago

This would be very nice. Right now I'm running tower-cli host list -a -f json but in variables I get a string with YAML which is quite impossible to parse with tools like jq.