这是我想要实现的目标:
我有一个厨师erb文件,其中包含以下声明:
<% node[:opsworks][:layers][:mylayer][:instances].each do |private_ip| %>
- "<%= private_ip %>"
<% end %>
产生输出:
"["mylayer2", {"public_dns_name"=>nil,
"private_dns_name"=>"ip-10-0-1-157.us-west-1.compute.internal",
"backends"=>2, "ip"=>nil,
"private_ip"=>"10.0.1.157",
"instance_type"=>"t2.micro",
"status"=>"online",
"id"=>"ba33dad5-c294-44ab-bdb5-d292e02e0658",
"aws_instance_id"=>"i-a93bb169",
"elastic_ip"=>nil,
"created_at"=>"2015-08-26T11:58:03+00:00",
"booted_at"=>"2015-08-26T11:58:45+00:00",
"region"=>"us-west-1", "availability_zone"=>"us-west-1a",
"infrastructure_class"=>"ec2"}]"
和
"["mylayer1", {"public_dns_name"=>nil,
"private_dns_name"=>"ip-10-0-1-253.us-west-1.compute.internal",
"backends"=>2, "ip"=>nil,
"private_ip"=>"10.0.1.253",
"instance_type"=>"t2.micro",
"status"=>"online",
"id"=>"be6e9645-ef12-416b-9401-e7379c3f85e1",
"aws_instance_id"=>"i-b90c9b79",
"elastic_ip"=>nil,
"created_at"=>"2015-08-28T08:39:42+00:00",
"booted_at"=>"2015-08-28T08:41:05+00:00",
"region"=>"us-west-1",
"availability_zone"=>"us-west-1a",
"infrastructure_class"=>"ec2"}]"
它生成所有实例信息,但我需要输出(仅限私有IP&#39;)
- 10.0.1.157
- 10.0.1.253
请帮助我。
答案 0 :(得分:0)
通过迭代您的实例,并在您执行此操作时调用每个实例private_ip
,您看起来很困惑。试试这个:
<% node[:opsworks][:layers][:mylayer][:instances].each do |instance| %>
- "<%= instance['private_ip'] %>"
<% end %>
答案 1 :(得分:0)
正如在IRC上提到的那样,实例数据看起来可能是一个哈希,所以你想要同时获取键和值,然后从值中获取private_ip:
<% node[:opsworks][:layers][:mylayer][:instances].each do |instance_name, instance_data| %>
- "<%= instance_data['private_ip'] %>"
<% end %>