Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple ssh keys #147

Merged
merged 5 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ More specifically this provisions:

1 - New vnet for all vms

2 - Ubuntu 14.04 Server VMs using `vm_os_publisher`, `vm_os_offer` and `vm_os_sku` which is configured with:
2 - Ubuntu 18.04 Server VMs using `vm_os_publisher`, `vm_os_offer` and `vm_os_sku` which is configured with:
sudoapt-getclean marked this conversation as resolved.
Show resolved Hide resolved

- No public IP assigned, so access can only happen through another machine on the vnet.
- Opens up port 22 for SSH access with the default ~/.ssh/id_rsa.pub key
Expand All @@ -142,7 +142,11 @@ More specifically this provisions:

- "nb_data_disk" Number of the data disks attached to each virtual machine

- "enable_ssh_key" Enable ssh key authentication in Linux virtual Machine
- "enable_ssh_key" Enable ssh key authentication in Linux virtual Machine.
When ssh keys are enabled you can either
- use the default "~/.ssh/id_rsa.pub"
- set one key by setting a path in ssh_key variable. e.g "joey_id_rsa.pub"
- set shh_key and add zero or more files paths in extra_ssh_keys variable e.g. ["ross_id_rsa.pub", "rachel_id_rsa.pub"] (since v3.8.0)

```hcl
provider "azurerm" {
Expand Down
8 changes: 6 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ data "azurerm_resource_group" "vm" {
name = var.resource_group_name
}

locals {
ssh_keys = merge([var.ssh_key], var.extra_ssh_keys)
}

resource "random_id" "vm-sa" {
keepers = {
vm_hostname = var.vm_hostname
Expand Down Expand Up @@ -87,10 +91,10 @@ resource "azurerm_virtual_machine" "vm-linux" {
disable_password_authentication = var.enable_ssh_key

dynamic ssh_keys {
for_each = var.enable_ssh_key ? [var.ssh_key] : []
for_each = var.enable_ssh_key ? local.ssh_keys : []
content {
path = "/home/${var.admin_username}/.ssh/authorized_keys"
key_data = file(var.ssh_key)
key_data = file(ssh_keys.value)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/fixture/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ module "debianservers" {
vnet_subnet_id = azurerm_subnet.subnet2.id
allocation_method = "Static"
enable_ssh_key = true

extra_ssh_keys = ["monica_id_rsa.pub"]

depends_on = [azurerm_resource_group.test]
}

Expand Down
1 change: 1 addition & 0 deletions test/fixture/monica_id_rsa.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC8GIRF1Snlg9NKCmM74RHXqRGMXyui088+ntQqkQkFIL/BrlgP3CzOgHQmJ+3f0Up/+9UY9vX7AmT7WxVTyqBHT/Aes3VmU3wLO5/MMV/HRrT4z2QV/80futhxjk2unNdWGvbFcR6Y3I44EJFmr8GMbyXRtr0ibuv8BlTYx/K6AXSJ3V+kBqXMOF1QRvVoX9fJKPKjMsebe0cB1IYlm9KLqtciMy+aFOEsSNfrw5cNVsQfK3BgOUKAHsLfBiR7imA2ca+hh005GEtcVJvpvFzcM+bZggUpdqQwIzk1Kv/tROiJiGS0NnyzoxIZYeM3z/mQ5qnglp+174XGCG66EAnVdf5kbaI0Iu7FpAmVhJ92N+MNKoP6vT8cMkYYZf3RaiMMnzjswK/VLbb5ks6Qe9qEPXW1IBtkaaF7+0PCWbPr86I0G2bOa2tFyOHm046Z9sRlkaOO95hmer6Y6MUbMpfeprmjR87u6MVOPglnARfV3UI9i6wOUhVVIi6Wb424HWU= regina.filange@nana
8 changes: 7 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ variable "admin_password" {
default = ""
}

variable "extra_ssh_keys" {
description = "Same as ssh_key, but allows for setting multiple public keys. Set your first key in ssh_key, and the extras here."
type = list(string)
default = []
}

variable "ssh_key" {
description = "Path to the public key to be used for ssh access to the VM. Only used with non-Windows vms and can be left as-is even if using Windows vms. If specifying a path to a certification on a Windows machine to provision a linux vm use the / in the path versus backslash. e.g. c:/home/id_rsa.pub."
description = "Path to the public key to be used for ssh access to the VM. Only used with non-Windows vms and can be left as-is even if using Windows vms. If specifying a path to a certification on a Windows machine to provision a linux vm use the / in the path versus backslash. e.g. c:/home/id_rsa.pub."
type = string
default = "~/.ssh/id_rsa.pub"
}
Expand Down