"Dynamic" Pillars (calling arbitrary Python functions in Pillars)

319 views
Skip to first unread message

Vitaly Isaev

unread,
Jun 23, 2016, 5:58:09 AM6/23/16
to Salt-users
Hello! I'm pretty new to Salt, and possibly I misunderstand some concepts... Recently I faced with a need of "dynamic" Pillar: some keys should be initialized with values at the Pillar's compile time (i.e. should not be stored statically). Jinja macro are not sufficient here, I would like to recruit Python for this purpose. Please consider two simple cases below. Is it possible to implement them in SaltStack?

  1. # Say I have a Python function 'token_get' that encapsulates some heavy logic
  2. # and obtains token string required for the particular Pillar file.
  3. # Is it possible to call this function from Jinja template (Variant 1),
  4. # or should I wrap this function into a distinct binary ('token_get_binary')
  5. # and call it like in Variant 2?
  6.  
  7. # Variant 1 (More preferred)
  8. config:
  9.     hostabc.def.com
  10.     port8081
  11.     token{{ token_get(arg1, arg2) }}
  12.  
  13.  
  14. # Variant 2 (Less preffered)
  15. config:
  16.     hostabc.def.com
  17.     port8081
  18.     {% set token_get_cmd = ["token_get_binary", arg1, arg2] | join " " %}
  19.     token{{ salt['cmd.run'](token_get_cmd) }}


Michael Ward

unread,
Jun 23, 2016, 11:06:45 AM6/23/16
to Salt-users
There may be more than one way to do this, but I just tested using the "py" renderer, and it allowed me to write a pillar in "pure" Python, including the ability to call upon other packages and Python functions.

vim /srv/pillar/test/mytest1.sls:

#!py

import socket

def run():
    mydict
= {}
    mydict
['myhost'] = socket.gethostname() + "lolol"
   
return mydict

After adding this to my pillar top.sls file, I was able to call a host and get this pillar data:



[root@salt-master ~]# salt 'box603.example.edu' pillar.item myhost
box603
.example.edu:
   
----------
    myhost
:
        salt
-masterlolol


Hopefully this is of use to you?  One thing to consider is that as you can see above, the call to get the hostname happened *on* the master, so myhost evaluated to the salt-master, not the calling minion.

Mike

Florian Ermisch

unread,
Jun 23, 2016, 11:06:59 AM6/23/16
to salt-...@googlegroups.com
Hi Vitaly,

you can just have a little command
returning/outputting JSON [1] or YAML
as a source for an external pillar. That's
probably the easiest way to get started
with dynamic pillars.

Regards, Florian

[1]: https://docs.saltstack.com/en/latest/topics/development/external_pillars.html#example-configuration

Am 23. Juni 2016 11:58:09 MESZ, schrieb Vitaly Isaev <vitaly...@gmail.com>:
> Hello! I'm pretty new to Salt, and possibly I misunderstand some
> concepts... Recently I faced with a need of "dynamic" Pillar: some
> keys
> should be initialized with values at the Pillar's compile time (i.e.
> should
> not be stored statically). Jinja macro are not sufficient here, I
> would
> like to recruit Python for this purpose. Please consider two simple
> cases
> below. Is it possible to implement them in SaltStack?
>
>
> 1. # Say I have a Python function 'token_get' that encapsulates some
> heavy logic
> 2. # and obtains token string required for the particular Pillar file.
> 3. # Is it possible to call this function from Jinja template (Variant
>
> 1),
> 4. # or should I wrap this function into a distinct binary
> ('token_get_binary')
> 5. # and call it like in Variant 2?
> 6.
> 7. # Variant 1 (More preferred)
> 8. config:
> 9. host: abc.def.com
> 10. port: 8081
> 11. token: {{ token_get(arg1, arg2) }}
> 12.
> 13.
> 14. # Variant 2 (Less preffered)
> 15. config:
> 16. host: abc.def.com
> 17. port: 8081
> 18. {% set token_get_cmd = ["token_get_binary", arg1, arg2] | join
> "
> " %}
> 19. token: {{ salt['cmd.run'](token_get_cmd) }}
>

Vitaly Isaev

unread,
Jun 23, 2016, 2:02:20 PM6/23/16
to Salt-users
Hi Michael, thanks for these snippets. I've heard about "py" renderer, but I thought that the one can only be used in States, but not in Pillars nor Grains. I will try to use it. 

The real problem is "passing the arguments" into a "py"-rendered Pillar. I wanted to keep it abstract like Jinja macro, but now I'll have to use workarounds.

четверг, 23 июня 2016 г., 18:06:45 UTC+3 пользователь Michael Ward написал:

Vitaly Isaev

unread,
Jun 23, 2016, 2:07:38 PM6/23/16
to Salt-users, florian...@alumni.tu-berlin.de
Thank you Florian, good point.

четверг, 23 июня 2016 г., 18:06:59 UTC+3 пользователь Florian Ermisch написал:
Reply all
Reply to author
Forward
0 new messages