Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json output would be nice #18

Open
philregier opened this issue Jul 25, 2018 · 1 comment
Open

json output would be nice #18

philregier opened this issue Jul 25, 2018 · 1 comment

Comments

@philregier
Copy link

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant