Making a result of custom function available to multiple classes

47 views
Skip to first unread message

JeremyCampbell

unread,
Sep 8, 2012, 11:59:31 AM9/8/12
to puppet...@googlegroups.com
Hey,

I'm using Puppet 2.7.19 with hiera 0.3.0. I have an Openvpn module which has a custom function that queries a mysql db and returns a hash. I also need the hash values in other modules e.g. firewall module. Since the custom function is in the openvpn/lib/puppet/parser/functions folder I believe it is autoloaded and is available from other modules. However, I don't want to have to call the custom function multiple times as it's an expensive mysql query. What would be the best way to call the custom function once and make the returned hash available to other modules?

Thanks for your time!

Cheers,
J.

Krzysztof Wilczynski

unread,
Sep 8, 2012, 6:31:06 PM9/8/12
to puppet...@googlegroups.com
Hi,

Since functions run on the Puppet Master side, the simplest solution is to cache the value if its "freshness" is not mandatory i.e. does not change, lets say every 5 minutes? Or, even it will be fine every 24 hours, etc ...

Consider this snippet: https://gist.github.com/3680388

Let me know if you have troubles adapting this for your function, I am always keen to help :)

KW

jcbollinger

unread,
Sep 10, 2012, 9:47:35 AM9/10/12
to puppet...@googlegroups.com


On Saturday, September 8, 2012 5:31:06 PM UTC-5, Krzysztof Wilczynski wrote:
Hi,

Since functions run on the Puppet Master side, the simplest solution is to cache the value if its "freshness" is not mandatory i.e. does not change, lets say every 5 minutes? Or, even it will be fine every 24 hours, etc ...

Consider this snippet: https://gist.github.com/3680388

Let me know if you have troubles adapting this for your function, I am always keen to help :)


And one form of caching the result would be to assign it to a class (or global) variable.  A class variable make sense to me, as you should have exactly one class responsible for actually running the function and assigning the result anyway.  That's about as simple as you can do, and it would get you one function call per catalog compilation.

Caching the result outside of Puppet would allow you to reduce the number of function calls further.


John

Reply all
Reply to author
Forward
0 new messages