日本語版 / Japanese | Config Subcommand
The config subcommand supports Jinja2 templates (.j2 files) to generate per-host set commands from a single template. This is useful when deploying configurations that vary by host — such as hostnames, NTP servers, or protocol settings based on device role.
Jinja2 is an optional dependency:
pip install junos-ops[template]Note: Jinja2 is already included as a dependency of junos-eznc (PyEZ), so it may already be available without explicit installation.
Common variables go in [DEFAULT] and are inherited by all hosts. Host-specific values can override them.
[DEFAULT]
var_ntp_server = 192.0.2.1
var_syslog_host = 192.0.2.2
[rt1.example.jp]
tags = tokyo, core
[sw1.example.jp]
tags = tokyo, access{# ntp.set.j2 — NTP and syslog configuration #}
set system host-name {{ hostname }}
set system ntp server {{ ntp_server }}
set system syslog host {{ syslog_host }} any warning
{% if facts.personality == 'SWITCH' %}
set protocols vstp
{% endif %}junos-ops config -f ntp.set.j2 --dry-run rt1.example.jp sw1.example.jpFor rt1.example.jp (router), the rendered output:
set system host-name rt1.example.jp
set system ntp server 192.0.2.1
set system syslog host 192.0.2.2 any warning
For sw1.example.jp (switch), the rendered output includes set protocols vstp:
set system host-name sw1.example.jp
set system ntp server 192.0.2.1
set system syslog host 192.0.2.2 any warning
set protocols vstp
junos-ops config -f ntp.set.j2 rt1.example.jp sw1.example.jpThree sources of variables are available in templates:
| Source | Description | Example |
|---|---|---|
hostname |
Config section name | {{ hostname }} → rt1.example.jp |
var_* keys |
config.ini keys with var_ prefix (prefix stripped) |
var_ntp_server → {{ ntp_server }} |
facts |
Device facts (dict) retrieved after NETCONF connection | {{ facts.model }} → MX240 |
var_ keys follow configparser's standard inheritance:
- Host section value (highest priority)
[DEFAULT]section value (fallback)
[DEFAULT]
var_ntp_server = 192.0.2.1 # used by all hosts
[rt1.example.jp]
var_ntp_server = 192.0.2.99 # overrides DEFAULT for rt1 onlyThe facts variable is a dict containing device information retrieved via NETCONF after connection. Common keys:
| Key | Example | Description |
|---|---|---|
facts.hostname |
rt1 |
Device hostname |
facts.model |
MX240 |
Device model |
facts.version |
21.4R3-S5.4 |
JUNOS version |
facts.personality |
MX, SWITCH, SRX_BRANCH |
Device type |
facts.serialnumber |
ABC1234 |
Serial number |
Both dot notation (facts.model) and bracket notation (facts['model']) work.
set system ntp server {{ ntp_server }}
{% if facts.personality == 'SWITCH' %}
set protocols vstp
set ethernet-switching-options storm-control interface all
{% elif facts.personality == 'SRX_BRANCH' %}
set security zones security-zone trust
{% endif %}Define a comma-separated list in config.ini:
[DEFAULT]
var_ntp_servers = 192.0.2.1,192.0.2.2,192.0.2.3Use {% for %} to iterate:
{% for server in ntp_servers.split(',') %}
set system ntp server {{ server }}
{% endfor %}Rendered output:
set system ntp server 192.0.2.1
set system ntp server 192.0.2.2
set system ntp server 192.0.2.3
[DEFAULT]
var_snmp_community = public
var_snmp_prefixes = 192.0.2.0/24,198.51.100.0/24,203.0.113.0/24{% for prefix in snmp_prefixes.split(',') %}
set snmp community {{ snmp_community }} clients {{ prefix }}
{% endfor %}Jinja2 comments ({# ... #}) and shell-style comments (# ...) are both supported. Both are removed from the final output.
{# This Jinja2 comment is removed during rendering #}
# This shell-style comment is removed after rendering
set system ntp server {{ ntp_server }}Templates use Jinja2's StrictUndefined mode. Any undefined variable causes an immediate error with a clear message — the template is never partially applied.
set system ntp server {{ undefined_var }}jinja2.exceptions.UndefinedError: 'undefined_var' is not defined
Jinja2 syntax errors (e.g., missing {% endif %}) are caught before any configuration is sent to the device.
If Jinja2 is not available, an ImportError with installation instructions is raised:
ImportError: Jinja2 is required for template support. Install it with: pip install junos-ops[template]