ruby 在OpenStack,GCE,DigitalOcean上应用terraform后生成ssh_config的快速脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 在OpenStack,GCE,DigitalOcean上应用terraform后生成ssh_config的快速脚本相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env ruby
# NOTICE: GCE and OpenStack providers only.

require 'json'
require 'erb'

def get_template()
%{
<% hosts.each do |key, entry| %>
Host <%= key %>
  User <%= entry[:user] %>
  Hostname <%= entry[:hostname] %>
<% end %>
}
end

class SshConfig
  attr_accessor :hosts

  def initialize(hosts)
    @hosts = hosts
  end

  def get_binding
    binding()
  end
end

file = File.read('terraform.tfstate')
data_hash = JSON.parse(file)

hosts = {}

data_hash['modules'][0]['resources'].each do |key, resource|
  if ['openstack_compute_instance_v2','google_compute_instance','digitalocean_droplet'].include?(resource['type'])
    attributes = resource['primary']['attributes']
    name = attributes['name']
    hostname = attributes['access_ip_v4'] || attributes['network_interface.0.access_config.0.assigned_nat_ip'] || attributes['ipv4_address']

    if resource['type'] == 'digitalocean_droplet'
      user = 'root'
    else
      user = 'ubuntu'
    end

    hosts[name] = {
      :hostname => hostname,
      :user => user,
    }
  end
end

renderer = ERB.new(get_template)

puts renderer.result(SshConfig.new(hosts).get_binding)

以上是关于ruby 在OpenStack,GCE,DigitalOcean上应用terraform后生成ssh_config的快速脚本的主要内容,如果未能解决你的问题,请参考以下文章

OpenStack 上 Kubernetes 中节点(minions)的水平自动缩放

cloud-init 执行分析

如何在Ruby中定义一个参数没有类型的方法[关闭]

在 GCE 帐户中插入图像

GCE 上没有名为“google.cloud._helpers”的模块

sh 在GCE上安装vpn