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

feat: ✨Add jinja settings support for golden config plugin #527

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 3 additions & 1 deletion development/nautobot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys

from nautobot.core.settings import * # noqa: F403
from nautobot.core.settings_funcs import parse_redis_connection
from nautobot.core.settings_funcs import is_truthy, parse_redis_connection


#
Expand Down Expand Up @@ -174,6 +174,8 @@
"enable_postprocessing": is_truthy(os.environ.get("ENABLE_POSTPROCESSING", True)),
"postprocessing_callables": os.environ.get("POSTPROCESSING_CALLABLES", []),
"postprocessing_subscribed": os.environ.get("POSTPROCESSING_SUBSCRIBED", []),
"jinja_env_trim_blocks": is_truthy(os.getenv("NAUTOBOT_JINJA_ENV_TRIM_BLOCKS", True)),
"jinja_env_lstrip_blocks": is_truthy(os.getenv("NAUTOBOT_JINJA_ENV_LSTRIP_BLOCKS", False)),
# The platform_slug_map maps an arbitrary platform slug to its corresponding parser.
# Use this if the platform slug names in your Nautobot instance don't correspond exactly
# to the Nornir driver names ("arista_eos", "cisco_ios", etc.).
Expand Down
21 changes: 20 additions & 1 deletion nautobot_golden_config/nornir_plays/config_intended.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from nornir.core.task import Result, Task

from django.template import engines
from jinja2 import StrictUndefined
from jinja2.sandbox import SandboxedEnvironment

from nornir_nautobot.exceptions import NornirNautobotException
from nornir_nautobot.plugins.tasks.dispatcher import dispatcher
Expand All @@ -18,6 +20,7 @@
from nautobot_plugin_nornir.constants import NORNIR_SETTINGS
from nautobot_plugin_nornir.utils import get_dispatcher

from nautobot_golden_config.utilities.constant import PLUGIN_CFG
from nautobot_golden_config.utilities.db_management import close_threaded_db_connections
from nautobot_golden_config.models import GoldenConfigSetting, GoldenConfig
from nautobot_golden_config.utilities.helper import (
Expand All @@ -32,7 +35,22 @@
InventoryPluginRegister.register("nautobot-inventory", NautobotORMInventory)
LOGGER = logging.getLogger(__name__)

jinja_env = engines["jinja"].env
# Use a custom Jinja2 environment instead of Django's to avoid HTML escaping
# Trim_blocks option defaulted to True to match nornir's default environment
OPTION_LSTRIP_BLOCKS = False
OPTION_TRIM_BLOCKS = True
itdependsnetworks marked this conversation as resolved.
Show resolved Hide resolved
if PLUGIN_CFG.get("jinja_env_trim_blocks"):
OPTION_TRIM_BLOCKS = PLUGIN_CFG.get("jinja_env_trim_blocks")
if PLUGIN_CFG.get("jinja_env_lstrip_blocks"):
OPTION_LSTRIP_BLOCKS = PLUGIN_CFG.get("jinja_env_lstrip_blocks")

jinja_env = SandboxedEnvironment(
undefined=StrictUndefined,
trim_blocks=OPTION_TRIM_BLOCKS,
lstrip_blocks=OPTION_LSTRIP_BLOCKS,
)
# Retrieve filters from the Djanog jinja template engine
jinja_env.filters = engines["jinja"].env.filters
itdependsnetworks marked this conversation as resolved.
Show resolved Hide resolved


@close_threaded_db_connections
Expand Down Expand Up @@ -85,6 +103,7 @@ def run_template( # pylint: disable=too-many-arguments
output_file_location=output_file_location,
default_drivers_mapping=get_dispatcher(),
jinja_filters=jinja_env.filters,
jinja_env=jinja_env,
itdependsnetworks marked this conversation as resolved.
Show resolved Hide resolved
)[1].result["config"]
intended_obj.intended_last_success_date = task.host.defaults.data["now"]
intended_obj.intended_config = generated_config
Expand Down