瀏覽代碼

Add support for jinja2 renderer options. (2018.2)

Change-Id: Idf6c18597a222e6c1b60bf940eadb1d6d8a611c7
pull/73/head
Petr Michalec 6 年之前
父節點
當前提交
df75d684a6
沒有連結到貢獻者的電子郵件帳戶。
共有 8 個檔案被更改,包括 145 行新增6 行删除
  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 查看文件

@@ -17,8 +17,8 @@ provisioner:
noservices: true
vendor_repo:
- 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
distribution: testing
state_top:

+ 1
- 1
.travis.yml 查看文件

@@ -13,7 +13,7 @@ install:
gem 'kitchen-docker'
gem 'kitchen-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

env:

+ 54
- 1
README.rst 查看文件

@@ -340,7 +340,60 @@ Event to trigger the key removal
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.

+ 23
- 0
salt/files/_renderer.conf 查看文件

@@ -0,0 +1,23 @@
{%- 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 查看文件

@@ -201,6 +201,12 @@ RedHat:
- pkg-config
{%- 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 %}
{%- 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) %}

+ 15
- 1
salt/master/service.sls 查看文件

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

{%- if master.source.get('engine', 'pkg') == 'pkg' %}
@@ -70,6 +70,20 @@ salt_master_packages:

{%- 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:
service.running:
- name: {{ master.service }}

+ 17
- 1
salt/minion/service.sls 查看文件

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

{%- if minion.source.get('engine', 'pkg') == 'pkg' %}
@@ -92,6 +92,22 @@ salt_minion_{{ service_name }}_dependencies_pip:
{%- endif %}
{%- 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:
service.running:
- name: {{ minion.service }}

+ 27
- 0
tests/pillar/master_formulas.sls 查看文件

@@ -49,3 +49,30 @@ salt:
source: git
address: 'https://github.com/salt-formulas/salt-formula-bind.git'
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…
取消
儲存