LLNL / py-hostlist

hostlist utility implemented in python
MIT License
6 stars 2 forks source link

json output would be nice #18

Open philregier opened 6 years ago

philregier commented 6 years ago

Following discussions of #17 and #12 I ran into a use case which I think would justify json output.

Turns out ansible supports dynamic inventories determined by invoking scripts, but needs the output of such a script to be in json format:

# ansible -i /etc/ansible/hosts.d/cumulus.sh --list-hosts all
[WARNING]: * Failed to parse /etc/ansible/hosts.d/cumulus.sh with script plugin: failed to parse executable inventory script results from /etc/ansible/hosts.d/cumulus.sh: needs to be a json dict

If I manually wrap a nodeattr invocation in json, I get what I want:

# cat /etc/ansible/hosts.d/cumulus.sh
#!/bin/bash
echo '{ "cumulus":["'$(nodeattr -f /etc/genders.d/cumulus.g -c cumulus|sed 's/,/","/g')'"]}'
# ansible -i /etc/ansible/hosts.d/cumulus.sh --list-hosts all
  hosts (6):
    mgmtsw1
    mgmtsw2
    mgmtsw3
    mgmtsw4
    mgmtsw5
    mgmtsw6

This would be a lot cleaner if I could use hostlist to postprocess:

#!/bin/bash
nodeattr -f /etc/genders.d/cumulus.g -c cumulus | hostlist -j

Such a -j option could either add a dummy name (like "hostlist") to the json, e.g.,

{ "hostlist": ["host1","host2","host3"] }

...or could possibly take a label as an argument: -j <listname>.

This isn't an urgent priority, but I thought it was a pretty clean example of how other hostname/genders utilities could be retrofitted with a single CLI switch to interact with newer frameworks.

philregier commented 6 years ago

The more I think about it, the less I like the label argument; I think that could make lua-hostlist compatibility nightmarishly difficult. Still something to think about though.