Browse Source

initial refactor commit

tags/1.0.0
ravi 2 months ago
parent
commit
5d759dba41
12 changed files with 292 additions and 139 deletions
  1. +1
    -1
      .pre-commit-config.yaml
  2. +9
    -2
      README.md
  3. +8
    -3
      examples/default/README.md
  4. +2
    -1
      examples/default/providers.tf
  5. +7
    -2
      examples/disabled/README.md
  6. +2
    -1
      examples/disabled/providers.tf
  7. +8
    -3
      examples/external-resources/README.md
  8. +2
    -1
      examples/external-resources/providers.tf
  9. +8
    -3
      examples/x-vms-x-disks-x-netint/README.md
  10. +2
    -1
      examples/x-vms-x-disks-x-netint/providers.tf
  11. +242
    -120
      main.tf
  12. +1
    -1
      versions.tf

+ 1
- 1
.pre-commit-config.yaml View File

@@ -1,6 +1,6 @@
repos:
- repo: git://github.com/antonbabenko/pre-commit-terraform
rev: v1.27.0
rev: v1.29.0
hooks:
- id: terraform_fmt
- id: terraform_docs


+ 9
- 2
README.md View File

@@ -11,16 +11,23 @@ See `examples` folders for usage of this module.
- No disks can be encrypted automatically.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| terraform | >= 0.12.20 |
| azurerm | >= 2.0.0 |

## Providers

| Name | Version |
|------|---------|
| azurerm | >= 1.42.0, < 2.0.0 |
| azurerm | >= 2.0.0 |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:-----:|
|------|-------------|------|---------|:--------:|
| additional\_capabilities\_ultra\_ssd\_enabled | Should Ultra SSD disk be enabled for this Virtual Machine? | `bool` | `false` | no |
| additional\_unattend\_config\_content | Specifies the base-64 encoded XML formatted content that is added to the unattend.xml file for the specified path and component. | `string` | `""` | no |
| additional\_unattend\_config\_setting\_name | Specifies the name of the setting to which the content applies. Possible values are: FirstLogonCommands and AutoLogon. | `string` | `"FirstLogonCommands"` | no |


+ 8
- 3
examples/default/README.md View File

@@ -5,23 +5,28 @@ This will create basic resources.
## Usage
```
terraform init
terraform plan
terraform apply
terraform destroy
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| azurerm | >= 2.0.0 |

## Providers

| Name | Version |
|------|---------|
| azurerm | >= 1.42.0, < 2.0.0 |
| azurerm | >= 2.0.0 |
| random | n/a |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:-----:|
|------|-------------|------|---------|:--------:|
| client\_id | n/a | `string` | n/a | yes |
| client\_secret | n/a | `string` | n/a | yes |
| subscription\_id | n/a | `string` | n/a | yes |


+ 2
- 1
examples/default/providers.tf View File

@@ -1,5 +1,6 @@
provider "azurerm" {
version = ">= 1.42.0, < 2.0.0"
features {}
version = ">= 2.0.0"
client_id = var.client_id
client_secret = var.client_secret
subscription_id = var.subscription_id


+ 7
- 2
examples/disabled/README.md View File

@@ -5,12 +5,17 @@ This should not create any resources.
## Usage
```
terraform init
terraform plan
terraform apply
terraform destroy
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| azurerm | >= 2.0.0 |

## Providers

No provider.
@@ -18,7 +23,7 @@ No provider.
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:-----:|
|------|-------------|------|---------|:--------:|
| client\_id | n/a | `string` | n/a | yes |
| client\_secret | n/a | `string` | n/a | yes |
| subscription\_id | n/a | `string` | n/a | yes |


+ 2
- 1
examples/disabled/providers.tf View File

@@ -1,5 +1,6 @@
provider "azurerm" {
version = ">= 1.42.0, < 2.0.0"
features {}
version = ">= 2.0.0"
client_id = var.client_id
client_secret = var.client_secret
subscription_id = var.subscription_id


+ 8
- 3
examples/external-resources/README.md View File

@@ -5,23 +5,28 @@ This will use externally created resources.
## Usage
```
terraform init
terraform plan
terraform apply
terraform destroy
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| azurerm | >= 2.0.0 |

## Providers

| Name | Version |
|------|---------|
| azurerm | >= 1.42.0, < 2.0.0 |
| azurerm | >= 2.0.0 |
| random | n/a |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:-----:|
|------|-------------|------|---------|:--------:|
| client\_id | n/a | `string` | n/a | yes |
| client\_secret | n/a | `string` | n/a | yes |
| subscription\_id | n/a | `string` | n/a | yes |


+ 2
- 1
examples/external-resources/providers.tf View File

@@ -1,5 +1,6 @@
provider "azurerm" {
version = ">= 1.42.0, < 2.0.0"
features {}
version = ">= 2.0.0"
client_id = var.client_id
client_secret = var.client_secret
subscription_id = var.subscription_id


+ 8
- 3
examples/x-vms-x-disks-x-netint/README.md View File

@@ -5,23 +5,28 @@ This will create multiple VMs with multiple network interfaces, with multiple da
## Usage
```
terraform init
terraform plan
terraform apply
terraform destroy
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| azurerm | >= 2.0.0 |

## Providers

| Name | Version |
|------|---------|
| azurerm | >= 1.42.0, < 2.0.0 |
| azurerm | >= 2.0.0 |
| random | n/a |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:-----:|
|------|-------------|------|---------|:--------:|
| client\_id | n/a | `string` | n/a | yes |
| client\_secret | n/a | `string` | n/a | yes |
| subscription\_id | n/a | `string` | n/a | yes |


+ 2
- 1
examples/x-vms-x-disks-x-netint/providers.tf View File

@@ -1,5 +1,6 @@
provider "azurerm" {
version = ">= 1.42.0, < 2.0.0"
features {}
version = ">= 2.0.0"
client_id = var.client_id
client_secret = var.client_secret
subscription_id = var.subscription_id


+ 242
- 120
main.tf View File

@@ -1,6 +1,7 @@
locals {
should_create_availability_set = var.enabled && var.availability_set_enabled && ! var.availability_set_exists
should_create_network_interface = var.enabled && var.network_interface_enabled && ! var.network_interface_exists && var.vm_count > 0
data_disk_attachement = var.enabled && (var.vm_count > 0 || var.vm_exists)
storage_os_disk_name = var.storage_os_disk_name != "" ? var.storage_os_disk_name : var.name
supports_encryption_set = var.resource_group_location == "eastus2" || var.resource_group_location == "canadacentral" || var.resource_group_location == "westcentralus" || var.resource_group_location == "northeurope"
}
@@ -18,6 +19,7 @@ resource "azurerm_availability_set" "this" {

platform_update_domain_count = var.availability_set_platform_update_domain_count
platform_fault_domain_count = var.availability_set_platform_fault_domain_count
proximity_placement_group_id = var.availability_set_proximity_placement_group_id

managed = var.availability_set_managed

@@ -35,22 +37,23 @@ resource "azurerm_availability_set" "this" {
###

resource "azurerm_network_interface" "this" {
count = local.should_create_network_interface ? var.network_interface_count * var.vm_count : 0
count = local.should_create_network_interface ? var.network_interface_count : 0

name = var.network_interface_count * var.vm_count > 0 ? format("%s%0${var.num_suffix_digits}d", element(var.network_interface_names, count.index % var.network_interface_count), count.index + 1) : element(var.network_interface_names, count.index % var.network_interface_count)
name = var.network_interface_count > 0 ? element(var.network_interface_names, count.index) : null
location = var.resource_group_location
resource_group_name = var.resource_group_name

network_security_group_id = element(var.network_interface_network_security_group_ids, count.index % var.network_interface_count)
internal_dns_name_label = var.network_interface_count * var.vm_count > 0 ? format("%s%0${var.num_suffix_digits}d", element(var.network_interface_internal_dns_name_labels, count.index % var.network_interface_count), count.index + 1) : element(var.network_interface_internal_dns_name_labels, count.index % var.network_interface_count)
enable_ip_forwarding = element(var.network_interface_enable_ip_forwardings, count.index % var.network_interface_count)
enable_accelerated_networking = element(var.network_interface_enable_accelerated_networkings, count.index % var.network_interface_count)
dns_servers = element(var.network_interface_dns_servers, count.index % var.network_interface_count)
internal_dns_name_label = var.network_interface_count > 0 ? element(var.network_interface_internal_dns_name_labels, count.index) : null
enable_ip_forwarding = element(var.network_interface_enable_ip_forwardings, count.index)
enable_accelerated_networking = element(var.network_interface_enable_accelerated_networkings, count.index)
dns_servers = element(var.network_interface_dns_servers, count.index)

ip_configuration {
name = element(var.network_interface_ip_configuration_names, count.index)
subnet_id = element(var.network_interface_ip_configuration_subnet_ids, count.index)
private_ip_address = element(var.network_interface_ip_configuration_private_ip_addresses, count.index)
primary = element(var.network_interface_ip_configuration_primary, count.index)
public_ip_address_id = element(var.network_interface_ip_configuration_public_ip_address_ids, count.index)
subnet_id = element(var.network_interface_ip_configuration_private_ip_address_versions, count.index) == "IPv4" ? element(var.network_interface_ip_configuration_subnet_ids, count.index) : null
private_ip_address = element(var.network_interface_ip_configuration_private_ip_address_allocations, count.index) == "Static" ? element(var.network_interface_ip_configuration_private_ip_addresses, count.index) : null
private_ip_address_allocation = element(var.network_interface_ip_configuration_private_ip_address_allocations, count.index)
private_ip_address_version = element(var.network_interface_ip_configuration_private_ip_address_versions, count.index)
}
@@ -68,7 +71,6 @@ resource "azurerm_network_interface_application_security_group_association" "thi
count = var.enabled ? var.network_interface_application_security_group_count * var.network_interface_count : 0

network_interface_id = element((var.network_interface_exists ? data.azurerm_network_interface.this.*.id : azurerm_network_interface.this.*.id), element(var.network_interface_application_security_group_ids, count.index).network_interface_index + floor(count.index / var.network_interface_count))
ip_configuration_name = element(var.network_interface_ip_configuration_names, element(var.network_interface_application_security_group_ids, count.index).network_interface_index + floor(count.index / var.network_interface_count))
application_security_group_id = element(var.network_interface_application_security_group_ids, count.index).application_security_group_id
}

@@ -76,7 +78,7 @@ resource "azurerm_network_interface_application_gateway_backend_address_pool_ass
count = var.enabled ? var.network_interface_application_gateway_backend_address_pool_count * var.network_interface_count : 0

network_interface_id = element((var.network_interface_exists ? data.azurerm_network_interface.this.*.id : azurerm_network_interface.this.*.id), element(var.network_interface_application_gateway_backend_address_pool_ids, count.index).network_interface_index + floor(count.index / var.network_interface_count))
ip_configuration_name = element(var.network_interface_ip_configuration_names, element(var.network_interface_application_security_group_ids, count.index).network_interface_index + floor(count.index / var.network_interface_count))
ip_configuration_name = element(var.network_interface_ip_configuration_names, count.index)
backend_address_pool_id = element(var.network_interface_application_gateway_backend_address_pool_ids, count.index).application_gateway_backend_address_pool_id
}

@@ -84,7 +86,7 @@ resource "azurerm_network_interface_backend_address_pool_association" "this" {
count = var.enabled ? var.network_interface_backend_address_pool_count * var.network_interface_count : 0

network_interface_id = element((var.network_interface_exists ? data.azurerm_network_interface.this.*.id : azurerm_network_interface.this.*.id), element(var.network_interface_backend_address_pool_ids, count.index).network_interface_index + floor(count.index / var.network_interface_count))
ip_configuration_name = element(var.network_interface_ip_configuration_names, element(var.network_interface_backend_address_pool_ids, count.index).network_interface_index + floor(count.index / var.network_interface_count))
ip_configuration_name = element(var.network_interface_ip_configuration_names, count.index)
backend_address_pool_id = element(var.network_interface_backend_address_pool_ids, count.index).backend_address_pool_id
}

@@ -92,7 +94,7 @@ resource "azurerm_network_interface_nat_rule_association" "this" {
count = var.enabled ? var.network_interface_nat_rule_association_count * var.network_interface_count : 0

network_interface_id = element((var.network_interface_exists ? data.azurerm_network_interface.this.*.id : azurerm_network_interface.this.*.id), element(var.network_interface_nat_rule_association_ids, count.index).network_interface_index + floor(count.index / var.network_interface_count))
ip_configuration_name = element(var.network_interface_ip_configuration_names, element(var.network_interface_nat_rule_association_ids, count.index).network_interface_index + floor(count.index / var.network_interface_count))
ip_configuration_name = element(var.network_interface_ip_configuration_names, count.index)
nat_rule_id = element(var.network_interface_nat_rule_association_ids, count.index).nat_rule_id
}

@@ -109,111 +111,206 @@ resource "azurerm_marketplace_agreement" "this" {
}

###
# Virtual Machine
# Windows virtual Machine
###

resource "azurerm_virtual_machine" "this" {
resource "azurerm_windows_virtual_machine" "this" {
count = var.enabled ? var.vm_count : 0

license_type = var.vm_type == "Windows" ? var.license_type : null

name = var.vm_count > 1 ? format("%s%0${var.num_suffix_digits}d", var.name, count.index + 1) : var.name
location = var.resource_group_location
resource_group_name = var.resource_group_name
network_interface_ids = element(chunklist((var.network_interface_exists ? data.azurerm_network_interface.this.*.id : azurerm_network_interface.this.*.id), var.network_interface_count), count.index)
primary_network_interface_id = var.network_interface_exists ? data.azurerm_network_interface.this.*.id[count.index * var.network_interface_count] : azurerm_network_interface.this.*.id[count.index * var.network_interface_count]
vm_size = var.vm_size

delete_os_disk_on_termination = var.delete_os_disk_on_termination
delete_data_disks_on_termination = var.delete_data_disks_on_termination

availability_set_id = var.availability_set_enabled ? (var.availability_set_exists ? data.azurerm_availability_set.this.*.id[0] : concat(azurerm_availability_set.this.*.id, [""])[0]) : ""
name = element(var.vm_names, count.index)
size = var.vm_size
zone = var.zones
location = var.resource_group_location
resource_group_name = var.resource_group_name
admin_username = element(var.windows_admin_usernames, count.index)
admin_password = element(var.windows_admin_passwords, count.index)
network_interface_ids = var.vm_count > 1 ? element(slice((var.network_interface_exists ? data.azurerm_network_interface.this.*.id : azurerm_network_interface.this.*.id), element(var.network_interface_start_index, count.index), element(var.network_interface_end_index, count.index)), count.index) : concat(azurerm_network_interface.this.*.id, list(""))
allow_extensions_operations = true
timezone = var.windows_timezone
priority = var.priority
custom_data = element(var.custom_data, count.index)
license_type = element(var.windows_license_types, count.index)
computer_name = element(var.computer_name, count.index)
max_bid_price = var.priority == "Spot" ? element(var.windows_max_bid_prices, count.index) : ""
eviction_policy = var.priority == "Spot" ? element(var.windows_eviction_policies, count.index) : ""
source_image_id = var.source_image_id
dedicated_host_id = element(var.dedicated_host_ids, count.index)
availability_set_id = var.availability_set_enabled ? (var.availability_set_exists ? data.azurerm_availability_set.this.*.id[0] : concat(azurerm_availability_set.this.*.id, list(""))[0]) : ""
provision_vm_agent = var.provision_vm_agent
source_image_reference = var.source_image_reference
enable_automatic_updates = element(var.windows_enable_automatic_updates, count.index)

additional_capabilities {
ultra_ssd_enabled = var.additional_capabilities_ultra_ssd_enabled
}

boot_diagnostics {
enabled = var.boot_diagnostics_enabled
storage_uri = var.boot_diagnostics_storage_uri
dynamic "additional_unattend_content" {
for_each = var.additional_unattend_content_seeting != "" ? [1] : []

content {
content = var.additional_unattend_content_content
setting = var.additional_unattend_content_seeting
}
}

dynamic "storage_image_reference" {
for_each = var.storage_image_reference_id != "" ? [1] : []
dynamic "boot_diagnostics" {
for_each = var.boot_diagnostics_storage_account_uri != "" ? [1] : []

content {
id = var.storage_image_reference_id
storage_account_uri = var.boot_diagnostics_storage_account_uri
}
}

dynamic "storage_image_reference" {
for_each = var.storage_image_reference_publisher != "" ? [1] : []
dynamic "identity" {
for_each = element(var.identity_types, count.index) != "" ? [1] : []

content {
publisher = var.storage_image_reference_publisher
offer = var.storage_image_reference_offer
sku = var.storage_image_reference_sku
version = var.storage_image_reference_version
type = element(var.identity_types, count.index)
identity_ids = element(var.identity_identities_ids, count.index)
}
}

storage_os_disk {
name = var.vm_count > 1 ? format("%s%0${var.num_suffix_digits}d", local.storage_os_disk_name, count.index + 1) : local.storage_os_disk_name
caching = var.storage_os_disk_caching
create_option = var.storage_os_disk_create_option
disk_size_gb = var.storage_os_disk_size_gb
managed_disk_id = var.storage_os_disk_create_option == "Attach" ? element(azurerm_managed_disk.this_os.*.id, count.index) : null
managed_disk_type = var.storage_os_managed_disk_type
write_accelerator_enabled = var.storage_os_write_accelerator_enabled
os_type = var.vm_type == "Windows" ? "Windows" : "Linux"
dynamic "os_disk" {
for_each = var.os_disk_caching != "" ? [1] : []

content {
name = var.os_disk_name
caching = var.os_disk_caching
storage_account_type = var.os_disk_storage_account_type
disk_encryption_set_id = var.os_disk_encryption_set_id
disk_size_gb = var.os_disk_size_gb
write_accelerator_enabled = var.os_disk_storage_account_type == "Premium_LRS" && var.os_disk_caching == "None" ? true : false

dynamic "diff_disk_settings" {
for_each = var.diff_disk_settings_option != "" ? [1] : []

content {
option = var.diff_disk_settings_option
}
}
}
}

os_profile {
computer_name = var.vm_type == "Windows" ? substr((var.vm_count > 1 ? format("%s%0${var.num_suffix_digits}d", var.name, count.index + 1) : var.name), -15, 15) : var.vm_count > 1 ? format("%s%0${var.num_suffix_digits}d", var.name, count.index + 1) : var.name
admin_username = var.os_profile_admin_username
admin_password = var.os_profile_admin_password
custom_data = var.os_profile_custom_data
dynamic "plan" {
for_each = var.plan_name != "" ? [1] : []

content {
name = var.plan_name
product = var.plan_product
publisher = var.plan_publisher
}
}

dynamic "os_profile_linux_config" {
for_each = var.vm_type == "Linux" ? [1] : []
dynamic "secret" {
for_each = var.secret_key_vault_id != "" ? [1] : []

content {
disable_password_authentication = var.os_profile_linux_config_disable_password_authentication
key_vault_id = var.secret_key_vault_id

dynamic "ssh_keys" {
for_each = var.os_profile_linux_config_ssh_keys
dynamic "certificate" {
for_each = var.certificate_store != "" ? [1] : []

content {
key_data = ssh_keys.value.key_data
path = format("/home/%s/.ssh/authorized_keys", var.os_profile_admin_username)
store = var.certificate_store
url = var.certificate_url
}
}
}
}

dynamic "os_profile_windows_config" {
for_each = var.vm_type == "Windows" ? [1] : []
winrm_listener {
Protocol = var.winrm_listener_protocol
cerificate_url = var.winrm_listener_certificate_url
}


tags = merge(
var.tags,
var.windows_vm_tags,
{
Terraform = "true"
},
)


}


###
# Linux virtual machine
###

resource "azurerm_linux_virtual_machine" "this" {
count = var.linux_vm_enabled ? var.vm_count : 0

name = element(var.vm_names, count.index)
zone = var.zones
location = var.resource_group_location
resource_group_name = var.resource_group_name
admin_username = var.linux_admin_usernames
admin_password = var.linux_admin_passwords
network_interface_ids = var.vm_count > 1 ? element(slice((var.network_interface_exists ? data.azurerm_network_interface.this.*.id : azurerm_network_interface.this.*.id), element(var.network_interface_start_index, count.index), element(var.network_interface_end_index, count.index)), count.index) : concat(azurerm_network_interface.this.*.id, list(""))
allow_extensions_operations = true
priority = var.priority
custom_data = var.custom_data
computer_name = var.computer_names
max_bid_price = var.priority == "Spot" ? var.linux_max_bid_prices : ""
eviction_policy = var.priority == "Spot" ? var.linux_eviction_policies : ""
source_image_id = var.source_image_id
dedicated_host_id = var.dedicated_host_ids
provision_vm_agent = var.provision_vm_agent
availability_set_id = var.availability_set_enabled ? (var.availability_set_exists ? data.azurerm_availability_set.this.*.id[0] : concat(azurerm_availability_set.this.*.id, list(""))[0]) : ""
proximity_placement_group_id = var.proximity_placement_group_id
disable_password_authentication = var.linux_admin_password == "" ? true : false

additional_capabilities {
ultra_ssd_enabled = var.additional_capabilities_ultra_ssd_enabled
}

dynamic "admin_ssh_key" {
for_each = var.admin_passwords == null ? var.admin_ssh_key : {}

content {
provision_vm_agent = var.os_profile_windows_config_provision_vm_agent
enable_automatic_upgrades = var.os_profile_windows_config_enable_automatic_upgrades
timezone = var.os_profile_windows_config_timezone
public_key = var.admin_ssh_key.value.public_key
username = var.admin_ssh_key.value.username
}
}

dynamic "additional_unattend_config" {
for_each = var.additional_unattend_config_content != "" ? [1] : []
dynamic "boot_diagnostics" {
for_each = var.boot_diagnostics_storage_account_uri != "" ? [1] : []

content {
storage_account_uri = var.boot_diagnostics_storage_account_uri
}
}

dynamic "identity" {
for_each = element(var.identity_types, count.index) != "" ? [1] : []

content {
type = element(var.identity_types, count.index)
identity_ids = element(var.identity_identities_ids, count.index)
}
}

dynamic "os_disk" {
for_each = var.os_disk_caching != "" ? [1] : []

content {
name = var.os_disk_name
caching = var.os_disk_caching
storage_account_type = var.os_disk_storage_account_type
disk_encryption_set_id = var.os_disk_encryption_set_id
disk_size_gb = var.os_disk_size_gb
write_accelerator_enabled = var.os_disk_storage_account_type == "Premium_LRS" && var.os_disk_caching == "None" ? true : false

dynamic "diff_disk_settings" {
for_each = var.diff_disk_settings_option != "" ? [1] : []

content {
pass = "oobeSystem"
component = "Microsoft-Windows-Shell-Setup"
setting_name = var.additional_unattend_config_setting_name
content = var.additional_unattend_config_content
option = var.diff_disk_settings_option
}
}

winrm {
protocol = var.winrm_protocol
certificate_url = var.winrm_certificate_url
}
}
}

@@ -222,23 +319,22 @@ resource "azurerm_virtual_machine" "this" {

content {
name = var.plan_name
publisher = var.plan_publisher
product = var.plan_product
publisher = var.plan_publisher
}
}

dynamic "os_profile_secrets" {
for_each = var.os_profile_secrets_source_vault_id != "" ? [1] : []
dynamic "secret" {
for_each = var.secret_key_vault_id != "" ? [1] : []

content {
source_vault_id = var.os_profile_secrets_source_vault_id
key_vault_id = var.secret_key_vault_id

dynamic "vault_certificates" {
for_each = var.os_profile_secrets_vault_certificates
dynamic "certificate" {
for_each = var.certificate_url != "" ? [1] : []

content {
certificate_url = vault_certificates.value.certificate_url
certificate_store = vault_certificates.value.certificate_store
url = var.certificate_url
}
}
}
@@ -246,34 +342,61 @@ resource "azurerm_virtual_machine" "this" {

tags = merge(
var.tags,
var.vm_tags,
var.windows_vm_tags,
{
Terraform = "true"
},
)
}


###
# Managed Disks
###

resource "azurerm_managed_disk" "this_os" {
count = var.enabled && var.storage_os_disk_create_option == "Attach" ? var.vm_count : 0
resource "azurerm_managed_disk" "this" {
count = var.enabled && var.managed_disk_count > 0 ? var.managed_disk_count : 0

location = var.resource_group_location
resource_group_name = var.resource_group_name

name = var.vm_count > 0 ? format("%s%0${var.num_suffix_digits}d", local.storage_os_disk_name, count.index + 1) : local.storage_os_disk_name
storage_account_type = var.storage_os_managed_disk_type
disk_size_gb = var.storage_os_disk_size_gb
name = element(var.managed_disk_names, count.index)
storage_account_type = element(var.managed_disk_storage_account_types, count.index)
disk_size_gb = element(var.managed_disk_size_gbs, count.index)

create_option = var.storage_os_disk_managed_disk_create_option
create_option = element(var.managed_disk_create_options, count.index)

image_reference_id = var.storage_os_disk_managed_disk_create_option == "FromImage" ? data.azurerm_platform_image.this_os.*.id[0] : null
source_resource_id = var.storage_os_disk_managed_disk_create_option == "Copy" ? var.storage_os_disk_managed_disk_source_resource_id : null
source_uri = var.storage_os_disk_managed_disk_create_option == "Import" ? var.storage_os_disk_managed_disk_source_uri : null
image_reference_id = element(var.managed_disk_create_options, count.index) == "FromImage" ? element(var.managed_disk_image_reference_ids, count.index) : null
source_resource_id = element(var.managed_disk_create_options, count.index) == "Copy" ? element(var.managed_disk_source_resource_ids, count.index) : null
source_uri = element(var.managed_disk_create_options, count.index) == "Import" ? element(var.managed_disk_source_uris, count.index) : null

os_type = var.vm_type == "Windows" ? "Windows" : "Linux"
os_type = element(var.managed_disk_os_types, count.index)

dynamic "encryption_settings" {
for_each = var.managed_disk_encryption_settings_enabled ? [1] : []

content {
enabled = var.managed_disk_encryption_settings_enabled

dynamic "disk_encryption_key" {
for_each = var.managed_disk_encryption_key_secret_url != "" ? [1] : []

content {
secret_url = var.managed_disk_encryption_key_secret_url
source_valut_id = var.managed_disk_encryption_key_source_vault_id
}
}

dynamic "key_encryption_key" {
for_each = var.managed_disk_key_encryption_key_source_valut_id != "" ? [1] : []

content = {
key_url = var.managed_disk_key_encryption_key_key_url
source_vault_id = var.managed_disk_key_encryption_key_source_valut_id
}
}
}
}

tags = merge(
var.tags,
@@ -284,40 +407,39 @@ resource "azurerm_managed_disk" "this_os" {
)
}

resource "azurerm_managed_disk" "this" {
count = var.enabled && var.managed_disk_count > 0 ? var.managed_disk_count * var.vm_count : 0
resource "azurerm_virtual_machine_data_disk_attachment" "this" {
count = local.data_disk_attachement ? var.managed_disk_count : 0

location = var.resource_group_location
resource_group_name = var.resource_group_name
managed_disk_id = element(azurerm_managed_disk.this.*.id, count.index)
virtual_machine_id = var.vm_type == "Windows" ? element(compact(concat(azurerm_windows_virtual_machine.this.*.id, var.exisiting_vm_ids)), count.index) : element(compact(concat(azurerm_linux_virtual_machine.this.*.id, var.exisiting_vm_ids)), count.index)

name = var.managed_disk_count * var.vm_count > 0 ? format("%s%0${var.num_suffix_digits}d", element(var.managed_disk_names, floor(count.index / var.vm_count) % var.managed_disk_count), count.index) : element(var.managed_disk_names, floor(count.index / var.vm_count) % var.managed_disk_count)
storage_account_type = element(var.managed_disk_storage_account_types, floor(count.index / var.vm_count) % var.managed_disk_count)
disk_size_gb = element(var.managed_disk_size_gbs, floor(count.index / var.vm_count) % var.managed_disk_count)
lun = count.index
caching = element(var.managed_disk_cachings, count.index)
create_option = element(var.managed_disk_create_options, count.index)
write_accelerator_enabled = element(var.managed_disk_write_accelerator_enableds, count.index)
}

create_option = element(var.managed_disk_create_options, floor(count.index / var.vm_count) % var.managed_disk_count)
###
# Virtual machine extensions
###

image_reference_id = element(var.managed_disk_create_options, floor(count.index / var.vm_count) % var.managed_disk_count) == "FromImage" ? element(var.managed_disk_image_reference_ids, floor(count.index / var.vm_count) % var.managed_disk_count) : null
source_resource_id = element(var.managed_disk_create_options, floor(count.index / var.vm_count) % var.managed_disk_count) == "Copy" ? element(var.managed_disk_source_resource_ids, floor(count.index / var.vm_count) % var.managed_disk_count) : null
source_uri = element(var.managed_disk_create_options, floor(count.index / var.vm_count) % var.managed_disk_count) == "Import" ? element(var.managed_disk_source_uris, floor(count.index / var.vm_count) % var.managed_disk_count) : null
resource "azurerm_virtual_machine_extension" "this_extension" {
count = var.enabled && var.vm_extensions_enabled ? length(var.vm_extension_names) : 0

os_type = element(var.managed_disk_os_types, floor(count.index / var.vm_count) % var.managed_disk_count)
name = element(var.vm_extension_names, count.index)
type = element(var.vm_extension_types, count.index)
settings = element(var.vm_extension_settings, count.index)
publisher = element(var.vm_extension_publishers, count.index)
protected_settings = elementt(var.vm_extension_protected_settings, count.index)
virtual_machine_id = var.vm_type == "Windows" ? element(azurem_windows_virtual_machine.this.*.id, count.index) : element(azurerm_linux_virtual_machine.this.*.id, count.index)
type_handler_version = element(var.vm_extension_type_handler_versions, count.index)
auto_upgrade_minor_version = element(var.vm_extension_auto_upgarde_minor_version, count.index)

tags = merge(
var.tags,
var.managed_disk_tags,
var.vm_extension_tags,
{
Terraform = "true"
},
)
}

resource "azurerm_virtual_machine_data_disk_attachment" "this" {
count = var.enabled && var.vm_count > 0 ? var.managed_disk_count * var.vm_count : 0

managed_disk_id = azurerm_managed_disk.this.*.id[count.index]
virtual_machine_id = azurerm_virtual_machine.this.*.id[count.index % var.vm_count]

lun = count.index
caching = element(var.managed_disk_cachings, floor(count.index / var.vm_count) % var.managed_disk_count)
write_accelerator_enabled = element(var.managed_disk_write_accelerator_enableds, floor(count.index / var.vm_count) % var.managed_disk_count)
}

+ 1
- 1
versions.tf View File

@@ -2,6 +2,6 @@ terraform {
required_version = ">= 0.12.20"

required_providers {
azurerm = ">= 1.42.0, < 2.0.0"
azurerm = ">= 2.0.0"
}
}

Loading…
Cancel
Save