Accommodate template inheritance for nginx confs #740
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
From #688
Whereas #688 enables the injection of snippets, this PR proposes a more powerful alternative enabling users to extend the nginx confs, overriding or augmenting any portion. This PR uses jinja template inheritance, which is probably a more proper tool for the job, and perhaps more straightforward than #688's implementation of hook variables.
Simple example
The PR's very first commit provides the easiest introduction to the concept. It is simple (4 additions and 1 deletion) and enables users to indicate a child template to use for
nginx.conf
. This could meet the aforementioned need to inject a directive in thehttp
context.The user defines the template somewhere like
group_vars/all/main.yml
The user creates the
nginx-includes/nginx.conf.child
template using jinja guidelines.Using
super()
The PR's second commit enables an example of augmenting a jinja block (vs. replacing it) by adding
something additional
plus the original block content viasuper()
. Building on the example above:Base WordPress conf
This note from #688 still applies:
Fewer templates/includes, but still DRY
I moved the content of https.conf.j2 and wordpress_multisite_subdirectories.conf.j2 into wordpress-site.conf.j2 because block definitions enable simple DRY reuse via
{{ self.https() }}
. However, we could have kept these as separate templates, if that would be preferable, calling them like this:Notes
wordpress-site.conf.j2 The child template for
wordpress-site.conf.j2
could be defined for all sites ingroup_vars/all/main.yml
as well:This template could also vary by site, and be defined in
wordpress_sites
instead..child file extension. Child templates don't need to end in
.child
but that may be a helpful convention. It is better to avoid the pattern*.conf.j2
, however, to avoid these templates being templated toincludes.d
on the server.Indentation and formatting. The revised templating also unifies the indentation strategy in the template and finally creates perfectly formatted confs on the server (except for a blank line at end of file that is possible under some circumstances).