Puppet custom function for arithmetic operation

284 views
Skip to first unread message

Ashish Jaiswal

unread,
Nov 7, 2014, 6:15:23 AM11/7/14
to puppet...@googlegroups.com
Hello,

Just trying to write a custom function. Basically looking to pass an
argument from manifest and value should get calculated.

module Puppet::Parser::Functions
newfunction(:shmmax, :type => :rvalue) do |args|
def shmmax(args)
product = args * 1024 * 1024 * 1024
return product
end
end
end


@ foo.pp
$shmmax_variable = shmmax(2)
sysctl { "kernel.shmmax" : val => $shmmax_variable }

In this case I'm expecting a value of 2147483648 for kernel.shmmax in
sysctl.conf file

Its just fails, Any pointer what is wrong here ?

Regards,
Ashish Jaiswal.

jcbollinger

unread,
Nov 7, 2014, 9:11:15 AM11/7/14
to puppet...@googlegroups.com
Your custom function does not have the necessary form.  Most significantly, the block passed to newfunction() should perform the work of the function directly, not define a Ruby method to perform that work.  For rvalue functions, the value obtained by executing the block is the return value of the function.  Secondarily, the function arguments are presented to the function body as an array.  For example,

module Puppet::Parser::Functions
    newfunction
(:shmmax, :type => :rvalue) do |args|

        args
[0] * 1024 * 1024 * 1024
   
end
end

Note that if you want to perform any argument validation then that's on you.

Note also that the function must appear in a Ruby file with the same base name as the function itself, and that although you should put your function in a module, you need to sync it into the master's lib directory before the master will use it.  Furthermore, be aware that if you change the function implementation then you must both sync the new function implementation into the master and restart the master, in that order, before your changes are effective.


John

Ashish Jaiswal

unread,
Nov 8, 2014, 12:01:54 AM11/8/14
to puppet...@googlegroups.com, puppet...@googlegroups.com
Hi John,

Thanks for the pointer, for workaround I used the inline_template function to work. Thanks for the piece of code. I will try this once I reach office, that would be on Monday.
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/aa7cab69-49cd-42e3-8fd1-0f374b908432%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Felix Frank

unread,
Nov 14, 2014, 1:38:33 PM11/14/14
to puppet...@googlegroups.com
On 11/08/2014 06:01 AM, Ashish Jaiswal wrote:
> Hi John,
>
> Thanks for the pointer, for workaround I used the inline_template
> function to work. Thanks for the piece of code. I will try this once I
> reach office, that would be on Monday.

Yeah, that's pretty expensive in case you're low on CPU.

Has that worked out for you?

Ashish

unread,
Nov 16, 2014, 6:20:08 AM11/16/14
to puppet...@googlegroups.com
Hi Felix,

It was consuming lot of CPU and causing the host machine to get hanged.. I used the inline_template for the moment.

Any work around would be nice ??

I don’t know why it is causing hurt to cpu, cause its just a simple math out there. Let me know ??
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/54664C12.5050807%40alumni.tu-berlin.de.

Spencer Krum

unread,
Nov 16, 2014, 6:31:58 AM11/16/14
to puppet...@googlegroups.com
Puppet can actually do math inline:


$shmmax = 2 * 1024 * 1024 * 1024
notify { $shmmax: }

Maybe this is the easiest way to solve your problem?

Thanks,
Spencer


For more options, visit https://groups.google.com/d/optout.



--
Spencer Krum
(619)-980-7820
Reply all
Reply to author
Forward
0 new messages