Terraform random_string作为Linux机器的密码

Tim*_*lbe 2 google-cloud-platform terraform

我想用它为gcp中的linux机器生成一个随机密码。

我的问题是我以后如何获取密码。我应该为此使用输出,还是将其存储在其他任何地方?我在互联网上看到了这段代码,然后问自己如何知道密码。

resource "random_string" "master_password" {
  length  = 16
  special = true
}

resource "google_container_cluster" "test" {
  name               = "test"
  zone               = "europe-west1-d"

  master_auth {
    username = "client"
    password = "${random_string.master_password.result}"
  }

  node_pool = [{
    name       = "pool"
    autoscaling = {
      min_node_count = 1
      max_node_count = 3
    }

    node_config {
      disk_size_gb = 100
      machine_type = "n1-standard-2"

      oauth_scopes = [
        "https://www.googleapis.com/auth/compute",
        "https://www.googleapis.com/auth/devstorage.read_only",
        "https://www.googleapis.com/auth/logging.write",
        "https://www.googleapis.com/auth/monitoring",
      ]

      labels {
        test = "true"
      }
    }
  }]
}
Run Code Online (Sandbox Code Playgroud)

Eri*_*son 5

密码将存储在您的状态文件中。您可以在那里进行挖掘,但是完全有可能文件的确切位置会在Terraform版本之间改变。

如前所述,获得一致输出的最佳方法是使用输出块。然后,当您执行a时terraform apply,此密码将以良好的可读性输出。请注意,有权访问您的州的任何人都可以访问该密码,因此请确保州的安全。

如果您使用远程状态(例如在S3存储桶中),则还可以使用terraform_remote_state从另一个Terraform运行中访问此状态。您将需要显式指定output要从中使用的值terraform_remote_state

最后,请注意,如果某些内容捕获了您的输出terraform apply,那么自从terraform apply写入STDOUT 以来,它还将捕获该输出。如果使用CI工具,可能会发生这种情况。只是要注意的事情。