Skip to content

Instantly share code, notes, and snippets.

@yamamoto-febc
Last active May 23, 2018 08:15
Show Gist options
  • Save yamamoto-febc/af549b265abfaf52022ae6fd6f419e19 to your computer and use it in GitHub Desktop.
Save yamamoto-febc/af549b265abfaf52022ae6fd6f419e19 to your computer and use it in GitHub Desktop.
Rancherのホスト(エージェント)登録をTerraformで
variable "password" {
default = "PUT_YOUR_PASSWORD_HERE"
}
variable "hostname" {
default = "agent-from-terraform"
}
#Create a new Rancher registration token
resource "rancher_registration_token" "default" {
name = "default_token"
description = "Registration token for the defauot environment"
environment_id = "1a5"
agent_ip = "${sakuracloud_server.rancher.ipaddress}"
}
resource rancher_host "foo" {
name = "foo"
description = "The foo node"
environment_id = "1a5"
hostname = "${var.hostname}"
}
# 公開鍵(さくらのクラウド上で生成)
resource "sakuracloud_ssh_key_gen" "key" {
name = "foobar"
}
# さくらのクラウドならコンソールをポチポチしなくてもRancherOSインストールできちゃうよ
data sakuracloud_archive "rancheros" {
os_type = "rancheros"
}
# ディスク定義
resource "sakuracloud_disk" "rancher" {
name = "disk01"
source_archive_id = "${data.sakuracloud_archive.rancheros.id}"
ssh_key_ids = ["${sakuracloud_ssh_key_gen.key.id}"]
password = "${var.password}"
hostname = "rancher-from-terraform"
disable_pw_auth = true
}
# サーバー定義
resource "sakuracloud_server" "rancher" {
name = "server01"
disks = ["${sakuracloud_disk.rancher.id}"]
}
# プロビジョニング
resource "null_resource" "run_agent" {
triggers {
register_token = "${rancher_registration_token.default.id}"
servers = "${join(",", sakuracloud_server.rancher.*.id)}"
}
connection {
host = "${sakuracloud_server.rancher.ipaddress}"
user = "rancher"
private_key = "${sakuracloud_ssh_key_gen.key.private_key}"
}
provisioner "remote-exec" {
inline = [
"${rancher_registration_token.default.command}",
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment