diff options
author | James Falcon <james.falcon@canonical.com> | 2022-09-14 10:03:31 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-14 09:03:31 -0600 |
commit | 197cc8ccb9d2b9856456ed2e2a5cb4748df5ec0f (patch) | |
tree | 294adaec5129c6823bfff099e7172c5c1167fbeb /cloudinit/stages.py | |
parent | b861ea8a5e1fd0eb33096f60f54eeff42d80d3bd (diff) | |
download | cloud-init-git-197cc8ccb9d2b9856456ed2e2a5cb4748df5ec0f.tar.gz |
Allow jinja templating in /etc/cloud (SC-1170) (#1722)
There have been multiple requests to allow jinja templating in
/etc/cloud configs the same way we allow jinja templating in vendordata
and userdata. This commit allows for templating both
/etc/cloud/cloud.cfg and any file in /etc/cloud/cloud.cfg.d. The same
instance data used for substitution in vendordata and userdata will be
used here.
Note that these configs get loaded multiple times during the lifetime
of cloud-init, and during cloud-init's earlier loads, instance data
is not yet available.
LP: #1913461
Diffstat (limited to 'cloudinit/stages.py')
-rw-r--r-- | cloudinit/stages.py | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/cloudinit/stages.py b/cloudinit/stages.py index 132dd83b..cfce0dad 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -234,11 +234,14 @@ class Init(object): def _read_cfg(self, extra_fns): no_cfg_paths = helpers.Paths({}, self.datasource) + instance_data_file = no_cfg_paths.get_runpath( + "instance_data_sensitive" + ) merger = helpers.ConfigMerger( paths=no_cfg_paths, datasource=self.datasource, additional_fns=extra_fns, - base_cfg=fetch_base_config(), + base_cfg=fetch_base_config(instance_data_file=instance_data_file), ) return merger.cfg @@ -506,7 +509,7 @@ class Init(object): self._get_ipath(datasource), str(processed_data), 0o600 ) - def _default_handlers(self, opts=None): + def _default_handlers(self, opts=None) -> List[handlers.Handler]: if opts is None: opts = {} @@ -526,11 +529,10 @@ class Init(object): ShellScriptByFreqPartHandler(PER_INSTANCE, **opts), ShellScriptByFreqPartHandler(PER_ONCE, **opts), BootHookPartHandler(**opts), + JinjaTemplatePartHandler( + **opts, sub_handlers=[cloudconfig_handler, shellscript_handler] + ), ] - opts.update( - {"sub_handlers": [cloudconfig_handler, shellscript_handler]} - ) - def_handlers.append(JinjaTemplatePartHandler(**opts)) return def_handlers def _default_userdata_handlers(self): @@ -958,13 +960,15 @@ def read_runtime_config(): return util.read_conf(RUN_CLOUD_CONFIG) -def fetch_base_config(): +def fetch_base_config(*, instance_data_file=None) -> dict: return util.mergemanydict( [ # builtin config, hardcoded in settings.py. util.get_builtin_cfg(), # Anything in your conf.d or 'default' cloud.cfg location. - util.read_conf_with_confd(CLOUD_CONFIG), + util.read_conf_with_confd( + CLOUD_CONFIG, instance_data_file=instance_data_file + ), # runtime config. I.e., /run/cloud-init/cloud.cfg read_runtime_config(), # Kernel/cmdline parameters override system config @@ -972,6 +976,3 @@ def fetch_base_config(): ], reverse=True, ) - - -# vi: ts=4 expandtab |