You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Puppet Apply调用Deferred函数报错:未知函数mymodule::myupcase

Puppet Deferred函数测试报错:Unknown function 'mymodule::myupcase'

我按照Puppet官方文档测试Deferred函数,执行了以下步骤:

  1. 创建目录结构并运行PDK命令:
cd ~/dev/src/local/puppet_tests
pdk new module mymodule
cd mymodule
pdk new class mymodule
mkdir -p lib/puppet/functions/mymodule
  1. 在manifests/init.pp文件中写入代码:
class mymodule {
  $d = Deferred("mymodule::myupcase", ["mysecret"])

  notify { example :
    message => $d
  }
}

class { 'mymodule': }
  1. 在lib/puppet/functions/mymodule/目录下创建myupcase.rb文件并写入代码:
Puppet::Functions.create_function(:'mymodule::myupcase') do
  dispatch :up do
    param 'String', :some_string
  end

  def up(some_string)
    Puppet::Pops::Types::PSensitiveType::Sensitive.new(some_string.upcase)
  end
end
  1. 运行puppet apply manifests/init.pp命令时出现错误:
~/dev/src/local/puppet_tests/mymodule main*
❯ puppet apply manifests/init.pp
Notice: Compiled catalog for localhost.localdomain in environment production in 0.04 seconds
Error: Unknown function 'mymodule::myupcase'

移除init.pp中引用$d变量的代码块后可正常运行,但这并非解决问题的方法。

我的环境信息:

  • 操作系统:MacOS 13.4.1
  • Puppet版本:8.1.0
  • 通过brew安装puppet-agent和PDK:
brew install --cask puppetlabs/puppet/puppet-agent
brew install --cask puppetlabs/puppet/pdk

补充测试:改用pdk new function命令创建函数,错误仍然存在:

cd ~/dev/src/local/puppet_tests
pdk new module mymodule
cd mymodule
pdk new class mymodule
pdk new function myupcase --type=v4

该命令生成了spec目录及相关文件,最终的myupcase.rb代码如下:

# frozen_string_literal: true

# https://github.com/puppetlabs/puppet-specifications/blob/master/language/func-api.md#the-4x-api
Puppet::Functions.create_function(:"mymodule::myupcase") do
  dispatch :myupcase do
    param 'String', :a
    return_type 'String'
  end
  # the function below is called by puppet and and must match
  # the name of the puppet function above. You can set your
  # required parameters below and puppet will enforce these
  # so change x to suit your needs although only one parameter is required
  # as defined in the dispatch method.
  def myupcase(x)
    x.upcase
  end

  # you can define other helper methods in this code block as well
end

init.pp文件内容保持不变。请问我遗漏了什么?


内容的提问来源于stack exchange,提问作者Giuliani Sanches

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 16:42:45