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

[FEATURE REQUEST] JC renderer to convert command output to objects #58355

Open
kellyjonbrazil opened this issue Sep 1, 2020 · 1 comment
Open
Labels
Feature new functionality including changes to functionality and code refactors, etc.
Milestone

Comments

@kellyjonbrazil
Copy link

kellyjonbrazil commented Sep 1, 2020

I recently created an Ansible filter plugin for JC (https://github.com/kellyjonbrazil/jc - I am the author) and it was requested I create something similar for Salt. I believe this would be a renderer serializer (deserializer, to be specific), and an outputter in Salt, though I'm open to other options.

JC is a command-line utility and python library that converts the output of dozens of commands and file-types into a python dict or list of dicts. The JC CLI converts the command output to JSON. Here is a blog post that demonstrates how it can be used in Ansible: https://blog.kellybrazil.com/2020/08/30/parsing-command-output-in-ansible-with-jc

I'm still getting my Salt dev environment set up, but I noticed in the Contributing docs it recommended opening an issue before issuing a PR.

I have created a first draft of the renderer here:
https://github.com/kellyjonbrazil/salt/blob/jc-renderer/salt/renderers/jc.py

I have created a first draft of the serializer and outputter here:
https://github.com/kellyjonbrazil/salt/blob/jc-renderer/salt/serializers/jc.py
https://github.com/kellyjonbrazil/salt/blob/jc-renderer/salt/output/jc.py

And here is a first draft of the unit tests:
https://github.com/kellyjonbrazil/salt/blob/jc-renderer/tests/unit/renderers/test_jc.py

I would just like to make sure a Renderer is the proper approach and if there are any other pieces missing, like documentation/Changelog before I submit a PR.

The outputter works, but requires a parser parameter passed to it. I didn't know how to get a parameter into the outputter, so I'm using an environment variable:

$ JC_PARSER=uptime salt '*' cmd.run 'uptime' --out=jc

and here's an example of the serializer in an sls file:

{% set date_out = salt.cmd.run_stdout('date') %}
{% set date_jc = salt.slsutil.deserialize('jc', date_out, parser='date') %}

test:
    cmd.run:
        - name: >
            echo 'The timezone is {{ date_jc.timezone }}'

The only issue with this setup is that jc needs to be installed on the minions for this to work. I was hoping to have the deserialization happen on the master. If anyone has any suggestions on that I'm open.

Thanks,
Kelly

@kellyjonbrazil kellyjonbrazil added the Feature new functionality including changes to functionality and code refactors, etc. label Sep 1, 2020
@sagetherage sagetherage added this to the Approved milestone Sep 23, 2020
@kellyjonbrazil
Copy link
Author

kellyjonbrazil commented Feb 10, 2022

Note, there is a new high-level API for calling jc parsers that can simplify the implementation a bit:

https://github.com/kellyjonbrazil/jc/tree/master/docs

Also, there are now a handful of 'streaming' parsers that return generators that can be iterated over. Not sure if there is a use case for those. In any case, for every streaming parser there is a corresponding 'standard' parser, so streaming parsers can be filtered out if they are not useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature new functionality including changes to functionality and code refactors, etc.
Projects
None yet
Development

No branches or pull requests

3 participants