【发布时间】:2020-07-01 06:11:56
【问题描述】:
我在厨师食谱中使用 http_request 资源来发出需要代理用户和密码的 http 请求。我面临替换属性中定义的变量或实际上任何变量的问题 例如以下代码工作正常,其中用户名和密码是硬编码的。
http_request 'get-info' do
url "http://host:8080/v123/orgs/abc"
headers({ 'AUTHORIZATION' => "Basic #{ Base64.encode64('user1:pwd123')}",
'Content-Type' => 'application/json' } )
message ( "{ } " )
action :get
end
但是如果我使用变量而不是像下面这样的硬编码凭据
u_name=node['mychef']['username']
pwd=node['mychef']['password']
http_request 'get-info' do
url "http://host:8080/v123/orgs/abc"
headers({ 'AUTHORIZATION' => "Basic #{ Base64.encode64('#{u_name}:#{pwd}')}",
'Content-Type' => 'application/json' } )
message ( "{ } " )
action :get
end
然后我得到以下错误
================================================================================
Error executing action `get` on resource 'http_request[get-info]'
================================================================================
Net::HTTPServerException
------------------------
401 "Unauthorized"
当然,凭据没有被正确读取。感谢有关如何在厨师资源 http_request 中替换变量的帮助。
【问题讨论】:
标签: ruby automation chef-infra devops chef-recipe