Browse Source

Add support for jinja2 renderer options. (2018.2)

Change-Id: Idf6c18597a222e6c1b60bf940eadb1d6d8a611c7
pull/73/head
Petr Michalec 6 years ago
parent
commit
df75d684a6
No account linked to committer's email address
8 changed files with 145 additions and 6 deletions
  1. +2
    -2
      .kitchen.yml
  2. +1
    -1
      .travis.yml
  3. +54
    -1
      README.rst
  4. +23
    -0
      salt/files/_renderer.conf
  5. +6
    -0
      salt/map.jinja
  6. +15
    -1
      salt/master/service.sls
  7. +17
    -1
      salt/minion/service.sls
  8. +27
    -0
      tests/pillar/master_formulas.sls

+ 2
- 2
.kitchen.yml View File

noservices: true noservices: true
vendor_repo: vendor_repo:
- type: apt - type: apt
url: http://apt-mk.mirantis.com/xenial
key_url: http://apt-mk.mirantis.com/public.gpg
url: http://apt.mirantis.com/xenial
key_url: http://apt.mirantis.com/public.gpg
components: salt components: salt
distribution: testing distribution: testing
state_top: state_top:

+ 1
- 1
.travis.yml View File

gem 'kitchen-docker' gem 'kitchen-docker'
gem 'kitchen-inspec' gem 'kitchen-inspec'
gem 'inspec' gem 'inspec'
gem 'kitchen-salt', :git => 'https://github.com/salt-formulas/kitchen-salt.git'
gem 'kitchen-salt' #, :git => 'https://github.com/salt-formulas/kitchen-salt.git'
- bundle install - bundle install


env: env:

+ 54
- 1
README.rst View File

salt-call event.send 'salt/key/remove' salt-call event.send 'salt/key/remove'




Encrypted Pillars
Jinja options
-------------

Use following options to update default jinja renderer options. Salt recognize Jinja options for templates and for sls files.

For full list of options check jinja documentation: http://jinja.pocoo.org/docs/api/#high-level-api.

.. code-block:: yaml


salt:
renderer:
# for templates
jinja: &jina_env
# Default Jinja environment options
block_start_string: '{%'
block_end_string: '%}'
variable_start_string: '{{'
variable_end_string: '}}'
comment_start_string: '{#'
comment_end_string: '#}'
keep_trailing_newline: False
newline_sequence: '\n'

# Next two are enabled by default in Salt
trim_blocks: True
lstrip_blocks: True

# Next two are not enabled by default in Salt
# but worth to consider to enable in future for salt-formulas
line_statement_prefix: '%'
line_comment_prefix: '##'

# for .sls state files
jinja_sls: *jinja_env



With the line_statement/comment* _prefix options enabled following code statements are valid:

.. code-block:: yaml
%- set myvar = 'one'

## You can mix even with '{%'
{%- set myvar = 'two' %} ## comment
%- set mylist = ['one', 'two', 'three'] ## comment

## comment
%- for item in mylist: ## comment
{{- item }}
%- endfor


Encrypted pillars
~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~


Note: NACL + below configuration will be available in Salt > 2017.7. Note: NACL + below configuration will be available in Salt > 2017.7.

+ 23
- 0
salt/files/_renderer.conf View File

{%- from "salt/map.jinja" import renderer with context %}

{%- if renderer.get('jinja_sls', {}) | length > 0 %}

jinja_sls_env:
{% for key, value in renderer.jinja_sls.iteritems() %}
{{ key }}: "{{ value }}"
{%- endfor %}

{%- endif %}

{%- if renderer.get('jinja', {}) | length > 0 %}

jinja_env:
{% for key, value in renderer.jinja.iteritems() %}
{{ key }}: "{{ value }}"
{%- endfor %}

{%- endif %}

{#-
vim: syntax=jinja
-#}

+ 6
- 0
salt/map.jinja View File

- pkg-config - pkg-config
{%- endload %} {%- endload %}


{%- load_yaml as renderer_defaults %}
default:
renderer: {}
{%- endload %}
{%- set renderer = salt['grains.filter_by'](renderer_defaults, merge=salt['pillar.get']('salt:renderer')) %}

{%- if pillar.salt.control is defined %} {%- if pillar.salt.control is defined %}
{%- set raw_control = salt['grains.filter_by'](control_specific, merge=salt['pillar.get']('salt:control')) %} {%- set raw_control = salt['grains.filter_by'](control_specific, merge=salt['pillar.get']('salt:control')) %}
{%- set control = salt['grains.filter_by'](control_common, merge=raw_control) %} {%- set control = salt['grains.filter_by'](control_common, merge=raw_control) %}

+ 15
- 1
salt/master/service.sls View File

{%- from "salt/map.jinja" import master with context %}
{%- from "salt/map.jinja" import master, renderer with context %}
{%- if master.enabled %} {%- if master.enabled %}


{%- if master.source.get('engine', 'pkg') == 'pkg' %} {%- if master.source.get('engine', 'pkg') == 'pkg' %}


{%- endif %} {%- endif %}


{%- if renderer | length > 0 %}

/etc/salt/master.d/_renderer.conf:
file.managed:
- source: salt://salt/files/_renderer.conf
- user: root
- template: jinja
- require:
- {{ master.install_state }}
- watch_in:
- service: salt_master_service

{%- endif %}

salt_master_service: salt_master_service:
service.running: service.running:
- name: {{ master.service }} - name: {{ master.service }}

+ 17
- 1
salt/minion/service.sls View File

{%- from "salt/map.jinja" import minion with context %}
{%- from "salt/map.jinja" import minion,renderer with context %}
{%- if minion.enabled %} {%- if minion.enabled %}


{%- if minion.source.get('engine', 'pkg') == 'pkg' %} {%- if minion.source.get('engine', 'pkg') == 'pkg' %}
{%- endif %} {%- endif %}
{%- endfor %} {%- endfor %}



{%- if renderer | length > 0 %}

/etc/salt/minion.d/_renderer.conf:
file.managed:
- source: salt://salt/files/_renderer.conf
- user: root
- template: jinja
- require:
- {{ minion.install_state }}
- watch_in:
- service: salt_minion_service

{%- endif %}


salt_minion_service: salt_minion_service:
service.running: service.running:
- name: {{ minion.service }} - name: {{ minion.service }}

+ 27
- 0
tests/pillar/master_formulas.sls View File

source: git source: git
address: 'https://github.com/salt-formulas/salt-formula-bind.git' address: 'https://github.com/salt-formulas/salt-formula-bind.git'
revision: master revision: master
renderer:
jinja:
block_start_string: {{ '"{%"' }}
block_end_string: {{ '"%}"' }}
variable_start_string: {{ '"{{"' }}
variable_end_string: {{ '"}}"' }}
comment_start_string: {{ '"{#"' }}
comment_end_string: {{ '"#}"' }}
keep_trailing_newline: False
newline_sequence: '\n'
trim_blocks: True
lstrip_blocks: True
line_statement_prefix: "%"
line_comment_prefix: "##"
jinja_sls:
block_start_string: {{ '"{%"' }}
block_end_string: {{ '"%}"' }}
variable_start_string: {{ '"{{"' }}
variable_end_string: {{ '"}}"' }}
comment_start_string: {{ '"{#"' }}
comment_end_string: {{ '"#}"' }}
keep_trailing_newline: False
newline_sequence: '\n'
trim_blocks: True
lstrip_blocks: True
line_statement_prefix: "%"
line_comment_prefix: "##"

Loading…
Cancel
Save