hairyhenderson / gomplate

A flexible commandline tool for template rendering. Supports lots of local and remote datasources.
https://gomplate.ca
MIT License
2.65k stars 181 forks source link

Gomplate not preserving AWS-specific custom YAML tags on merge #1997

Closed greg-caffeine closed 3 months ago

greg-caffeine commented 7 months ago

I would like to use gomplate to allow overriding and merging of sections of CloudFormation YAML templates with other YAML sections defined in other files by using the gomplate merge datasource. Unfortunately, gomplate drops the AWS-specific YAML tags in the merged output, breaking the template.

Example:

cloudfront_template.yaml:

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31
Description: A gomplate test

Parameters:
  MyParameter:
    Type: String

Globals:
  Function:
    Environment:
      Variables:
        MY_PARAMETER: !Ref MyParameter

cloudfront_overrides.yaml:

Globals:
  Function:
    Environment:
      Variables:
        MY_OTHER_PARAMETER: my_other_value

Gomplate Command % gomplate -d 'merger=merge:cloudfront_template.yaml|cloudfront_overrides.yaml' -i '{{ include "merger" }}'

OUTPUT

AWSTemplateFormatVersion: 2010-09-09T00:00:00Z
Description: A gomplate test
Globals:
  Function:
    Environment:
      Variables:
        MY_OTHER_PARAMETER: my_other_value
        MY_PARAMETER: MyParameter
Parameters:
  MyParameter:
    Type: String
Transform: AWS::Serverless-2016-10-31

Note in the output that MY_PARAMETER: !Ref MyParameter has not been preserved and instead has been transformed into MY_PARAMETER: MyParameter, losing the !Ref tag.

Is there any way to get gomplate to preserve all HTML tags in the output, or to provide a list of tags to preserve?

A list of AWS custom tags can be found here.

hairyhenderson commented 6 months ago

Hi @greg-caffeine, sorry for the delay.

Custom YAML tags are something that are going to be pretty difficult to support with gomplate. The main problem is that internally the data is parsed into an intermediate form which is a map with nested maps, and so something like the above would be parsed into (in JSON terms) {"MY_PARAMETER": "MyParameter"}, with no ability to retain custom tags.

You may be able to accomplish this if you instead reframe the approach and build a template out of cloudfront_template.yaml rather than using it as part of a datasource.

hairyhenderson commented 6 months ago

Hmm... There may actually be a possibility to use a different intermediate format, as hinted at by https://github.com/go-yaml/yaml/issues/191#issuecomment-376058792, but it's unclear exactly how. I'd need to find some time to dig into this deeper.

github-actions[bot] commented 4 months ago

This issue is stale because it has been open for 60 days with no activity. If it is no longer relevant or necessary, please close it. Given no action, it will be closed in 14 days.

If it's still relevant, one of the following will remove the stale marking: