Ami*_*med 5 ruby chef-infra chef-recipe lwrp
我正在尝试创建一个LWRP,它将调用自身内部定义的资源.我的食谱结构如下:

在机器食谱的提供者,我有一个代码片段如下:
require 'chef/provisioning' # driver for creating machines
require '::File'
def get_environment_json
@@environment_template = JSON.parse(File::read(new_resource.template_path + "environment.json"))
return @@environment_template
end
Run Code Online (Sandbox Code Playgroud)
代码只是尝试读取json文件,我正在使用File :: read.
我一直收到如下错误:
无法加载此类文件 - :: File
有谁知道如何在我的LWRP提供商内部使用File :: read?
好的,所以前两个答案都是对的.你有两个问题.首先,你不能要求,::File因为它已经是Ruby的一部分.这是导致错误的原因.
其次,如果你打电话,File.read你会抓住Chef's File而不是ruby's.你需要做一个::File.read使用Ruby的File类.
这是因为 Chef 已经拥有文件资源。我们必须在配方中使用 Ruby File 类。我们使用 ::File 来使用 Ruby File 类来解决此问题。例如:
execute 'apt-get-update' do
command 'apt-get update'
ignore_failure true
only_if { apt_installed? }
not_if { ::File.exist?('/var/lib/apt/periodic/update-success-stamp') }
end
Run Code Online (Sandbox Code Playgroud)
来源: https: //docs.chef.io/ruby.html#ruby-class
| 归档时间: |
|
| 查看次数: |
1692 次 |
| 最近记录: |