function not loaded when .pp file runs through command line >> Error: Unknown function:

674 views
Skip to first unread message

bodik

unread,
Nov 8, 2016, 8:55:02 AM11/8/16
to Puppet Users
hello,

I'm getting into troubles while using rake (not important) for --noop
tests during modules development. Using

puppet apply -e 'include class'

everything works fine, but using .pp file containing exactly the same

puppet apply tests/class.pp

results in "Unknown function:" as shown below, where whole usecase is
documented.

Acording to strace myfunc.rb is found by lstat and fopen in both cases,
but in the latter (error one), myfunc.rb is searched also in gems
directories ...

any advice would be appreciated


thanks
bodik


===============================================
===============================================



root@tester:~/pb1# find .
./mod1
./mod1/manifests
./mod1/manifests/init.pp
./mod1/lib
./mod1/lib/facter
./mod1/lib/facter/myfunc.rb
./mod1/tests
./mod1/tests/init.pp
./run.sh



root@tester:~/pb1# cat ./mod1/manifests/init.pp
class mod1::puppet_function_resolution() {
notice(myfunc(1))
}


root@tester:~/pb1# cat ./mod1/lib/facter/myfunc.rb
# simple wrapper for custom execs
require "puppet"
module Puppet::Parser::Functions
newfunction(:myfunc, :type => :rvalue) do |args|
return "returned value"
end
end




root@tester:~/pb1# cat ./mod1/tests/init.pp
include mod1::puppet_function_resolution


root@tester:~/pb1# cat ./run.sh
#!/bin/sh
set -x

puppet apply --modulepath=/root/pb1 -e 'include
mod1::puppet_function_resolution' --noop

puppet apply --modulepath=/root/pb1 "mod1/tests/init.pp" --noop




root@tester:~/pb1# sh ./run.sh

+ puppet apply --modulepath=/root/pb1 -e include
mod1::puppet_function_resolution --noop
Notice: Scope(Class[Mod1::Puppet_function_resolution]): returned value
Notice: Compiled catalog for tester in environment production in 0.04
seconds
Notice: Applied catalog in 0.63 seconds


+ puppet apply --modulepath=/root/pb1 mod1/tests/init.pp --noop
Error: Evaluation Error: Unknown function: 'myfunc'. at
/root/pb1/mod1/manifests/init.pp:2:9 on node tester




root@tester:~/pb1# puppet --version
4.5.2

Peter Huene

unread,
Nov 8, 2016, 7:08:27 PM11/8/16
to puppet...@googlegroups.com
Hi bodik,

This is not the correct place for Puppet 3.x custom functions (this location is for custom facts).

The correct location would be mod1/lib/puppet/parser/functions/myfunc.rb.

This is probably why the compiler can't find it.
Given that you're using Puppet 4.x, I recommend using the Puppet 4.x function API rather than the 3.x API.

As an example for your custom function, in mod1/lib/puppet/functions/myfunc.rb you would have:

```
Puppet::Functions.create_function(:'mod1::myfunc') do
  dispatch :myfunc do
    param :Integer, :arg
  end

  def myfunc(arg)
     "returning value #{arg}"
  end
end
```

And this would be invoked like `mod1::myfunc(1)`.

Cheers,

Peter
 

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/5821A30B.1040104%40cesnet.cz.
For more options, visit https://groups.google.com/d/optout.



--
--
Peter Huene - Senior Software Engineer

peter...@puppet.com | @peterhuene

bodik

unread,
Nov 9, 2016, 2:55:06 AM11/9/16
to puppet...@googlegroups.com


On 11/09/2016 01:08 AM, Peter Huene wrote:
> Hi bodik,
>
> On Tue, Nov 8, 2016 at 2:03 AM, bodik <bo...@cesnet.cz
> everything works fine, but using .pp file containing exactly the same
>
> puppet apply tests/class.pp
>
> results in "Unknown function:" as shown below, where whole usecase is
> documented.

> Given that you're using Puppet 4.x, I recommend using the Puppet 4.x
> function API rather than the 3.x API.
>
> As an example for your custom function, in
> mod1/lib/puppet/functions/myfunc.rb you would have:
>
> ```
> Puppet::Functions.create_function(:'mod1::myfunc') do
> dispatch :myfunc do
> param :Integer, :arg
> end
>
> def myfunc(arg)
> "returning value #{arg}"
> end
> end
> ```
>
> And this would be invoked like `mod1::myfunc(1)`.
>
> Cheers,
>
> Peter


Thank you very much, work like a charm.

bodik
Reply all
Reply to author
Forward
0 new messages