Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new execution module for troubleshooting Jinja map files #51047

Merged
merged 2 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/ref/modules/all/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,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:
8 changes: 8 additions & 0 deletions doc/topics/releases/neon.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Salt Release Notes - Codename Neon
==================================


Execution Module for Checking Jinja Map Files
=============================================

To aid in troubleshooting, an execution module has been added, which allows one
to see the data loaded from a jinja map, or imported using ``import_yaml`` or
``import_json``. See :py:mod:`here <salt.modules.jinja>` for more information.


Saltcheck Updates
=================

Expand Down
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'])