在puppet中添加自定义函数

Alv*_*ado 2 ruby file-io function puppet

我正试图在puppet中移植一个Ruby脚本.据我所知,实现这一目标的唯一方法是在模块中创建自定义函数.如果有另一种方式,请随时告诉我.我尝试了我的第一个测试,如https://docs.puppetlabs.com/guides/custom_functions.html所示,我宣布了一个新模块:

/etc/puppet/modules/custom_module
Run Code Online (Sandbox Code Playgroud)

并编辑了一个/etc/puppet/modules/custom_module/lib/puppet/parser/functions/newfunction使用以下代码调用的新函数文件:

module Puppet::Parser::Functions
    newfunction(:write_line_to_file) do |args|
            filename = args[0]
            str = args[1]
            File.open(filename, 'a') {|fd| fd.puts str }
    end
end
Run Code Online (Sandbox Code Playgroud)

然后我在这个清单中/etc/puppet/environments/desarrollo/manifests/des.pp使用了这个内容:

node "develserver" {
        write_line_to_file('/tmp/some_file', "Hello world!")
}
Run Code Online (Sandbox Code Playgroud)

最后,当我运行puppet agent -tod它时,它显示以下错误:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Unknown function write_line_to_file at /etc/puppet/environments/desarrollo/manifests/des.pp:5 on node develserver
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Fel*_*ank 6

直接问题可能是.rb您的函数文件名缺少扩展名.

请记住,Ruby代码在目录编译期间在主服务器上运行.如果您希望Puppet在代理端执行操作,则无法使用自定义函数.您必须编写自定义资源类型(通常是提供程序)才能执行此操作.