运行 带有 Terraform 的脚本

Running a script with Terraform

出于学习目的,我正在尝试在 GCP 上安装和设置我自己的 Kubernetes 集群。

我想使用 bootstrap 脚本在 GCP 上配置我的实例。

这是我的 google_compute_instance 配置

resource "google_compute_instance" "default" {
    name = var.vm_name
    machine_type = "f1-micro"
    zone = "europe-west1-b"

    boot_disk {
        initialize_params {
            image = "debian-cloud/debian-9"
        }
    }

    network_interface {
        network = var.network
        access_config {
            // Include this section to give the VM an external IP address
        }
    }

    provisioner "remote-exec" {
        script = var.script_path
        connection {
            type        = "ssh"
            host        = var.ip_address
            user        = "root"
        }
    }

    tags = ["node"]
}

我做的时候遇到这个问题terraform apply

Error: Failed to open script 'sudo apt-get update

sudo apt-get install
apt-transport-https
ca-certificates
curl
gnupg-agent
software-properties-common
zsh
vim

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/debian \ $(lsb_release -cs) \
stable" sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list deb https://apt.kubernetes.io/ kubernetes-xenial main EOF sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl ': open sudo apt-get update

sudo apt-get install
apt-transport-https
ca-certificates
curl
gnupg-agent
software-properties-common
zsh
vim

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/debian \ $(lsb_release -cs) \
stable" sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list deb https://apt.kubernetes.io/ kubernetes-xenial main EOF sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl : no such file or directory

我所有的实例都是在云上创建的,它似乎找到了 bootstrap 脚本,但它显示了这个错误。

我错过了什么?有更好的方法吗?

这是脚本:

#bin/bash

sudo apt-get update

sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common \
    zsh \
    vim

curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/debian \
   $(lsb_release -cs) \
   stable"
sudo apt-get update && sudo apt-get install docker-ce docker-ce-cli containerd.io

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

您应该在 remote-execconnection 块中提供 private_key 参数。

private_key - The contents of an SSH key to use for the connection. These can be loaded from a file on disk using the file function. This takes preference over the password if provided.

示例块可能是这样的:

  provisioner "remote-exec" {
        script = var.script_path
        connection {
            host     = var.ip_address
            type     = "ssh"      
            user     = "root"
            private_key = fileexists("/temp/private_key") ? file("/temp/private_key") : file("C:/private_key")             
        }
  }

对于那些感兴趣的人,我找到了一个更简单的解决方案,不使用 ssh,而是使用创建资源时可用的 google 元数据。

metadata_startup_script = file("./scripts/bootstrap.sh")

resource "google_compute_instance" "default" {
    name = var.vm_name
    machine_type = "e2-standard-2"
    zone = "europe-west1-b"

    boot_disk {
        initialize_params {
            image = "debian-cloud/debian-9"
        }
    }

    network_interface {
        network = var.network
        access_config {
            // Include this section to give the VM an external IP address
        }
    }

    metadata_startup_script = file("./scripts/bootstrap.sh")

    tags = ["node"]
}