I use Ansible and Terraform together for different deployments within MS Azure. Now I want to create a Vnet with Subnets with the module “azurerm_virtual_network and azurerm_subnet”. Only I run into the problem that “Address_space” and “address_prefixes” are defined in terraform as “list (string)” and this is done in Ansible by “string”.
My question is how I get this variable from Ansible to Terraform in the correct form without getting the following message:
“default = \u001b[4m"{{subnet1_cidr}}"\u001b[0m\n\u001b[0m\nThis default value is not compatible with the variable's type constraint: list\nof string”
I get the ansible vars “group_vars” using an ansible role that copies a file called “terraform.tfvars.j2” to “{{playbook_dir}} / terraform / terraform.tfvars ". in the “variables.tf” the variables are loaded as follow
variable "vnet_cidr" {
type = list(string)
default = "{{ [vnet_cidr] | list | to_json }}"
}
variable "subnet1_cidr" {
type = list(string)
default = "{{ [subnet1_cidr] | list | to_json }}"
}
variable "subnet2_cidr" {
type = list(string)
default = "{{ [subnet2_cidr] | list | to_json }}"
}
Ansible Group_Vars,
VNET_Name: AZ-Venet
GV_Virtual_Network_Cidr: 10.0.0.0/16
SNET1_Name: AZ-FrontEnd
GV_Subnet_Cidr1: 10.0.1.0/24
Survey_SNET2_Name: Az-Backend
GV_Subnet_Cidr2: 10.0.2.0/24
Terraform terraform.tfvars.j2 /terraform.tfvars
# Networking
vnet_cidr = "{{ GV_Virtual_Network_Cidr }}"
subnet1_cidr = "{{ GV_Subnet_Cidr1 }}"
subnet2_cidr = "{{ GV_Subnet_Cidr2 }}"