-
Notifications
You must be signed in to change notification settings - Fork 58
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
Enable lstrip_blocks Jinja2 Environment Option for Intended Configs #525
Comments
You should be able to update the configurations now, not sure what the issues is right now, but I think there should be a solution. That being said, I think this is a common enough request to make better integrations as you mentioned. @jdrew82 @bile0026 can you put in a PR for this? Not too opinioated on the solution, but I think it makes sense to have the config in nautobot_config.py. |
Adding additional clarification that I have 2 PRs associated with this issue. One is for nautobot-golden-config and the other is for nornir-nautobot. nornir-nautobot/pull/97 |
🌎 Environment
Enable use of the
lstrip_blocks
jinja2 environment option to enable golden config templates to use indented code blocks without the added whitespace showing in the rendered config.👨⚖️ Proposed Functionality
As a golden config template maintainer, I'd like to be able to indent my code blocks in my jinja2 templates so that I can enhance readability in situations such as nested loops. This would make the templates easier to read and maintain across teams of developers, and lower the barrier to entry for template creation.
👩💻 Use Case
This NTC Blog on Whitespace Control does a great job explaining the need and use cases for
lstrip_blocks
.However, the listed directive
#jinja2: lstrip_blocks: True
which the article mentions can be added to a Jinja template when used with Ansible, does not seem to have any effect when used with Nautobot templates.In a large scale deployment of Nautobot with dozens or even hundreds of templates, ensuring that directive is properly added to all templates and their includes may be cumbersome.
😡 Solutions That Don't Work
The first solution I tried was to create a custom jinja2 environment and install it as a module during the build of Nautobot.
At first glance, the code in
nornir_plays/config_intended.py
references thejinja
templating engine created in django. However, this engine is only imported so that the associated filters can be passed to thegenerate_config
task. The environment itself is not sent with the task.Modifying both the
nautobot-golden-config
andnornir-nautobot
modules to pass the environment as a parameter, does cause nornir to use the custom environment, but also causes any quoted values in your source YAML files to be escaped as HTML entities in the rendered intended config. This seems to be a side effect of django's intended use of their jinja engine.✅ Working Solution
I was able to get the desired functionality working by creating a jinja2 environment inside the
nautobot-golden-config
module with the intention of this environment being specifically used for controlling the rendering of jinja inside the plugin.I added 2 new settings to our
nautobot_config.py
for external control of the options.In
nautobot_golden_config/nornir_plays/config_intended.py
, I replacedwith
In the same file's
run_template
method, I added an additional parameter totask.run
.Finally, in the
nornir-nautobot
module, in the filenornir-nautobot/plugins/tasks/dispatcher/default.py
, I continued to ensure the parameter for the environment was passed through to thetemplate_file
task.It should be noted that the
nornir-jinja2
module creates its own jinja2 environment if one is not sent. With this environment, it enables the optiontrim_blocks
by default. This is the section of code controlling this behavior.🏁 Conclusion
lstrip_blocks
option globally, and would be helpful to template maintainers.trim_blocks
enabled by default and cannot be disabled at present.The text was updated successfully, but these errors were encountered: