Skip to content

Commit 3dcc320

Browse files
author
dmytro_velychko3
committed
fix
1 parent 4604d7d commit 3dcc320

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ module "linux_virtual_machine" {
6565
| <a name="input_custom_network_interface_name"></a> [custom\_network\_interface\_name](#input\_custom\_network\_interface\_name)| Specifies the name of the virtual machine interface name resource | `string` | null | no |
6666
| <a name="input_custom_public_ip_name"></a> [custom\_public\_ip\_name](#input\_custom\_public\_ip\_name)| Specifies the name of the public ip name name resource | `string` | null | no |
6767
| <a name="input_tags"></a> [tags](#input\_tags)| Resource tags | map(any) | {} | no |
68-
| <a name="input_allocation_method"></a> [allocation\_method](#input\_allocation\_method)| Defines the allocation method for this IP address. Possible values are Static or Dynamic | map(any) | Dynamic | no |
68+
| <a name="input_public_ip_allocation_method"></a> [public\_ip\_allocation\_method](#input\_public\_ip\_allocation_method)| Defines the allocation method for this IP address. Possible values are Static or Dynamic | map(any) | Dynamic | no |
6969
| <a name="input_subnet_id"></a> [subnet\_id](#input\_subnet\_id)| The ID of the Subnet where this Network Interface should be located in. | `string` | n/a | yes |
7070
| <a name="input_network_interface"></a> [network\_interface](#input\_network\_interface)| Objects to configure network interface | <pre>object({<br> private_ip_address_allocation = string<br> public_ip_enabled = bool<br>})</pre> | <pre>{<br> private_ip_address_allocation = "Dynamic"<br> public_ip_enabled = true<br>}</pre> | no |
7171
| <a name="input_virtual_machine"></a> [virtual\_machine](#input\_virtual\_machine)| Objects to configure virtual_machine | <pre>object({<br> size = string<br> admin_username = string<br>})</pre> | <pre>{<br> size = "Standard_F2"<br> admin_username = "adminuser"<br>}</pre> | no |
7272
| <a name="input_admin_ssh_key"></a> [admin\_ssh\_key](#input\_admin\_ssh\_key)| Objects to configure ssh key reference for virtual machine | <pre>object({<br> username = string<br> public_key = string<br>})</pre> | n/a | yes |
7373
| <a name="input_os_disk"></a> [os\_disk](#input\_os\_disk)| Objects to configure os disk reference for virtual machine | <pre>object({<br> caching = string<br> storage_account_type = string<br>})</pre> | <pre>{<br> caching = "ReadWrite"<br> storage_account_type = "Standard_LRS"<br>}</pre> | no |
7474
| <a name="input_source_image_reference"></a> [source\_image\_reference](#input\_source\_image\_reference)| Objects to configure source image reference for virtual machine | <pre>object({<br> publisher = string<br> offer = string<br> sku = string<br> version = string<br>})</pre> | <pre>{<br> publisher = "Canonical"<br> offer = "0001-com-ubuntu-server-focal"<br> sku = "20_04-lts"<br> version = "latest"<br>}</pre> | no |
75+
| <a name="input_password_access_enable"></a> [password\_access\_enable](#input\_password\_access\_enable)| Password access enable | `bool` | true | no |
7576
7677
## Modules
7778

@@ -91,7 +92,8 @@ No modules.
9192
| Name | Description |
9293
| ----------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- |
9394
| <a name="output_id"></a> [id](#output\_id) | The ID of the Linux Virtual Machine |
94-
| <a name="output_access_connector_identity"></a> [access\_connector\_identity](#output\_access\_connector\_identity) | linux virtual machine Identities list |
95+
| <a name="output_identity"></a> [identity](#output\_identity) | linux virtual machine identity |
96+
| <a name="output_password"></a> [password](#output\_password) | The password of the Linux Virtual Machine |
9597
<!-- END_TF_DOCS -->
9698

9799
## License

main.tf

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ locals {
55
public_ip = var.custom_public_ip_name == null ? "ip-${var.project}-${var.env}-${var.location}${local.suffix}" : "${var.custom_public_ip_name}${local.suffix}"
66
}
77

8+
resource "random_string" "this" {
9+
count = var.password_access_enable ? 1 : 0
10+
11+
length = 16
12+
special = true
13+
override_special = "/@£I"
14+
}
15+
816
resource "azurerm_public_ip" "this" {
917
count = var.network_interface.public_ip_enabled ? 1 : 0
1018

@@ -24,28 +32,31 @@ resource "azurerm_network_interface" "this" {
2432
name = "ip-config-${var.project}-${var.env}-${var.location}"
2533
subnet_id = var.subnet_id
2634
private_ip_address_allocation = var.network_interface.private_ip_address_allocation
27-
public_ip_address_id = var.network_interface.public_ip_enabled ? azurerm_public_ip.this[0].id : ""
35+
public_ip_address_id = var.network_interface.public_ip_enabled ? azurerm_public_ip.this[0].id : null
2836
}
2937
}
3038

3139
resource "azurerm_linux_virtual_machine" "this" {
32-
name = local.virtual_machine_name
33-
resource_group_name = var.resource_group
34-
location = var.location
35-
size = var.virtual_machine.size
36-
admin_username = var.virtual_machine.admin_username
37-
tags = var.tags
38-
network_interface_ids = [azurerm_network_interface.this.id, ]
40+
name = local.virtual_machine_name
41+
resource_group_name = var.resource_group
42+
location = var.location
43+
size = var.virtual_machine.size
44+
admin_username = var.virtual_machine.admin_username
45+
tags = var.tags
46+
network_interface_ids = [azurerm_network_interface.this.id, ]
47+
disable_password_authentication = var.password_access_enable ? false : true
48+
admin_password = var.password_access_enable ? random_string.this[0].result : null
3949

4050
identity {
4151
type = "SystemAssigned"
4252
}
4353

4454
admin_ssh_key {
45-
username = var.admin_ssh_key.username
46-
public_key = var.admin_ssh_key.public_key
55+
username = var.password_access_enable ? null : var.admin_ssh_key.username
56+
public_key = var.password_access_enable ? null : var.admin_ssh_key.public_key
4757
}
4858

59+
4960
os_disk {
5061
caching = var.os_disk.caching
5162
storage_account_type = var.os_disk.storage_account_type

outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ output "access_connector_identity" {
77
value = azurerm_linux_virtual_machine.this.identity[0].principal_id
88
description = "linux virtual machine Identities list"
99
}
10+
11+
output "password" {
12+
value = var.password_access_enable ? random_string.this[0].result : ""
13+
description = "The password of the Linux Virtual Machine"
14+
}

variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ variable "network_interface" {
6767
description = "Objects to configure network interface"
6868
default = {
6969
private_ip_address_allocation = "Dynamic"
70-
public_ip_enabled = false
70+
public_ip_enabled = true
7171
}
7272
}
7373

@@ -118,3 +118,9 @@ variable "source_image_reference" {
118118
version = "latest"
119119
}
120120
}
121+
122+
variable "password_access_enable" {
123+
type = bool
124+
description = "Password access enable"
125+
default = true
126+
}

0 commit comments

Comments
 (0)