Saltstack: use a Python module in both states and pillar

configuration-managementsaltstack

I have a custom module with a function that i can use in my states like this

{% set myvar = salt['mymodule.myfunction']() %}

Now I want/need to use it in my pillar. I have tried calling it the same way but I got an error

Jinja variable 'salt.loader.LazyLoader object' has no attribute 'mymodule.myfunction'

Is there any way to declare this module and function as valid in pillar?

Thanks

edit

The actual function is a simple regex to convert the minion ID to a short form of it. I finally implemented the actual functionality with Jinja in both states and pillar, and it works.
I want to use it in states and pillar to be able to load a .sls file if it exists to override default setting with per-minion ones.

So my problem is actually solved but the question behind: how to have a (execution) module that can be loaded in states AND pillar, is still relevant i think. I may rename my question.

Best Answer

The pillar compilation happens on the master, not the minion. This means that modules only available in the salt://... tree (e.g. in salt://_modules) cannot be used inside pillar files.

In order to use the module in a pillar file, it must also be made available to the master. In the default configuration, this should be possible by copying the module to /var/cache/salt/master/extmods/modules. In order to avoid having to maintain the modules in two locations, one might simply add a symbol link:

ln -s /srv/salt/_modules /var/cache/salt/master/extmods/modules

Depending on the values of the extension_modules and module_dirs options in /etc/salt/master, the actual path that has to be used might be different.

Related Topic