Chef 库辅助方法未定义
Chef library helper method is undefined
我正在尝试编写一个 LWRP,它会根据条件调用辅助库中的方法。我在让提供者读取外部方法时遇到语法问题。
提供商非常简单
# providers/provider.rb
require_relative '../libraries/acx_shared'
include Acx
action :create do
Chef::Log.debug('{jefflibrary->lwrp->user} - start')
if @new_resource.shared == true
Acx::User::Shared.true_shared()
else
Acx::User::Shared.false_shared()
end
if @new_resource.sudo == true
Chef::Log.error('I HAVE THE POWER')
else
Chef::Log.error('my power is weak and feeble')
end
if @new_resource.password == true
Chef::Log.error('the secret password is 12345')
else
Chef::Log.error('I will never tell you the secret to the airlock')
end
Chef::Log.debug('{jefflibrary->lwrp->user} - end')
end
连同辅助库
#libraries/acx_shared.rb
module Acx
module User
module Shared
def true_shared
#puts blah
Chef::Log.error('I am pulling this from a library reference')
end
def false_shared
Chef::Log.error('I am not very good at sharing')
end
end
end
end
但是每当我尝试 运行 它时,无论资源属性设置如何,我都会得到
NoMethodError
-------------
undefined method `false_shared' for Acx::User::Shared:Module
我在编写帮助程序库的文档中显然遗漏了一些内容,但我不确定是什么。尝试移动一些东西,但开始 运行 失去想法。
尝试删除 include Acx
可能会发生的情况是,因为您正在这样做,所以它实际上是在 在 Acx 内部查找另一个模块名称 Acx。
因此,要么从对 *shared 方法的调用中删除 include 或删除 Acx:: 。
您也不能在尝试时直接从模块调用实例变量。您需要 class 包含模块,然后在 class.
的对象上调用方法
作为替代方案,您可以将这些方法提升为 class 方法(自己),然后您可以直接调用它们。
类似于:
# providers/provider.rb
require_relative '../libraries/acx_shared'
action :create do
Chef::Log.debug('{jefflibrary->lwrp->user} - start')
acx = Class.new.exted(Acx::User::Shared)
if @new_resource.shared == true
acx.true_shared()
else
acx.false_shared()
end
if @new_resource.sudo == true
Chef::Log.error('I HAVE THE POWER')
else
Chef::Log.error('my power is weak and feeble')
end
if @new_resource.password == true
Chef::Log.error('the secret password is 12345')
else
Chef::Log.error('I will never tell you the secret to the airlock')
end
Chef::Log.debug('{jefflibrary->lwrp->user} - end')
end
或
#libraries/acx_shared.rb
module Acx
module User
module Shared
def self.true_shared
#puts blah
Chef::Log.error('I am pulling this from a library reference')
end
def self.false_shared
Chef::Log.error('I am not very good at sharing')
end
end
end
end
我正在尝试编写一个 LWRP,它会根据条件调用辅助库中的方法。我在让提供者读取外部方法时遇到语法问题。
提供商非常简单
# providers/provider.rb require_relative '../libraries/acx_shared' include Acx action :create do Chef::Log.debug('{jefflibrary->lwrp->user} - start') if @new_resource.shared == true Acx::User::Shared.true_shared() else Acx::User::Shared.false_shared() end if @new_resource.sudo == true Chef::Log.error('I HAVE THE POWER') else Chef::Log.error('my power is weak and feeble') end if @new_resource.password == true Chef::Log.error('the secret password is 12345') else Chef::Log.error('I will never tell you the secret to the airlock') end Chef::Log.debug('{jefflibrary->lwrp->user} - end') end
连同辅助库
#libraries/acx_shared.rb module Acx module User module Shared def true_shared #puts blah Chef::Log.error('I am pulling this from a library reference') end def false_shared Chef::Log.error('I am not very good at sharing') end end end end
但是每当我尝试 运行 它时,无论资源属性设置如何,我都会得到
NoMethodError ------------- undefined method `false_shared' for Acx::User::Shared:Module
我在编写帮助程序库的文档中显然遗漏了一些内容,但我不确定是什么。尝试移动一些东西,但开始 运行 失去想法。
尝试删除 include Acx
可能会发生的情况是,因为您正在这样做,所以它实际上是在 在 Acx 内部查找另一个模块名称 Acx。
因此,要么从对 *shared 方法的调用中删除 include 或删除 Acx:: 。
您也不能在尝试时直接从模块调用实例变量。您需要 class 包含模块,然后在 class.
的对象上调用方法
作为替代方案,您可以将这些方法提升为 class 方法(自己),然后您可以直接调用它们。
类似于:
# providers/provider.rb
require_relative '../libraries/acx_shared'
action :create do
Chef::Log.debug('{jefflibrary->lwrp->user} - start')
acx = Class.new.exted(Acx::User::Shared)
if @new_resource.shared == true
acx.true_shared()
else
acx.false_shared()
end
if @new_resource.sudo == true
Chef::Log.error('I HAVE THE POWER')
else
Chef::Log.error('my power is weak and feeble')
end
if @new_resource.password == true
Chef::Log.error('the secret password is 12345')
else
Chef::Log.error('I will never tell you the secret to the airlock')
end
Chef::Log.debug('{jefflibrary->lwrp->user} - end')
end
或
#libraries/acx_shared.rb
module Acx
module User
module Shared
def self.true_shared
#puts blah
Chef::Log.error('I am pulling this from a library reference')
end
def self.false_shared
Chef::Log.error('I am not very good at sharing')
end
end
end
end