首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Terraform运行脚本

使用Terraform运行脚本
EN

Stack Overflow用户
提问于 2020-11-25 18:20:57
回答 2查看 683关注 0票数 1

为了学习,我尝试在GCP上安装和安装自己的Kubernetes集群。

我想为GCP上的实例提供一个引导脚本。

这是我的google_compute_instance配置

代码语言:javascript
复制
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的时候有这个问题

错误:未能打开脚本的sudo apt更新 sudo apt安装 apt-传输-https ca-证书 卷曲 gnupg剂 软件.特性.通用 zsh 维姆 curl -fsSL https://download.docker.com/linux/debian/gpg \ sudo sudo apt-key add存储库\ "deb arch=amd64 \ $(lsb_release -cs) \ 稳定的sudo apt-get更新& sudo apt-get安装docker-ce docker-ce-cli containerd.io curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg sudo sudo apt-key apt <https://apt.kubernetes.io/ kubernetes-其他主要EOF sudo apt-获取更新sudo apt-获取安装-y kubelet kubeadm kubectl sudo-标记持有kubelet kubeadm kubectl ':打开sudo<sudo apt安装 apt-传输-https ca-证书 卷曲 gnupg剂 软件.特性.通用 zsh 维姆 curl -fsSL https://download.docker.com/linux/debian/gpg \ sudo sudo apt-key add存储库\ "deb arch=amd64 \ $(lsb_release -cs) \ 稳定的sudo apt-get更新& sudo apt-get安装docker-ce docker-ce-cli containerd.io curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg sudo sudo apt-key apt <https://apt.kubernetes.io/ kubernetes-其他主要EOF sudo apt-获取更新sudo apt-获取安装-y kubelet kubeadm kubectl sudo-标记持有kubelet kubeadm kubectl :没有这样的文件或目录。

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

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

下面是脚本:

代码语言:javascript
复制
#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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-25 19:09:03

您应该在钥匙connection块中提供remote-exec参数。

代码语言:javascript
复制
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.

一个示例块可以如下所示:

代码语言:javascript
复制
  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")             
        }
  }
票数 3
EN

Stack Overflow用户

发布于 2020-12-06 19:17:51

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

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

代码语言:javascript
复制
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"]
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65010601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档