Skip to content

Preserve comments (in some way) #302

Description

@dnmvisser

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!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions