Skip to content

Commit

Permalink
feat(servers_config): passes context to custom source_path templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sticky-note committed Dec 28, 2022
1 parent 982705f commit 8035f4b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
6 changes: 2 additions & 4 deletions nginx/config.sls
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ nginx_config:
- name: {{ nginx.lookup.conf_file }}
- source:
{% if 'source_path' in nginx.server.config %}
- {{ nginx.server.config.source_path }}
- {{ nginx.server.config.pop('source_path') }}
{% endif %}
{{ files_switch(['nginx.conf'],
'nginx_config_file_managed'
)
}}
- 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) }}
6 changes: 2 additions & 4 deletions nginx/servers_config.sls
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ nginx_server_available_dir:
- name: {{ server_curpath(server) }}
- source:
{%- if 'source_path' in settings.config %}
- {{ settings.config.source_path }}
- {{ settings.config.pop('source_path') }}
{%- endif %}
{{ files_switch([server, 'server.conf'],
'server_conf_file_managed'
Expand All @@ -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) }}
Expand Down
16 changes: 11 additions & 5 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -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 (without the `source_path` key).
# `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
Expand Down Expand Up @@ -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 (without the `source_path` key).
# `jinja` is assumed as template type.
source_path: salt://path-to-site-file/mysite2
# Example:
port: 80 # is passed as `config.port` to `salt://path-to-site-file/mysite2` template
custom_key: custom_value

# Below configuration becomes handy if you want to create custom
# configuration files for example if you want to create
Expand Down

0 comments on commit 8035f4b

Please sign in to comment.