Json 使用URL中的node.Chef_环境调用厨师配方API
正在尝试进行API调用,该调用需要URL中的Json 使用URL中的node.Chef_环境调用厨师配方API,json,ruby,chef-infra,chef-recipe,Json,Ruby,Chef Infra,Chef Recipe,正在尝试进行API调用,该调用需要URL中的节点.chef_环境 我无法在我的厨师食谱中使用节点.chef_环境部分,有什么帮助吗 require 'net/http' require 'json' response = Net::HTTP.get URI('http://1.1.1.1:81/terra/v2/terraform/get/%{node.chef_environment}') octocat = JSON.parse response octocat.keys ip =
节点.chef_环境
我无法在我的厨师食谱中使用节点.chef_环境
部分,有什么帮助吗
require 'net/http'
require 'json'
response = Net::HTTP.get URI('http://1.1.1.1:81/terra/v2/terraform/get/%{node.chef_environment}')
octocat = JSON.parse response
octocat.keys
ip = octocat['terraform']['modules'][0]['outputs']['public_ip_address']['value'][0]
log ip
任何帮助或建议都会很棒
把简单看作
response=Net::HTTP.get URI(“+node.chef_environment)好的,首先是字符串。Ruby使用
{}
进行字符串插值,但必须在双引号内,而不是单引号内。%{}
格式也是一个东西,但它用于Ruby版本的printf的一些时髦的东西,而不是您想要的。因此,我们共同拥有”http://1.1.1.1:81/terra/v2/terraform/get/#{node.chef_environment}“
用于字符串
其次,Chef包含一个HTTP客户端,您通常应该使用它,而不是Chef代码内部的Net::HTTP
。在这种情况下:
octocat = Chef::HTTP::SimpleJSON('http://1.1.1.1:81/').get("/terra/v2/terraform/get/#{node.chef_environment}")
这将使用更快的JSON库Chef includes(ffi yajl)处理重定向、瞬时故障和JSON解码等问题。不要用另一个字符串添加字符串。。。字符串插值就是为了这个。例如:
response=Net::HTTP.get URI(“http://1.1.1.1:81/terra/v2/terraform/get/#{node.chef_environment}“
它只使用双引号;单引号可防止任何字符串插值。hiya,尝试过在运行纯ruby但不在chef recipeChef中运行它。配方是ruby解释的,所以您的代码中一定有单引号或键入错误。。。