Skip to content

Commit 1c6986b

Browse files
support raw value for ssh keys in addition to paths (#159)
* support raw value for ssh keys in addion to paths * support raw value for ssh keys in addion to paths * moved ssh key type check to linux vm resource * moving ssh value to a new variable * tf fmt * Update README.md change name to `ssh_key_values` * Update main.tf * Update main.tf * Update variables.tf * Update README.md format Co-authored-by: Yuping Wei <[email protected]>
1 parent 08cf99e commit 1c6986b

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ More specifically this provisions:
148148
When ssh keys are enabled you can either
149149
- use the default "~/.ssh/id_rsa.pub"
150150
- set one key by setting a path in ssh_key variable. e.g "joey_id_rsa.pub"
151-
- 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)
151+
- set ssh_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)
152+
- set ssh_key_values as a list of raw public ssh keys values or refer it to a data source with the public key value, e.g. `["ssh-rsa AAAAB3NzaC1yc..."]`
152153

153154
4 - You can install custom certificates / secrets on the virtual machine from Key Vault by using the variable `os_profile_secrets`.
154155

@@ -197,6 +198,7 @@ module "linuxservers" {
197198
data_disk_size_gb = 64
198199
data_sa_type = "Premium_LRS"
199200
enable_ssh_key = true
201+
ssh_key_values = ["ssh-rsa AAAAB3NzaC1yc2EAAAAD..."]
200202
vm_size = "Standard_D4s_v3"
201203
202204
tags = {

main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ resource "azurerm_virtual_machine" "vm-linux" {
108108
key_data = file(ssh_keys.value)
109109
}
110110
}
111+
112+
dynamic ssh_keys {
113+
for_each = var.enable_ssh_key ? var.ssh_key_values : []
114+
content {
115+
path = "/home/${var.admin_username}/.ssh/authorized_keys"
116+
key_data = ssh_keys.value
117+
}
118+
}
119+
111120
}
112121

113122
dynamic "os_profile_secrets" {

test/fixture/main.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ module "debianservers" {
100100
depends_on = [azurerm_resource_group.test]
101101
}
102102

103+
module "debianservers2" {
104+
source = "../../"
105+
vm_hostname = "${random_id.ip_dns.hex}-d2"
106+
resource_group_name = azurerm_resource_group.test.name
107+
location = var.location_alt
108+
admin_username = var.admin_username
109+
vm_os_simple = var.vm_os_simple_2
110+
vnet_subnet_id = azurerm_subnet.subnet2.id
111+
enable_ssh_key = true
112+
ssh_key_values = ["ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC8GIRF1Snlg9NKCmM74RHXqRGMXyui088+ntQqkQkFIL/BrlgP3CzOgHQmJ+3f0Up/+9UY9vX7AmT7WxVTyqBHT/Aes3VmU3wLO5/MMV/HRrT4z2QV/80futhxjk2unNdWGvbFcR6Y3I44EJFmr8GMbyXRtr0ibuv8BlTYx/K6AXSJ3V+kBqXMOF1QRvVoX9fJKPKjMsebe0cB1IYlm9KLqtciMy+aFOEsSNfrw5cNVsQfK3BgOUKAHsLfBiR7imA2ca+hh005GEtcVJvpvFzcM+bZggUpdqQwIzk1Kv/tROiJiGS0NnyzoxIZYeM3z/mQ5qnglp+174XGCG66EAnVdf5kbaI0Iu7FpAmVhJ92N+MNKoP6vT8cMkYYZf3RaiMMnzjswK/VLbb5ks6Qe9qEPXW1IBtkaaF7+0PCWbPr86I0G2bOa2tFyOHm046Z9sRlkaOO95hmer6Y6MUbMpfeprmjR87u6MVOPglnARfV3UI9i6wOUhVVIi6Wb424HWU="]
113+
114+
depends_on = [azurerm_resource_group.test]
115+
}
116+
103117
module "windowsservers" {
104118
source = "../../"
105119
vm_hostname = "${random_id.ip_dns.hex}-w" // line can be removed if only one VM module per resource group

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ variable "ssh_key" {
3838
default = "~/.ssh/id_rsa.pub"
3939
}
4040

41+
variable "ssh_key_values" {
42+
description = "List of Public SSH Keys values to be used for ssh access to the VMs."
43+
type = list(string)
44+
default = []
45+
}
4146
variable "remote_port" {
4247
description = "Remote tcp port to be used for access to the vms created via the nsg applied to the nics."
4348
type = string

0 commit comments

Comments
 (0)