3

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 }}"

1 Answer 1

1

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 }}" ]
Sign up to request clarification or add additional context in comments.

9 Comments

The init stage went fine, but it now went wrong with the apply with the following error message : Invalid value for input variable\n\n on terraform.tfvars line 22:\n 22: vnet_cidr = \"10.0.0.0/16\"\n\nThe given value is not valid for variable \"vnet_cidr\": list of
May you please give an example of Ansible variable values for vnet_cidr, subnet1_cidr and subnet2_cidr?
Ansible group_var: 'GV_Virtual_Network_Cidr: 10.0.0.0/16' is vnet_cidr
Do you have vnet_cidr variable defined in Ansible or you use variable GV_Virtual_Network_Cidr in Ansible?
GV_Virtual_Network_Cidr is the Ansible variable. Vnet_cidr is the terraform variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.