We're using YAML format for our Terraform tfvars files. This allows for comments, and is easy to parse by standard utilities. We already were using a shell wrapper, so this was fairly easy to do. Basically we added a yq step to convert the YAML file to a temp JSON file which can be used by Terraform. So far so good.
But we also have quite a few existing .tfvars iles. I used hcl2tojson here to convert those and while that works:
hcl2tojson --strip-string-quotes --with-comment terraform.tfvars
, JSON obviously does not know about comments. I see that they're there though, as a list of dicts like this:
"__comments__": [
{
"value": "VPC"
},
{
"value": "IPv6 related"
},
{
"value": "VPC Flow Logs"
},
{
"value": "VPC Peering"
},
{
"value": "AWS WAF"
},
{
"value": "RDS"
},
etc etc
But I don't see how they can be useful with context? Am I missing something?
My goal is to convert a commented .tfvars file to a commented YAML file.
I know that ruamel.yaml has support for commented YAML files.
I wonder if it is possible for python-hcl2 to export an HCL data structure in a way that ruamel.yaml understands...?
So that eventually one could do something like:
hcl2yaml terraform.tfvars > terraform.tfvars.yaml
thx!!
We're using YAML format for our Terraform tfvars files. This allows for comments, and is easy to parse by standard utilities. We already were using a shell wrapper, so this was fairly easy to do. Basically we added a
yqstep to convert the YAML file to a temp JSON file which can be used by Terraform. So far so good.But we also have quite a few existing
.tfvarsiles. I usedhcl2tojsonhere to convert those and while that works:, JSON obviously does not know about comments. I see that they're there though, as a list of dicts like this:
But I don't see how they can be useful with context? Am I missing something?
My goal is to convert a commented
.tfvarsfile to a commented YAML file.I know that
ruamel.yamlhas support for commented YAML files.I wonder if it is possible for
python-hcl2to export an HCL data structure in a way thatruamel.yamlunderstands...?So that eventually one could do something like:
hcl2yaml terraform.tfvars > terraform.tfvars.yamlthx!!