hashicorp / terraform-provider-cloudinit

Utility provider that exposes the cloudinit_config data source which renders a multipart MIME configuration for use with cloud-init (previously available as the template_cloudinit_config resource in the template provider)
https://registry.terraform.io/providers/hashicorp/cloudinit/latest
Mozilla Public License 2.0
103 stars 17 forks source link

`#cloud-init` should be the very first line in a multipart configuration #257

Open hahuang65 opened 3 months ago

hahuang65 commented 3 months ago

Terraform CLI and Provider Versions

Terraform v1.4.4 on darwin_arm64

Use Cases or Problem Statement

When using a multipart cloudinit_config such as

data "cloudinit_config" "rails" {
  gzip          = true
  base64_encode = true

  part {
    content    = file("files/user_data/parts/base.yaml")
    merge_type = "list(append)+dict(recurse_array)+str()"
  }

  part {
    content    = file("files/user_data/parts/rails.yaml")
    merge_type = "list(append)+dict(recurse_array)+str()"
  }

  part {
    content    = file("files/user_data/parts/users.yaml")
    merge_type = "list(append)+dict(recurse_array)+str()"
  }
}

it ends up generating a /var/lib/cloud/instance/user-data.txt file that starts with

--MIMEBOUNDARY
Content-Transfer-Encoding: 7bit
Content-Type: text/plain
Mime-Version: 1.0
X-Merge-Type: list(append)+dict(recurse_array)+str()

#cloud-config

This causes cloud-init schema --system --annotate to complain:

# E1: File None needs to begin with "#cloud-config"

The other issue is that merge_type = "list(append)+dict(recurse_array)+str()" doesn't seem to do anything for the parts besides the first.

I had to stick merge_type = "list(append)+dict(recurse_array)+str()" in my part YAML files for cloudinit to not overwrite previous parts.

Proposal

Would it be possible to move the #cloud-init above the --MIMEBOUNDARY, or add an additional one there?

I'm not sure what the feasible solutions might be.

How much impact is this issue causing?

Medium

Additional Information

This was discovered (both problems of the #cloud-init comment, and the merge_type = "list(append)+dict(recurse_array)+str()" not being inserted into each part) when troubleshooting upgrading from Amazon Linux 2 to Amazon Linux 2023 (cloud-init v19 to v22) here

https://github.com/canonical/cloud-init/issues/5533

You'll see that the resolution for that issue was that I needed to manually put merge_type = "list(append)+dict(recurse_array)+str()" into each of my user_data/parts/<part>.yaml, even though I had it in my Terraform block parts:

  part {
    content    = file("files/user_data/parts/base.yaml")
    merge_type = "list(append)+dict(recurse_array)+str()"
  }

Code of Conduct