How to import local JSON file in a python renderer state file

427 views
Skip to first unread message

Raja Vairaashtak

unread,
Jul 27, 2017, 3:03:12 PM7/27/17
to Salt-users
HI,

I am writing a state using python renderer in which I want to use one of the JSON file located on salt-master, How can I load that external JSON file in a variable and use it for further logic in the state. I am looking something equivalent to below in Python renderer state file, for importing external file and available as variable.

{% import_json "defaults.json" as defaults %}

The json file is only on salt-master.

Thanks


Seth House

unread,
Jul 27, 2017, 3:44:19 PM7/27/17
to salt users list
The following combo should do the trick. (The `slsutil.renderer`
should really be updated to handle salt:// paths natively.)

local_path = __salt__.cp.cache_file('salt://defaults.json')
defaults = __salt__.slsutil.renderer(local_path, renderer='json')

https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.slsutil.html#salt.modules.slsutil.renderer
> --
> You received this message because you are subscribed to the Google Groups
> "Salt-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to salt-users+...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/salt-users/ce7f1ee9-3c2d-4325-8ee3-2f1b4a35af61%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Raja Vairaashtak

unread,
Jul 27, 2017, 6:21:21 PM7/27/17
to Salt-users, se...@eseth.com
Thanks this works, I was able to load values in the variable, but when seeing the value of the variable it is throwing error:

stderr:
                  /bin/sh: -c: line 0: syntax error near unexpected token `('
                  /bin/sh: -c: line 0: `echo OrderedDict([('1', OrderedDict([('foo', 'True'), ('bar', 42), ('baz', [1, 2, 3]), ('qux', 2.0)])), ('2', OrderedDict([('foo', 'False'), ('bar', 43), ('baz', [4, 5, 6]), ('qux', 2.0)]))])'

Below is my JSON file:

{
    "1": {
        "bar": 42,
        "baz": [
            1,
            2,
            3
        ],
        "foo": "True",
        "qux": 2.0
    },
    "2": {
        "bar": 43,
        "baz": [
            4,
            5,
            6
        ],
        "foo": "False",
        "qux": 2.0
    }
}
 
Python code:

#!py

def run():
     config = {}
     local_path = __salt__.cp.cache_file('salt://test.json?saltenv=master')
     defaults = __salt__.slsutil.renderer(local_path, renderer='json')
     config['print json'] = {
        'cmd': [
            'run', {'name': 'echo {0}'.format(defaults) },
        ]
     }
     return config


Not sure what is wrong here? Is any other parameter need to be passed with renderer to handle this 

Seth House

unread,
Jul 27, 2017, 7:18:51 PM7/27/17
to Raja Vairaashtak, Salt-users
Salt's renderer system will load the dictionary from your JSON file
into a Python ordered dictionary. Since you're testing via `cmd.run`
that data structure will get cast to a string representation and the
shell is choking on that representation.

The shell will be happy for this particular test if you wrap it in quotes:

'echo "{0}"'.format(defaults)

Now that it's working you can reference the individual values:

'echo {0}'.format(defaults['1']['bar']) }

If you'd prefer to avoid the Pythonisms of ordered dictionaries you
can just turn it into a regular dictionary:

defaults = dict(__salt__.slsutil.renderer(local_path, renderer='json'))

On Thu, Jul 27, 2017 at 4:21 PM, Raja Vairaashtak

Raja Vairaashtak

unread,
Jul 28, 2017, 2:06:35 AM7/28/17
to Salt-users, raja.vai...@gmail.com, se...@eseth.com
many thanks, this works like charm.
Reply all
Reply to author
Forward
0 new messages