将变量从厨师重新解释到同一厨师食谱中的其他资源
我是厨师和Ruby的新手,我非常困惑地如何处理厨师中的变量,并且我试图将变量[var]从ruby_block资源传递到http_request资源,并将[var]值分配给<<代码>“ value”:“#{var}” 在厨师食谱[customer.rb]中,如下所示,我有一个错误。
[var.js]是一个JSON文件,包含100轮代码。我想将此文件的内容传递到变量[var],并调用此变量以更新网站的某些配置[http:// localhost:6000/master/master/endpoint_1_in/set]
directory 'work' do
owner 'root'
group 'root'
mode '0775'
recursive true
action :create
end
bash 'Extract customer Package' do
cwd '/work'
code <<-EOH
curl -L --header "PRIVATE-TOKEN: XXXXXXXXXXXXXX" "https://www.company.sm/api/v4/projects/44/packages/generic/customer/1.0.0-customer/customer.tar.gz" > /work/customer.tar.gz
tar xzf customer.tar.gz
EOH
end
ruby_block 'Read customer file into Var variable' do
block do
if File.exists?("/work/customer/Var.js")
Var = IO.read("/work/customer/Var.js")
end
end
end
http_request 'cusromer update' do
action :put
url 'http://localhost:6000/Master/endpoint_1_in/set'
message ({
"moduleId" => "endpoint_1_in",
"moduleType" => "ENDPOINT_1_IN",
"options" => [
{
"key": "somename.text",
"value":"#{Var}"
}
],
}.to_json)
headers({'Authorization' => "Basic #{basicauth}", 'Content-Type' => 'application/json;charset=utf-8' })
retries 10
retry_delay 30
end
,我遇到的错误是
Installing cookbook gem dependencies:
Compiling cookbooks...
Loading Chef InSpec profile files:
Loading Chef InSpec input files:
Loading Chef InSpec waiver files:
================================================================================
Recipe Compile Error in /tmp/kitchen/cache/cookbooks/clinic/recipes/customer.rb
================================================================================
NameError
---------
uninitialized constant #<Class:#<Chef::Recipe:0x0000000002bac870>>::Var
Cookbook Trace: (most recent call first)
----------------------------------------
Relevant File Content:
----------------------
/tmp/kitchen/cache/cookbooks/clinic/recipes/customer.rb:
64: url 'http://localhost:6000/Master/endpoint_1_in/set'
65: message ({
66: "moduleId" => "endpoint_1_in",
67: "moduleType" => "ENDPOINT_1_IN",
68: "options" => [
69: {
70: "key": "somename.text",
71>> "value":"#{Var}"
72: }
73: ],
74:
75: }.to_json)
System Info:
------------
chef_version=17.10.3
platform=ubuntu
platform_version=20.04
ruby=ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]
program_name=/opt/chef/bin/chef-client
executable=/opt/chef/bin/chef-client
Running handlers:
[2022-05-13T16:25:57+00:00] ERROR: Running exception handlers
Running handlers complete
[2022-05-13T16:25:57+00:00] ERROR: Exception handlers complete
Infra Phase failed. 0 resources updated in 02 seconds
[2022-05-13T16:25:57+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
[2022-05-13T16:25:57+00:00] FATAL: ---------------------------------------------------------------------------------------
[2022-05-13T16:25:57+00:00] FATAL: PLEASE PROVIDE THE CONTENTS OF THE stacktrace.out FILE (above) IF YOU FILE A BUG REPORT
[2022-05-13T16:25:57+00:00] FATAL: ---------------------------------------------------------------------------------------
[2022-05-13T16:25:57+00:00] FATAL: NameError: uninitialized constant #<Class:#<Chef::Recipe:0x0000000002bac870>>::Var
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: 1 actions failed.
>>>>>> Converge failed on instance <default-ubuntu-2004>. Please see .kitchen/logs/default-ubuntu-2004.log for more details
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try runn
我花了2几周来解决我无法。如果有能力做到这一点,请帮助我。
I'm new to Chef and Ruby, I'm very confused how to handle variables in Chef, and I am trying to pass a variable [Var] from ruby_block resource to http_request resource and assign the [Var] value to the value in "value":"#{Var}"
in the chef recipe [customer.rb] as shown in the code below and I got an error.
[Var.js] is a json file and contains a round 100 lines of code. I want to pass the content of this file to a variable [Var] and call this variable to update some configuration of the website [http://localhost:6000/Master/endpoint_1_in/set]
directory 'work' do
owner 'root'
group 'root'
mode '0775'
recursive true
action :create
end
bash 'Extract customer Package' do
cwd '/work'
code <<-EOH
curl -L --header "PRIVATE-TOKEN: XXXXXXXXXXXXXX" "https://www.company.sm/api/v4/projects/44/packages/generic/customer/1.0.0-customer/customer.tar.gz" > /work/customer.tar.gz
tar xzf customer.tar.gz
EOH
end
ruby_block 'Read customer file into Var variable' do
block do
if File.exists?("/work/customer/Var.js")
Var = IO.read("/work/customer/Var.js")
end
end
end
http_request 'cusromer update' do
action :put
url 'http://localhost:6000/Master/endpoint_1_in/set'
message ({
"moduleId" => "endpoint_1_in",
"moduleType" => "ENDPOINT_1_IN",
"options" => [
{
"key": "somename.text",
"value":"#{Var}"
}
],
}.to_json)
headers({'Authorization' => "Basic #{basicauth}", 'Content-Type' => 'application/json;charset=utf-8' })
retries 10
retry_delay 30
end
and the error I got is
Installing cookbook gem dependencies:
Compiling cookbooks...
Loading Chef InSpec profile files:
Loading Chef InSpec input files:
Loading Chef InSpec waiver files:
================================================================================
Recipe Compile Error in /tmp/kitchen/cache/cookbooks/clinic/recipes/customer.rb
================================================================================
NameError
---------
uninitialized constant #<Class:#<Chef::Recipe:0x0000000002bac870>>::Var
Cookbook Trace: (most recent call first)
----------------------------------------
Relevant File Content:
----------------------
/tmp/kitchen/cache/cookbooks/clinic/recipes/customer.rb:
64: url 'http://localhost:6000/Master/endpoint_1_in/set'
65: message ({
66: "moduleId" => "endpoint_1_in",
67: "moduleType" => "ENDPOINT_1_IN",
68: "options" => [
69: {
70: "key": "somename.text",
71>> "value":"#{Var}"
72: }
73: ],
74:
75: }.to_json)
System Info:
------------
chef_version=17.10.3
platform=ubuntu
platform_version=20.04
ruby=ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]
program_name=/opt/chef/bin/chef-client
executable=/opt/chef/bin/chef-client
Running handlers:
[2022-05-13T16:25:57+00:00] ERROR: Running exception handlers
Running handlers complete
[2022-05-13T16:25:57+00:00] ERROR: Exception handlers complete
Infra Phase failed. 0 resources updated in 02 seconds
[2022-05-13T16:25:57+00:00] FATAL: Stacktrace dumped to /tmp/kitchen/cache/chef-stacktrace.out
[2022-05-13T16:25:57+00:00] FATAL: ---------------------------------------------------------------------------------------
[2022-05-13T16:25:57+00:00] FATAL: PLEASE PROVIDE THE CONTENTS OF THE stacktrace.out FILE (above) IF YOU FILE A BUG REPORT
[2022-05-13T16:25:57+00:00] FATAL: ---------------------------------------------------------------------------------------
[2022-05-13T16:25:57+00:00] FATAL: NameError: uninitialized constant #<Class:#<Chef::Recipe:0x0000000002bac870>>::Var
>>>>>> ------Exception-------
>>>>>> Class: Kitchen::ActionFailed
>>>>>> Message: 1 actions failed.
>>>>>> Converge failed on instance <default-ubuntu-2004>. Please see .kitchen/logs/default-ubuntu-2004.log for more details
>>>>>> ----------------------
>>>>>> Please see .kitchen/logs/kitchen.log for more details
>>>>>> Also try runn
I spent 2 weeks to solve it I could not. If there is workaround to do it, please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您正在运行一些Ruby代码以获取文件内容,因此您甚至可以在厨师资源之外进行此操作。类似:
请注意,我使用了
var
与较低的情况v
。说明:
厨师厨师通过几个阶段。与此答案相关的两个相关的是:
所有变量和资源已编译,并在编译阶段进行分配。因此,
var
仍然没有分配,因为在编译阶段中没有定义。而ruby_block
和http_request
在收敛阶段运行。Since you are running some Ruby code to get the file contents, you can even do it outside Chef resources. Something like:
Note that I've used
var
with lower casev
.Explanation:
The chef-client goes through several phases during the run. The two relevant for this answer are:
All variable and resources are compiled and assignments happen in the compile phase. So
var
remains unassigned as there is no definition in for it in compile phase. Whereasruby_block
andhttp_request
run in the converge phase.