Skip to content

Commit

Permalink
Merge pull request saltstack#51047 from terminalmage/issue49619
Browse files Browse the repository at this point in the history
Add new execution module for troubleshooting Jinja map files
  • Loading branch information
garethgreenaway authored and max-arnold committed Dec 18, 2019
1 parent 0e5f8e8 commit 4ddf939
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/ref/modules/all/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ execution modules
jboss7
jboss7_cli
jenkinsmod
jinja
jira_mod
junos
k8s
Expand Down
6 changes: 6 additions & 0 deletions doc/ref/modules/all/salt.modules.jinja.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
==================
salt.modules.jinja
==================

.. automodule:: salt.modules.jinja
:members:
106 changes: 106 additions & 0 deletions salt/modules/jinja.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-
'''
Module for checking jinja maps and verifying the result of loading JSON/YAML
files
.. versionadded:: Neon
'''
from __future__ import absolute_import, print_function, unicode_literals

# Import Python libs
import functools
import logging
import textwrap

# Import Salt libs
import salt.loader
import salt.template
import salt.utils.json

log = logging.getLogger(__name__)


def _strip_odict(wrapped):
'''
dump to json and load it again, replaces OrderedDicts with regular ones
'''
@functools.wraps(wrapped)
def strip(*args):
return salt.utils.json.loads(salt.utils.json.dumps(wrapped(*args)))
return strip


@_strip_odict
def load_map(path, value):
'''
Loads the map at the specified path, and returns the specified value from
that map.
CLI Example:
.. code-block:: bash
# Assuming the map is loaded in your formula SLS as follows:
#
# {% from "myformula/map.jinja" import myformula with context %}
#
# the following syntax can be used to load the map and check the
# results:
salt myminion jinja.load_map myformula/map.jinja myformula
'''
tmplstr = textwrap.dedent('''\
{{% from "{path}" import {value} with context %}}
{{{{ {value} | tojson }}}}
'''.format(path=path, value=value))
return salt.template.compile_template_str(
tmplstr,
salt.loader.render(__opts__, __salt__),
__opts__['renderer'],
__opts__['renderer_blacklist'],
__opts__['renderer_whitelist'])


@_strip_odict
def import_yaml(path):
'''
Loads YAML data from the specified path
CLI Example:
.. code-block:: bash
salt myminion jinja.import_yaml myformula/foo.yaml
'''
tmplstr = textwrap.dedent('''\
{{% import_yaml "{path}" as imported %}}
{{{{ imported | tojson }}}}
'''.format(path=path))
return salt.template.compile_template_str(
tmplstr,
salt.loader.render(__opts__, __salt__),
__opts__['renderer'],
__opts__['renderer_blacklist'],
__opts__['renderer_whitelist'])


@_strip_odict
def import_json(path):
'''
Loads JSON data from the specified path
CLI Example:
.. code-block:: bash
salt myminion jinja.import_JSON myformula/foo.json
'''
tmplstr = textwrap.dedent('''\
{{% import_json "{path}" as imported %}}
{{{{ imported | tojson }}}}
'''.format(path=path))
return salt.template.compile_template_str(
tmplstr,
salt.loader.render(__opts__, __salt__),
__opts__['renderer'],
__opts__['renderer_blacklist'],
__opts__['renderer_whitelist'])

0 comments on commit 4ddf939

Please sign in to comment.