-
I'm building a template for a Terraform project folder structure that includes some resources by default, and am running into the problem that it has a lot of duplicate parts of the tree. For example, if you have a folder for each hosting environment (like dev, test, staging, prod) then a lot of the content in each of those folders is mostly the same:
This means any change to one of those resources requires finding all the copies. As the template becomes more advanced with if-statements etcetera the number of duplicate folders can grow exponentially. In software engineering you'd refactor any duplicate code into a function, and some MVC frameworks refer to reusable chunks of code as "partials". I'm wondering if it's possible to refactor in Copier by creating template blocks, which can then be reused within the main template? Read the manual about using git submodules, but that requires putting every little "partial" into a separate git repository. This would result in us creating half a dozen or more repo's, and how it all fits together being very hard to understand. Is there any way to use something resembling a
Would much appreciate any solutions. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Have you tried using symlinks? |
Beta Was this translation helpful? Give feedback.
-
You're looking for macros, well... Jinja has macros! https://jinja.palletsprojects.com/en/3.1.x/templates/#macros. In a file called {% macro ecs(...) -%}
...
{%- endmacro %}
{% macro route53(...) -%}
...
{%- endmacro %}
{% macro vpc(...) -%}
...
{%- endmacro %} (the dots are to be replaced with actual parameters/contents) Then in your templated files (that you still have to duplicate everywhere, but that now require no maintenance): {% import "general" as general %}
{{ general.ecs(...) }} {% import "general" as general %}
{{ general.route53(...) }} {% import "general" as general %}
{{ general.vpc(...) }} (again, dots to be replaced with actual arguments) |
Beta Was this translation helpful? Give feedback.
-
So we've been playing around with this for a while and in general the symlinks work well. We didn't go with the suggested macro solution, because the trees aren't always 100% similar. Still need to figure out how we can make a symlinked partial "smart". Right now it assumes every symlinked partial is identical, but that's not really the case. Imagine this simplified example:
We would want to write the region name into a variable in |
Beta Was this translation helpful? Give feedback.
-
https://jinja.palletsprojects.com/en/3.0.x/templates/#template-inheritance explains another way of reusing templates where only a part of the code changes. |
Beta Was this translation helpful? Give feedback.
You're looking for macros, well... Jinja has macros! https://jinja.palletsprojects.com/en/3.1.x/templates/#macros.
In a file called
general
in your repository root:(the dots are to be replaced with actual parameters/contents)
Then in your templated files (that you still have to duplicate everywhere, but that now require no maintenance):
(again, dots to be replaced with actua…