ruby - 如何将找到的每个值映射到不同的变量

标签 ruby chef-infra

我想创建一个 Chef Recipe ,但我被困在这一步:

members = search("node", "recipe:redis AND chef_environment:#{node.chef_environment}") || []
members.each do |node|
  puts "#{node["ipaddress"]}"
end

这正在运行,并向我返回找到的这三台服务器的 IP 地址。 但我想将找到的每个值(IP 地址)分配给一个变量,因为我将运行如下命令:

execute 'command' do
  command "redis-trib.rb create --replicas 1 #{node1}:6379 #{node2}:6379 #{node3}:6379 #{node1}:6380 #{node2}:6380 #{node3}:6380"
end

最佳答案

你可以这样做:

nodes = members.map do |node|
          node["ipaddress"]
        end

然后像这样使用它:

execute 'command' do
  command "redis-trib.rb create --replicas 1 #{nodes[0]}:6379 #{nodes[1]}:6379 #{nodes[2]}:6379 #{nodes[0]}:6380 #{nodes[1]}:6380 #{nodes[2]}:6380"
end

关于ruby - 如何将找到的每个值映射到不同的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886333/

相关文章:

ruby-on-rails - 没有路由匹配 { :action= >"show", :controller= >"users"} 错误

Ansible Tower (AWX) - 在剧本中使用安全变量?

ruby - 如何在 Chef 中使用特殊字符?

git - Vagrant 测试 Chef Recipe - 但如何处理私有(private) git

ruby - 在 Ruby 版本管理器 (RVM) 中清理旧引用

ruby - Ruby/Sinatra 中的继承

ruby - 如何比较数组中的值

chef-infra - 如何让 Chef 自定义 lwrp 实现通知和 not_if?

apache - 在 Vagrant 盒子上创建虚拟主机

ruby-on-rails - 更改 :id parameter in Routing resources for Rails 的名称