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 }}"
if you just need to convert string to list with one item, you can use Ansible filters. Here is an example:
{{ [example_variable] | list | to_json }}
However in your case there would be a bit different configuration.
In variables.tf file you can try to remove default values, as it seems you do not have variables vnet_cidr, subnet1_cidr and subnet2_cidr in Ansible vars.
variables.tf file will look like this:
variable "vnet_cidr" {
type = list(string)
default = []
}
variable "subnet1_cidr" {
type = list(string)
default = []
}
variable "subnet2_cidr" {
type = list(string)
default = []
}
Then file terraform.tfvars.j2 /terraform.tfvars will look like this:
# Networking
vnet_cidr = [ "{{ GV_Virtual_Network_Cidr }}" ]
subnet1_cidr = [ "{{ GV_Subnet_Cidr1 }}" ]
subnet2_cidr = [ "{{ GV_Subnet_Cidr2 }}" ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With