Using the salt data module in a salt runner

Salt runners don’t start with the full complement of salt modules loaded at start, but it is possible to load up some (many? all?) of the modules if they are needed.

I wanted to use the data module to store the result of a runner execution to reduce duplicate processing, but just adding an import salt.modules.data did not work, when the data.update() call was made, it threw an exception (something about __opts__). It took a bit of time and experimenting, but I found that I could load the module and call it like so:

    import salt.client
    import salt.config
    __opts__ = salt.config.minion_config('/etc/salt/minion')
    datamod = salt.loader.raw_mod(__opts__, 'data', None)
    datamod['data.update']('keyname', 'value')

Hopefully this will help someone else, as when I looked in Google for answers (late August 2015) it was difficult to find anything about using modules in salt runner scripts.

Tuesday September 1, 2015   ·   Permalink