cfgtemplater
cfgtemplater is a Jinja2 template CLI rendering tool. It allows to integrate metadata and defaults variables values within a YAML header. It is also integrates basic IP filters and tests since it's main development purpose is to generate network devices configuration files. It is as well able to load external filters and tests from other python modules.
Installation
pip install cfgtemplater
Usage
usage: cfgtemplater [-h] [-y YAML] [-p KEY=VALUE] [-e FILE] TEMPLATE
A simple YAML/Jinja2 config generator.
positional arguments:
TEMPLATE template
optional arguments:
-h, --help show this help message and exit
-y YAML YAML file
-p KEY=VALUE key/value pair
-e FILE Jinja2 extensions modules
Usage examples
Load variables from a YAML file
cfgtemplater -y variables.yml template.j2
Load variables from multiple YAML files (merged in order)
cfgtemplater -y default_variables.yml -y instance_variables.yml template.j2
Override variables from CLI
cfgtemplater -y default_variables -p 'variable1=value1' template.j2
Pass python literals from CLI
cfgtemplater -p "variable1=[{'name':'varx', 'value':'x'}, {'name':'vary', 'value':'y'}]" template.j2
Template
(Main) template may look like this:
---
name: Example template
variables:
variable1:
default: "example"
description: "example variable"
variable2:
default:
- value: 0
name: zero
- value: 1
name: one
---
This is an {{ variable1 }} file.
{% for variable in variable2 %}
This is a {{ variable.name }} : {{ variable.value }}
{% endfor %}
{{ variable3 }}
The YAML header may contain template metadata (like name, description, whatever). It may as well contain default variables or variables descriptions or any variable metadata. Defaults values are injected in the template when rendering, and can be redefined using CLI or YAML file.
Extensions
Jinja2 filters and tests can be loaded from external files.
Load ansible ipaddr filter (ansible_ipaddr.py)
import netaddr
from ansible.plugins.filter.ipaddr import *
Render template using ipaddr filters
cfgtemplater -e ansible_ipaddr.py template.j2
Basic module usage
>>> from cfgtemplater.config_template import ConfigTemplate
>>> t = ConfigTemplate('examples/example1.j2')
>>> t.name
'Example template'
>>> t.variables
{'variable1': {'default': 'example', 'description': 'example variable'}, 'variable2': {'default': [{'value': 0, 'name': 'zero'}, {'value': 1, 'name': 'one'}]}}
>>> t.content
'This is an {{ variable1 }} file.\n\n{% for variable in variable2 %}\n This is a {{ variable.name }} : {{ variable.value }}\n{% endfor %}\n\n{{ variable3 }}\n'
>>> print(t.render({'variable3': 'TEST'}))
This is an example file.
This is a zero : 0
This is a one : 1
TEST
Integrated filters and tests
IP filters
first_address
{{ ipv4_net | first_address }}
{{ ipv6_net | first_address }}
192.0.2.1/24
2001:db8::1/120
last_address
{{ ipv4_net | last_address }}
{{ ipv6_net | last_address }}
192.0.2.254/24
2001:db8::ff/120
address
{{ ipv4_net | address }}
{{ ipv6_net | address }}
192.0.2.0
2001:db8::
network
{{ ipv4_net | first_address | network }}
{{ ipv6_net | first_address | network }}
192.0.2.0/24
2001:db8::/120
broadcast
{{ ipv4_net | broadcast }}
{{ ipv6_net | broadcast }}
192.0.2.255/24
2001:db8::ff/120
prefixlen
{{ ipv4_net | prefixlen }}
{{ ipv6_net | prefixlen }}
24
120
netmask
{{ ipv4_net | netmask }}
{{ ipv6_net | netmask }}
255.255.255.0
ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00
hostmask
{{ ipv4_net | first_address | hostmask }}
{{ ipv6_net | first_address | hostmask }}
0.0.0.255
::ff
compress
{{ '192.000.002.001' | compress }}
{{ '2001:0db8:0000:0000:0000:0000:0000:0001' | compress }}
192.0.2.1
2001:db8::1
uncompress
{{ '192.0.2.1' | uncompress }}
{{ '2001:db8::1' | uncompress }}
192.000.002.001
2001:0db8:0000:0000:0000:0000:0000:0001
octets
{% for octet in ipv4_net | octets %}
{{ octet }}
{% endfor %}
192
0
2
0
hextets
{% for hextet in ipv6_net | hextets %}
{{ hextet }}
{% endfor %}
2001
0db8
0000
0000
0000
0000
0000
0000
IP tests
is_ip
{{ ipv4_net | is_ip }}
{{ ipv6_net | is_ip }}
{{ 'test' | is_ip
True
True
False
is_ipv4
{{ ipv4_net | is_ipv4 }}
{{ ipv6_net | is_ipv4 }}
True
False
is_ipv6
{{ ipv6_net | is_ipv6 }}
{{ ipv4_net | is_ipv6 }}
True
False
is_ip_net
{{ ipv4_net | is_ip_net }}
{{ ipv6_net | is_ip_net }}
{{ ipv6_net | first_address | is_ip_net }}
{{ ipv6_net | first_address | is_ip_net }}
True
True
False
False
is_ipv4_net
{{ ipv4_net | is_ipv4_net }}
{{ ipv6_net | is_ipv4_net }}
{{ ipv4_net | first_address | is_ipv4_net }}
{{ ipv6_net | first_address | is_ipv4_net }}
True
False
False
False
is_ipv6_net
{{ ipv6_net | is_ipv6_net }}
{{ ipv4_net | is_ipv6_net }}
{{ ipv6_net | first_address | is_ipv4_net }}
{{ ipv4_net | first_address | is_ipv4_net }}
True
False
False
False
is_ipv4_host
{{ ipv4_net | first_address | is_ipv4_host }}
{{ ipv4_net | is_ipv4_host }}
True
False
Metadata
Release files for cfgtemplater 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cfgtemplater-0.0.2.tar.gz | 7.3 kB | Without provenance | Details for cfgtemplater-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cfgtemplater-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for cfgtemplater-0.0.2-py3-none-any.whl |
Total release size: 15.9 kB
Release files / cfgtemplater-0.0.2.tar.gz
| Download URL | cfgtemplater-0.0.2.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
49a9b0bf86c87c124f827e915b062bacf0e8e83afb7711eae092e18cc7b2b583
|
|
BLAKE2b-256 checksum How to use checksums |
e216cce8c3497f2951f142386b1899effee52672ed57d34e8a44f86a1184bf94
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
|
Release files / cfgtemplater-0.0.2-py3-none-any.whl
| Download URL | cfgtemplater-0.0.2-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7cbc0f92a548547cd2ea028c48ae844433fbc3fd6e0121c6debeeb1d3b4b95cf
|
|
BLAKE2b-256 checksum How to use checksums |
5a41d73465844cf68b50d28f89e4224204fe8e9273e0753097ff8242f7a55377
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3
|
Security for cfgtemplater 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.