简体   繁体   中英

How Do I Seed SSH Keys For Ansible Using Terraform?

I am creating my Ubuntu VMs with terraform and will subsequently configure them with ansible.

However, ansible needs a user and ssh keys set up to connect so the public key used for ansible needs to be in the authorized_keys file on the server.

If I use my ssh public key it will work on my machine but it wont work on other machines or on the build server as they wont have the private key.

What is the command to create a new key and not overwrite my current key?

Should I check this key into git so that the same key is passed to terraform?

How do I change my terraform task to use the key from version control?

Current Terraform task:

resource "azurerm_virtual_machine" "client-vm" {
  name                  = "${var.prefix}-${var.client_name}-vm"
  resource_group_name   = var.resource_group_name
  location              = var.resource_group_location
  network_interface_ids = [azurerm_network_interface.network-interface.id]
  vm_size               = "Standard_D2s_v3"

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  #TODO: Switch to SSH Keys
  os_profile {
    computer_name  = "${var.client_name}"
    admin_username = var.username
    admin_password = var.password
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }

}

Yes, at least if this is how your cloud provider handles SSH keys.. (eg AWS does):

resource "aws_instance" "web" {
  ami           = "${data.aws_ami.ubuntu.id}"
  instance_type = "t2.micro"

  key_name = "name_of_the_aws_keypair"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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