Capistrano :NoMethodError: undefined method `merge' for"ec2 private ip":String

Capistrano :NoMethodError: undefined method `merge' for"ec2 private ip":String

我正在尝试通过堡垒主机部署多个 EC2 实例。我将我的 EC2 实例置于 ELB 下并始终通过 bastion 访问。

require 'aws-sdk'
require 'net/ssh/proxy/command'

ec2 = Aws::EC2::Client.new(region: fetch(:aws_region))

ec2_filtered = ec2.describe_instances(
    filters:[
        {name: "tag:env", values: [fetch(:rails_env)]},
        {name: "tag:role", values: [fetch(:aws_tag_role)]},
        {name: 'instance-state-name', values: ['running']}
    ])

instances = ec2_filtered.reservations.map(&:instances)[0].map(&:private_ip_address)
instances = ec2_filtered.reservations.map(&:instances)[1].map(&:private_ip_address)

set :branch, 'master'

role :app, *instances
role :web, *instances
role :db, [instances.first]

server *instances,
  user: fetch(:deploy_user),
  ssh_options: {
    forward_agent: true,
    keys: fetch(:deploy_ssh_keys),
    proxy: Net::SSH::Proxy::Command::new('ssh bastion.mamorio -W %h:%p')
  }

上面的代码有效,但它确实是多余的,我想立即获得整个 "private ip"。

我试过这段代码:

instances = ec2_filtered.reservations.map(&:instances).flatten.map(&:private_ip_address)

但我收到以下错误。

NoMethodError: undefined method `merge' for "10.0.xx.2xx":String

有什么建议吗?

我认为问题在于您传递给 Capistrano 的 role 声明的值。

role :app, *instances

role 方法需要一个数组作为第二个参数,但在您的示例中,您是 "splatting" 将 instances 数组分成单独的参数。

试试这个:

role :app, instances