将变量从厨师重新解释到同一厨师食谱中的其他资源

发布于 2025-01-28 12:35:26 字数 5156 浏览 3 评论 0原文

我是厨师和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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱你一生 2025-02-04 12:35:26

由于您正在运行一些Ruby代码以获取文件内容,因此您甚至可以在厨师资源之外进行此操作。类似:

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

说明:

厨师厨师通过几个阶段。与此答案相关的两个相关的是:

  • 编译
  • 收敛

所有变量和资源已编译,并在编译阶段进行分配。因此,var仍然没有分配,因为在编译阶段中没有定义。而ruby​​_blockhttp_request在收敛阶段运行。

Since you are running some Ruby code to get the file contents, you can even do it outside Chef resources. Something like:

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

Note that I've used var with lower case v.

Explanation:

The chef-client goes through several phases during the run. The two relevant for this answer are:

  • Compile
  • Converge

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. Whereas ruby_block and http_request run in the converge phase.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文