emrichen
emrichen copied to clipboard
Preserve comments
This appears, at least with the limited testing I've done, to remove comments from my template files. This wouldn't be such a big deal, but there is one specific use case where it is, which is making the user-data file for cloud-init images. The first line of the file, must be #cloud-config. I am not sure if this is a packer requirement, or cloud init or AutoInstall, but it needs to be there none the less. Is it possible to ensure that comments are left in the output file?
Sounds like a rather difficult and laborious exercise. We'd need to switch from yaml to some comment preserving YAML parser such as ruamel.yaml. In addition, our enrich method that performs template expansion (or enrichment in Emrichen lingo) would need to be rewritten to understand ruamel.yaml data structures instead of plain Python data returned by yaml.
For your particular use case, you could use the programmatic interface of Emrichen and write the #cloud-init line manually into the output stream before dumping the enriched output. Something like (NB untested)
t = Template.parse(template_yaml, 'yaml')
c = Context({"your": "vars"})
output_yaml = t.render(c)
with open("cloud-config.yml", "w") as output_file:
output_file.write("#cloud-init\n")
output_file.write(output_yaml)
or even in Bash
echo '#cloud-init' > cloud-config.yml
emrichen cloud-config.in.yml >> cloud-config.yml
or similar. Not pretty but should do the trick.
Yeah those are totally reasonable solutions. I just wanted to make sure I wasn't crazy or missing something. Thanks for the response and the project!
I would second that request, not only comments aren't preserved but also line breaks. It seems ruamel.yaml is a much more robust package than pyYaml. Thinking pratctically, perhaps adding a !Comment tag would solve the issue?
@ogreenz What would a hypothetical !Comment tag do? We can't emit YAML comments with PyYAML anyway.
@akx Yes you're right. It would need to switch from PyYAML to ruamel.yaml.