简体   繁体   English

如何使用 Terraform 在 Openstack 实例上添加非根 ssh 密钥?

[英]How to add a non-root ssh-key on Openstack instance using Terraform?

I'm trying to create a Openstack instance using Terraform, and after that, I want to configure the user "ubuntu" with my ssh-key.我正在尝试使用 Terraform 创建一个 Openstack 实例,之后,我想用我的 ssh 密钥配置用户“ubuntu”。

So far I'm doing:到目前为止,我正在做:

terraform-instances.tf terraform-instances.tf

resource "openstack_compute_keypair_v2" "tf-keypair" {
  name       = "${var.openstack_keypair}"
  public_key = "${file("~/.ssh/id_rsa.pub")}"
}

resource "openstack_compute_instance_v2" "k8s-master" {
  name      = "k8s-master"
  region    = "${var.openstack_region}"
  image_id  = "${var.openstack_image_id}"
  flavor_id = "${var.openstack_flavor_id}"
  key_pair  = "${var.openstack_keypair}"
  depends_on = ["openstack_compute_keypair_v2.tf-keypair"]

  network {
    uuid = "00000000-0000-0000-0000-000000000000"
    name = "public"
  }
  network {
    uuid = "11111111-1111-1111-1111-111111111111"
    name = "private"
  }
}

resource "null_resource" "authkeys" {
  depends_on = ["openstack_compute_instance_v2.k8s-master"]
  provisioner "file" {
    source      = "authorized_keys"
    destination = "/home/ubuntu/.ssh/authorized_keys"
    }

  connection {
    host        = "${openstack_compute_instance_v2.k8s-master.network.0.fixed_ip_v4}"
    type        = "ssh"
    user        = "root"
    private_key = "${file("~/.ssh/id_rsa")}"
    timeout     = "45s"
    }
}

So after run:所以运行后:

terraform apply 

The instance is created, with my ssh-key for the root user, but not with the ubuntu user.实例已创建,我的 ssh-key 为 root 用户,但不是 ubuntu 用户。

How can I deploy my ssh-key for the ubuntu user?如何为 ubuntu 用户部署我的 ssh 密钥?

我建议使用 cloud-init,因为 openstack 中没有任何东西(而且它实际上使用 cloud-init 本身来预置密钥)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM