Skip to content

Commit

Permalink
Remove Terraform variables
Browse files Browse the repository at this point in the history
The use of Terraform variables introduce some complexity. First, in the
Terraform generated resources some data will be obtained by other resources,
others from variables. Also, this require to expliciting dependencies between
Terraform resources, which can introduce errors in the apply process.
Removed variables files and switched to Terraform resource references where
needed, so Terraform can manage resource dependencies autonomously.
  • Loading branch information
Matteo Cappadonna committed May 8, 2020
1 parent ccb63dd commit 68b672d
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 47 deletions.
2 changes: 0 additions & 2 deletions roles/terraform_confs/defaults/main.yml
Expand Up @@ -3,7 +3,6 @@
# maintain terraform tfstate file synchronized between multiple users
azure_tfstate_resource_group_name: "terraform-tfstate"
azure_tfstate_resources:
- variables
- provider
- resource_group
- storage
Expand All @@ -26,7 +25,6 @@ azure_vnet_gw_name: 'vnet_gw'
azure_lnet_gw: ''
azure_lnet_gw_name: 'lnet_gw'
azure_resources:
- variables
- provider
- resource_group
- storage
Expand Down
5 changes: 2 additions & 3 deletions roles/terraform_confs/templates/availability_set.tf.j2
@@ -1,8 +1,7 @@
resource "azurerm_availability_set" "avset1" {
depends_on = [azurerm_resource_group.{{ azure_resource_group_name }}]
name = "avset1"
location = var.location
resource_group_name = var.resource_group_name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
platform_fault_domain_count = 2
platform_update_domain_count = 2
managed = true
Expand Down
9 changes: 4 additions & 5 deletions roles/terraform_confs/templates/localnetwork_gw.tf.j2
Expand Up @@ -6,18 +6,17 @@
resource "azurerm_local_network_gateway" "{{ localnetworkgw.name }}" {
depends_on = [azurerm_virtual_network_gateway.{{ azure_vnet_gw_name }}]
name = "{{ localnetworkgw.name }}"
resource_group_name = var.resource_group_name
location = var.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location
gateway_address = "{{ localnetworkgw.remote_peer }}"
address_space = ["{{ localnetworkgw.prefix }}"]
}

# {{ localnetworkgw.name }} Connection
resource "azurerm_virtual_network_gateway_connection" "{{ localnetworkgw.name }}_connection" {
depends_on = [azurerm_virtual_network_gateway.{{ azure_vnet_gw_name }}]
name = "{{ localnetworkgw.name }}_connection"
resource_group_name = var.resource_group_name
location = var.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location
type = "IPSec"
virtual_network_gateway_id = azurerm_virtual_network_gateway.{{ azure_vnet_gw_name }}.id
local_network_gateway_id = azurerm_local_network_gateway.{{ localnetworkgw.name }}.id
Expand Down
8 changes: 3 additions & 5 deletions roles/terraform_confs/templates/network.tf.j2
Expand Up @@ -7,19 +7,17 @@
resource "azurerm_virtual_network" "{{ azure_vnet_name }}" {
name = "{{ azure_vnet_name }}"
address_space = ["{{ azure_vnet_addr_space }}"]
location = var.location
resource_group_name = var.resource_group_name
depends_on = [azurerm_resource_group.{{ azure_resource_group_name }}]
location = azurerm_resource_group.{{ azure_resource_group_name }}.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
}

# Subnet creation block(s)
{% for subnet in subnets %}
resource "azurerm_subnet" "{{ subnet.name }}" {
name = "{{ subnet.name }}"
resource_group_name = var.resource_group_name
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
virtual_network_name = azurerm_virtual_network.{{ azure_vnet_name }}.name
address_prefix = "{{ subnet.prefix }}"
depends_on = [azurerm_virtual_network.{{ azure_vnet_name }}]
}

{% endfor -%}
6 changes: 3 additions & 3 deletions roles/terraform_confs/templates/network_sec_group.tf.j2
@@ -1,14 +1,14 @@
resource "azurerm_network_security_group" "{{ azure_network_sec_group_name }}" {
depends_on = [azurerm_resource_group.{{ azure_resource_group_name }}]
name = "{{ azure_network_sec_group_name }}"
location = var.location
resource_group_name = var.resource_group_name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
}

{% if securityrules is defined %}
{% for rule in securityrules %}
resource "azurerm_network_security_rule" "{{ rule.name|lower }}" {
resource_group_name = var.resource_group_name
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
network_security_group_name = azurerm_network_security_group.{{ azure_network_sec_group_name }}.name

name = "{{ rule.name }}"
Expand Down
4 changes: 2 additions & 2 deletions roles/terraform_confs/templates/resource_group.tf.j2
@@ -1,4 +1,4 @@
resource "azurerm_resource_group" "{{ azure_resource_group_name }}" {
name = var.resource_group_name
location = var.location
name = "{{ azure_resource_group_name }}"
location = "{{ azure_location }}"
}
5 changes: 2 additions & 3 deletions roles/terraform_confs/templates/storage.tf.j2
@@ -1,8 +1,7 @@
resource "azurerm_storage_account" "{{ azure_storage_account_name }}" {
depends_on = [azurerm_resource_group.{{ azure_resource_group_name }}]
name = "{{ azure_storage_account_name }}"
resource_group_name = var.resource_group_name
location = var.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location
account_replication_type = "LRS"
account_tier = "Standard"
}
1 change: 0 additions & 1 deletion roles/terraform_confs/templates/storage_container.tf.j2
@@ -1,5 +1,4 @@
resource "azurerm_storage_container" "{{ azure_storage_container_name }}" {
depends_on = [azurerm_resource_group.{{ azure_resource_group_name }}]
name = "{{ azure_storage_container_name }}"
storage_account_name = azurerm_storage_account.{{ azure_storage_account_name }}.name
}
24 changes: 10 additions & 14 deletions roles/terraform_confs/templates/template.tf.j2
@@ -1,4 +1,3 @@
# VM(s) that will be created:
{% for vm in groups[group.key] %}
# {{ vm }} - {{ hostvars[vm].internal_ip }}, {{ hostvars[vm].vm_size }}
{% for disk in hostvars[vm].disks %}
Expand All @@ -11,8 +10,8 @@
# Set primary_ip {{ vm }}
resource "azurerm_network_interface" "vm_nic1_{{ vm }}" {
name = "nic1_{{ vm }}"
resource_group_name = var.resource_group_name
location = var.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location

ip_configuration {
name = "ip1_{{ vm }}"
Expand All @@ -31,9 +30,9 @@ resource "azurerm_network_interface_security_group_association" "{{ vm }}_nic1_t
{% if hostvars[vm].secondary_ip is defined %}
# Set secondary_ip {{ vm }}
resource "azurerm_network_interface" "vm_nic2_{{ vm }}" {
name = "nic2_{{ vm }}"
resource_group_name = var.resource_group_name
location = var.location
name = "nic2_{{ vm }}"
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location

ip_configuration {
name = "ip2_{{ vm }}"
Expand All @@ -51,18 +50,16 @@ resource "azurerm_network_interface" "vm_nic2_{{ vm }}" {

# {{ vm }} resource block
resource "azurerm_virtual_machine" "{{ vm }}" {
depends_on = [azurerm_network_interface.vm_nic1_{{ vm }}]
name = "{{ vm }}"
vm_size = "{{ hostvars[vm].vm_size }}"
resource_group_name = var.resource_group_name
location = var.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location
{% if hostvars[vm].secondary_ip is defined %}
network_interface_ids = [azurerm_network_interface.vm_nic1_{{ vm }}.id, azurerm_network_interface.vm_nic2_{{ vm }}.id]
{% else %}
network_interface_ids = [azurerm_network_interface.vm_nic1_{{ vm }}.id]
{% endif %}
primary_network_interface_id = azurerm_network_interface.vm_nic1_{{ vm }}.id
#availability_set_id = azurerm_availability_set.avset1.id

# Delete data disk at vm deletion
delete_os_disk_on_termination = true
Expand Down Expand Up @@ -94,7 +91,7 @@ resource "azurerm_virtual_machine" "{{ vm }}" {
}

boot_diagnostics {
enabled = true
enabled = true
storage_uri = azurerm_storage_account.{{ azure_storage_account_name }}.primary_blob_endpoint
}
}
Expand All @@ -104,16 +101,15 @@ resource "azurerm_virtual_machine" "{{ vm }}" {
resource "azurerm_managed_disk" "{{ disk.name }}_{{ vm }}" {
depends_on = [azurerm_virtual_machine.{{ vm }}]
name = "{{ disk.name }}_{{ vm }}"
resource_group_name = var.resource_group_name
location = var.location
resource_group_name = azurerm_resource_group.{{ azure_resource_group_name }}.name
location = azurerm_resource_group.{{ azure_resource_group_name }}.location
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "{{ disk.size }}"
}

# {{ vm }} {{ disk.name }} attach disk
resource "azurerm_virtual_machine_data_disk_attachment" "{{ disk.name }}_{{ vm }}" {
depends_on = [azurerm_managed_disk.{{ disk.name }}_{{ vm }}]
managed_disk_id = azurerm_managed_disk.{{ disk.name }}_{{ vm }}.id
virtual_machine_id = azurerm_virtual_machine.{{ vm }}.id
lun = "{{ disk.lun }}"
Expand Down
9 changes: 0 additions & 9 deletions roles/terraform_confs/templates/variables.tf.j2

This file was deleted.

0 comments on commit 68b672d

Please sign in to comment.