将变量从厨师资源传递到同一厨师食谱中的其他资源
pass variables from a chef resouce to other resources in the same chef recipe
我是 Chef 和 Ruby 的新手,我很困惑如何在 Chef 中处理变量,我正在尝试将变量 [Var] 从 ruby_block 资源传递到 http_request 资源并将 [Var] 值分配给厨师食谱 [customer.rb] 中 "value":"#{Var}"
中的值,如下面的代码所示,我得到了一个错误。
[Var.js] 是一个 json 文件,包含大约 100 行代码。我想将这个文件的内容传递给一个变量[Var],并调用这个变量来更新网站的一些配置[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
我得到的错误是
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个星期才解决我不能。如果有解决方法,请帮助我。
由于您正在 运行 宁一些 Ruby 代码来获取文件内容,您甚至可以在 Chef 资源之外执行此操作。类似于:
if File.exists?("/work/customer/Var.js")
var = File.read("/work/customer/Var.js")
end
http_request 'cusromer update' do
action :put
# rest of code
end
请注意,我使用 var
小写 v
。
解释:
chef-client 在 运行 期间经过 several phases。与此答案相关的两个是:
- 编译
- 收敛
编译所有变量和资源,并在编译阶段进行赋值。所以 var
保持未分配状态,因为在编译阶段没有对它的定义。而 ruby_block
和 http_request
运行 在收敛阶段。
我是 Chef 和 Ruby 的新手,我很困惑如何在 Chef 中处理变量,我正在尝试将变量 [Var] 从 ruby_block 资源传递到 http_request 资源并将 [Var] 值分配给厨师食谱 [customer.rb] 中 "value":"#{Var}"
中的值,如下面的代码所示,我得到了一个错误。
[Var.js] 是一个 json 文件,包含大约 100 行代码。我想将这个文件的内容传递给一个变量[Var],并调用这个变量来更新网站的一些配置[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
我得到的错误是
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个星期才解决我不能。如果有解决方法,请帮助我。
由于您正在 运行 宁一些 Ruby 代码来获取文件内容,您甚至可以在 Chef 资源之外执行此操作。类似于:
if File.exists?("/work/customer/Var.js")
var = File.read("/work/customer/Var.js")
end
http_request 'cusromer update' do
action :put
# rest of code
end
请注意,我使用 var
小写 v
。
解释:
chef-client 在 运行 期间经过 several phases。与此答案相关的两个是:
- 编译
- 收敛
编译所有变量和资源,并在编译阶段进行赋值。所以 var
保持未分配状态,因为在编译阶段没有对它的定义。而 ruby_block
和 http_request
运行 在收敛阶段。