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 2 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
9 changes: 7 additions & 2 deletions development/nautobot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
import sys

from django.utils.module_loading import import_string

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

Expand Down Expand Up @@ -174,8 +176,11 @@
"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)),
"jinja_env": {
"undefined": import_string("jinja2.StrictUndefined"),
"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
14 changes: 12 additions & 2 deletions docs/admin/admin_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,21 @@ The plugin behavior can be controlled with the following list of settings.
| per_feature_bar_width | 0.15 | 0.15 | The width of the table bar within the overview report |
| per_feature_width | 13 | 13 | The width in inches that the overview table can be. |
| per_feature_height | 4 | 4 | The height in inches that the overview table can be. |
| jinja_env_trim_blocks | True | True | A boolean to represent whether the jinja2 option for "trim_blocks" should be enabled for intended config rendering |
| jinja_env_lstrip_blocks | True | False | A boolean to represent whether the jinja2 option for "lstrip_blocks" should be enabled for intended config rendering |
| jinja_env | {"jinja_env_lstrip_blocks": False} | See Note Below | A dictionary of Jinja2 Environment options compatible with Jinja2.SandboxEnvironment() |

!!! note
Over time the compliance report will become more dynamic, but for now allow users to configure the `per_*` configs in a way that fits best for them.

!!! note
Review [`nautobot_plugin_nornir`](https://docs.nautobot.com/projects/plugin-nornir/en/latest/user/app_feature_dispatcher/) for Nornir and dispatcher configuration options.

!!! note
Defaults for Jinja2 environment settings (`jinja_env`) are as follows:

```python
jinja_env = {
"undefined": import_string("jinja2.StrictUndefined"),
"jinja_env_trim_blocks": True,
"jinja_env_lstrip_blocks": False,
}
```
itdependsnetworks marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 6 additions & 2 deletions nautobot_golden_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

__version__ = metadata.version(__name__)

from jinja2 import StrictUndefined
from django.db.models.signals import post_migrate
from nautobot.extras.plugins import PluginConfig

Expand Down Expand Up @@ -36,8 +37,11 @@ class GoldenConfig(PluginConfig):
"per_feature_width": 13,
"per_feature_height": 4,
"get_custom_compliance": None,
"jinja_env_trim_blocks": True,
"jinja_env_lstrip_blocks": False,
"jinja_env": {
"undefined": StrictUndefined,
"jinja_env_trim_blocks": True,
"jinja_env_lstrip_blocks": False,
},
}

def ready(self):
Expand Down
10 changes: 2 additions & 8 deletions nautobot_golden_config/nornir_plays/config_intended.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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
Expand All @@ -36,14 +35,9 @@
LOGGER = logging.getLogger(__name__)

# Use a custom Jinja2 environment instead of Django's to avoid HTML escaping
OPTION_TRIM_BLOCKS = PLUGIN_CFG["jinja_env_trim_blocks"]
OPTION_LSTRIP_BLOCKS = PLUGIN_CFG["jinja_env_lstrip_blocks"]
JINJA_ENV = PLUGIN_CFG["jinja_env"]
jinja_env = SandboxedEnvironment(**JINJA_ENV)

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

Expand Down