简体   繁体   English

如何使用 Terraform 为 Ansible 播种 SSH 密钥?

[英]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.我正在使用 terraform 创建我的 Ubuntu VM,随后将使用 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.但是,ansible 需要设置用户和 ssh 密钥才能连接,因此用于 ansible 的公钥需要位于服务器上的authorized_keys文件中。

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.如果我使用我的 ssh 公钥,它将在我的机器上工作,但它不会在其他机器或构建服务器上工作,因为它们没有私钥。

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?我应该将此密钥检查到 git 中以便将相同的密钥传递给 terraform 吗?

How do I change my terraform task to use the key from version control?如何更改我的 terraform 任务以使用版本控制中的密钥?

Current Terraform task:当前 Terraform 任务:

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):是的,至少如果这是您的云提供商处理 SSH 密钥的方式..(例如 AWS 这样做):

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

  key_name = "name_of_the_aws_keypair"
}

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

相关问题 Jenkins 管道 - 参考 ansible 和 Z303E96F80576360FA4C7B07AEZB8 中的 SSH 密钥 - Jenkins pipeline - refering to SSH Keys in ansible and Terraform 如何使用Terraform在Azure虚拟机上创建SSH密钥? - How do I create a ssh key on a azure virtual machine using terraform? 使用 Terraform 创建 Ansible 库存 - Create Ansible inventory using Terraform 使用Terraform时如何查询2个不同的Azure目录? - How Do I Query 2 Different Azure Directories When Using Terraform? 如何在 Azure 中使用 Terraform 创建多个安全规则? - How do I create multiple Security rules using Terraform in Azure? 如何将 Terraform 和 Ansible 连接在一起? - How to connect Terraform and Ansible together? 如何用文本替换 terraform 中的变量? - how do i substitute text for a variable in a terraform? 如何使用 terraform 创建 azure 事件网格系统主题? - How do I create an azure event grid system-topic using terraform? 如何在虚拟机规模集中使用 terraform 请求 azure 现场实例? - How do I request azure spot instances using terraform, in a virtual machine scale set? 我想创建 2 azure 虚拟机并使用 terraform 安装 jenkins 和 Sonarqube 有人知道该怎么做吗? - I want to create 2 azure vm and install jenkins and Sonarqube using terraform does anyone know how to do that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM