From c835e537fb714359a54599de2c9e53dc999eed95 Mon Sep 17 00:00:00 2001 From: sticky-note Date: Wed, 28 Dec 2022 15:46:20 +1100 Subject: [PATCH] feat(servers_config): passes context to custom `source_path` templates --- nginx/config.sls | 4 +--- nginx/servers_config.sls | 4 +--- pillar.example | 16 +++++++++++----- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/nginx/config.sls b/nginx/config.sls index 3cd00b5..b3ea331 100644 --- a/nginx/config.sls +++ b/nginx/config.sls @@ -27,7 +27,5 @@ nginx_config: ) }} - template: jinja -{% if 'source_path' not in nginx.server.config %} - context: - config: {{ nginx.server.config|json(sort_keys=False) }} -{% endif %} + config: {{ nginx.server.get('config', {})|json(sort_keys=False) }} diff --git a/nginx/servers_config.sls b/nginx/servers_config.sls index 6652d64..2c3373b 100644 --- a/nginx/servers_config.sls +++ b/nginx/servers_config.sls @@ -129,11 +129,9 @@ nginx_server_available_dir: - {{ k }}: {{ v }} {%- endfor %} {%- endif %} -{% if 'source_path' not in settings.config %} - context: - config: {{ settings.config|json(sort_keys=False) }} nginx: {{ _nginx|json() }} -{% endif %} + config: {{ settings.get('config',{})|json(sort_keys=False) }} {% if 'overwrite' in settings and settings.overwrite == False %} - unless: - test -e {{ server_curpath(server) }} diff --git a/pillar.example b/pillar.example index 7b610ef..8ecdcc1 100644 --- a/pillar.example +++ b/pillar.example @@ -119,9 +119,10 @@ nginx: ## - - -- - - -- -- - - --- -- - -- - - - -- - - - - -- - - - -- - - - -- - ## config: include: 'snippets/letsencrypt.conf' - # IMPORTANT: This option is mutually exclusive with TOFS and the rest of - # the options; if it is found other options (worker_processes: 4 and so - # on) are not processed and just upload the file from source + # IMPORTANT: This option is mutually exclusive with TOFS. + # It uploads the file from source and passes the global `nginx` and the + # `config` dictionnary to the template. + # `jinja` is assumed as template type. source_path: salt://path_to_nginx_conf_file/nginx.conf worker_processes: 4 # pass as very first in configuration; otherwise nginx will fail to start @@ -278,9 +279,14 @@ nginx: available_dir: /etc/nginx/sites-available enabled_dir: /etc/nginx/sites-enabled config: - # IMPORTANT: This field is mutually exclusive with TOFS and other - # config options, it just uploads the specified file + # IMPORTANT: This field is mutually exclusive with TOFS. + # It uploads the file from source and passes the global `nginx` and the + # `config` dictionnary to the template. + # `jinja` is assumed as template type. source_path: salt://path-to-site-file/mysite2 + # Example: + port: 80 # is passed as `config.port` to template located at `source_path` + custom_key: custom_value # Below configuration becomes handy if you want to create custom # configuration files for example if you want to create