Terraform does not support evaluating Jinja2 template expressions, which is what you have in your Ansible file.
However, you really have a self-inflicted problem here, because you’re representing what could be data (a mapping from availability zone to subnet name) as code.
You should be able to refactor the Ansible YAML to store the data as actual data, and a separate lookup on that data:
ec2_vpc_subnet_ids:
eu-west-1a: subnet-d821f591
... more here ...
ec2_vpc_subnet_id: "{{ ec2_vpc_subnet_ids[availability_zone] }}"
and then you can read the data into Terraform so you can work with it there too.
By the way, this is legacy syntax obsoleted in Terraform 0.13:
Nowadays, just write this, without the "${ }"
value = yamldecode(file("./defaults/main.yaml"))["ec2_vpc_subnet_id"]
@maxb Nice, thanks !
also have another complexity conditional var
ec2_image_id: "{{
'ami-0ad028173a4e263b3' if ec2_image == 'centos-stream-9' and region == 'eu-west-1' else
'ami-03298425565afe6ff' if ec2_image == 'centos-stream-9' and region == 'us-east-1' else
'ami-007be2411e223714f' if ec2_image == 'centos-stream-9' and region == 'af-south-1' else
'ami-0b29c1244b006c7d1' if region == 'eu-west-1' else
'ami-03969aa2deda7e036' if region == 'us-east-1' else
'ami-0bf44ca2d0ea9ee87' if region == 'af-south-1' }}"
awkward to ask but maybe there is also solution for this ?
I must first admit that I am not very familiar with Ansible, and so this may not be a sensible suggestion, but might it be possible to invert this so that the source of this data is in Terraform and your Terraform configuration generates the Ansible YAML, rather than trying to evaluate the Ansible configuration from inside Terraform?
For example, both of the big Jinja conditional expressions you showed here could in principle be rewritten as a mapping inside Terraform:
This uses Terraform itself to perform the lookups, instead of using Jinja templates. That means that the mapping tables live inside the Terraform configuration instead of in the YAML template, and local.ansible_yaml will be a string containing literal YAML values that don’t require any further expansion to use them:
Elsewhere in your Terraform module you can refer to local.ec2_image_id and local.ec2_vpc_subnet_id to get those isolated values, without the need to decode YAML first to get them (because the data is already in Terraform’s scope.)