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

时间:2019-01-25 20:34:56

标签: openstack terraform ssh-keys terraform-provider-openstack

我试图使用Terraform创建一个Openstack实例,然后,我想用ssh-key配置用户“ ubuntu”。

到目前为止,我正在这样做:

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"
    }
}

因此,运行后:

terraform apply 

使用我的ssh键为root用户创建实例,但不为ubuntu用户创建实例。

如何为ubuntu用户部署ssh-key?

1 个答案:

答案 0 :(得分:0)

我建议使用cloud-init,因为没有任何东西可以放入openstack中(它实际上是使用cloud-init本身来预先植入密钥的。)