python jinja2を利用してネットワーク検証環境のコンフィグを作成する

# はじめに
 ネットワーク装置のスケール検証でindexだけ変えたコンフィグを大量に作成及び設定することがありました。
 その際、pythonのテンプレートエンジンjinja2が便利でしたので実際に使った例を紹介します。

# 目的
下記のunit,vidとLSPのindex,routing-instancesのindexを変数にして65セット作りたい
```
set interfaces ge-0/0/1 unit 1000 encapsulation vlan-vpls
set interfaces ge-0/0/1 unit 1000 vlan-id 1000
set protocols mpls label-switched-path vMX1-to-vMX2-00 from 10.100.100.1
set protocols mpls label-switched-path vMX1-to-vMX2-00 to 10.100.100.2
set protocols mpls label-switched-path vMX1-to-vMX2-00 primary vMX1-to-vMX2-00-pri
set protocols mpls label-switched-path vMX1-to-vMX2-00 secondary vMX1-to-vMX2-00-sby
set protocols mpls path vMX1-to-vMX2-00-pri 10.100.12.2 strict
set protocols mpls path vMX1-to-vMX2-00-sby 10.100.100.2 loose
set policy-options policy-statement rule_00000 term 10 from community 00000
set policy-options community 00000 members target:64512:00000
set policy-options policy-statement rule_00000 term 10 from rib mpls.0
set policy-options policy-statement rule_00000 term 10 from community 00000
set policy-options policy-statement rule_00000 term 10 then install-nexthop strict
set policy-options policy-statement rule_00000 term 10 then install-nexthop lsp vMX1-to-vMX2-00
set policy-options policy-statement rule_00000 term 10 then accept
set routing-options forwarding-table export rule_00000
set routing-instances rt00000 instance-type vpls
set routing-instances rt00000 protocols vpls site rt00000 site-identifier 1
set routing-instances rt00000 protocols vpls no-tunnel-services
set routing-instances rt00000 interface ge-0/0/1.1000
set routing-instances rt00000 route-distinguisher 64512:00000
set routing-instances rt00000 vrf-target target:64512:00000
```

# フォルダ構成
```
.
├── make_lab_config.py   #   実行部分
├── output.txt           #   出力結果
└── templates
    └── config.j2        #   jinja2 template
```

# メイン実行部分
```make_lab_config.py
import jinja2

jinja2_env = jinja2.Environment(loader=jinja2.FileSystemLoader("templates"))
jinja2_template = jinja2_env.get_template("config.j2")

text = ""

for vid, rt, index in zip(range(1000,1065), range(0,65), range(0,65)):
    text = text + jinja2_template.render({
        "vid": vid,
        "rt": rt,
        "index": index
        })

filename = "output.txt"
with open(filename, "w") as f:
    f.write(text)

```

##### 解説

```python
jinja2_env = jinja2.Environment(loader=jinja2.FileSystemLoader("templates"))
jinja2_template = jinja2_env.get_template("config.j2")
```
jinja2 templateファイルの指定
FileSystemLoader : フォルダパス
get_template("ファイル名") : ロードするファイル名

```python
for vid, rt, index in zip(range(1000,1065), range(0,65), range(0,65)):
    text = text + jinja2_template.render({
        "vid": vid,
        "rt": rt,
        "index": index
        })
```
繰り返し部分
jinja2_template.render(param): 前項で指定したjinja2 templateファイルに変数を埋め込んだ生成した結果のstringを返す。


# jinja2 template部分
```config.j2
set interfaces ge-0/0/1 unit {{vid}} encapsulation vlan-vpls
set interfaces ge-0/0/1 unit {{vid}} vlan-id {{vid}} 
set protocols mpls label-switched-path vMX1-to-vMX2-{{'%02d' % rt}} from 10.100.100.1
set protocols mpls label-switched-path vMX1-to-vMX2-{{'%02d' % rt}} to 10.100.100.2
set protocols mpls label-switched-path vMX1-to-vMX2-{{'%02d' % rt}} primary vMX1-to-vMX2-{{'%02d' % rt}}-pri
set protocols mpls label-switched-path vMX1-to-vMX2-{{'%02d' % rt}} secondary vMX1-to-vMX2-{{'%02d' % rt}}-sby
set protocols mpls path vMX1-to-vMX2-{{'%02d' % rt}}-pri 10.100.12.2 strict
set protocols mpls path vMX1-to-vMX2-{{'%02d' % rt}}-sby 10.100.100.2 loose
set policy-options policy-statement rule_{{'%05d' % rt}} term 10 from community {{'%05d' % rt}}
set policy-options community {{'%05d' % rt}} members target:64512:{{'%05d' % rt}}
set policy-options policy-statement rule_{{'%05d' % rt}} term 10 from rib mpls.0
set policy-options policy-statement rule_{{'%05d' % rt}} term 10 from community {{'%05d' % rt}}
set policy-options policy-statement rule_{{'%05d' % rt}} term 10 then install-nexthop strict
set policy-options policy-statement rule_{{'%05d' % rt}} term 10 then install-nexthop lsp vMX1-to-vMX2-{{'%02d' % rt}}
set policy-options policy-statement rule_{{'%05d' % rt}} term 10 then accept
set routing-options forwarding-table export rule_{{'%05d' % rt}}
set routing-instances rt{{'%05d' % rt}} instance-type vpls
set routing-instances rt{{'%05d' % rt}} protocols vpls site rt{{'%05d' % rt}} site-identifier 1
set routing-instances rt{{'%05d' % rt}} protocols vpls no-tunnel-services
set routing-instances rt{{'%05d' % rt}} interface ge-0/0/1.{{vid}}
set routing-instances rt{{'%05d' % rt}} route-distinguisher 64512:{{'%05d' % rt}}
set routing-instances rt{{'%05d' % rt}} vrf-target target:64512:{{'%05d' % rt}}
```
{{ 変数 }} :  変数の中身で置換されて出力する
{{ '%05d' % rt }} : 変数rtを5桁の0埋めして出力する

# 処理実行及び実行結果
```
$ python make_lab_config.py 
```

```output.txt
set interfaces ge-0/0/1 unit 1000 encapsulation vlan-vpls
set interfaces ge-0/0/1 unit 1000 vlan-id 1000 
set protocols mpls label-switched-path vMX1-to-vMX2-00 from 10.100.100.1
set protocols mpls label-switched-path vMX1-to-vMX2-00 to 10.100.100.2
set protocols mpls label-switched-path vMX1-to-vMX2-00 primary vMX1-to-vMX2-00-pri
set protocols mpls label-switched-path vMX1-to-vMX2-00 secondary vMX1-to-vMX2-00-sby
set protocols mpls path vMX1-to-vMX2-00-pri 10.100.12.2 strict
set protocols mpls path vMX1-to-vMX2-00-sby 10.100.100.2 loose
set policy-options policy-statement rule_00000 term 10 from community 00000
set policy-options community 00000 members target:64512:00000
set policy-options policy-statement rule_00000 term 10 from rib mpls.0
set policy-options policy-statement rule_00000 term 10 from community 00000
set policy-options policy-statement rule_00000 term 10 then install-nexthop strict
set policy-options policy-statement rule_00000 term 10 then install-nexthop lsp vMX1-to-vMX2-00
set policy-options policy-statement rule_00000 term 10 then accept
set routing-options forwarding-table export rule_00000
set routing-instances rt00000 instance-type vpls
set routing-instances rt00000 protocols vpls site rt00000 site-identifier 1
set routing-instances rt00000 protocols vpls no-tunnel-services
set routing-instances rt00000 interface ge-0/0/1.1000
set routing-instances rt00000 route-distinguisher 64512:00000
set routing-instances rt00000 vrf-target target:64512:00000
set interfaces ge-0/0/1 unit 1001 encapsulation vlan-vpls
set interfaces ge-0/0/1 unit 1001 vlan-id 1001 
set protocols mpls label-switched-path vMX1-to-vMX2-01 from 10.100.100.1
set protocols mpls label-switched-path vMX1-to-vMX2-01 to 10.100.100.2
set protocols mpls label-switched-path vMX1-to-vMX2-01 primary vMX1-to-vMX2-01-pri
set protocols mpls label-switched-path vMX1-to-vMX2-01 secondary vMX1-to-vMX2-01-sby
set protocols mpls path vMX1-to-vMX2-01-pri 10.100.12.2 strict
set protocols mpls path vMX1-to-vMX2-01-sby 10.100.100.2 loose
set policy-options policy-statement rule_00001 term 10 from community 00001
set policy-options community 00001 members target:64512:00001
set policy-options policy-statement rule_00001 term 10 from rib mpls.0
set policy-options policy-statement rule_00001 term 10 from community 00001
set policy-options policy-statement rule_00001 term 10 then install-nexthop strict
set policy-options policy-statement rule_00001 term 10 then install-nexthop lsp vMX1-to-vMX2-01
set policy-options policy-statement rule_00001 term 10 then accept
set routing-options forwarding-table export rule_00001
set routing-instances rt00001 instance-type vpls
set routing-instances rt00001 protocols vpls site rt00001 site-identifier 1
set routing-instances rt00001 protocols vpls no-tunnel-services
set routing-instances rt00001 interface ge-0/0/1.1001
set routing-instances rt00001 route-distinguisher 64512:00001
set routing-instances rt00001 vrf-target target:64512:00001
set interfaces ge-0/0/1 unit 1002 encapsulation vlan-vpls
set interfaces ge-0/0/1 unit 1002 vlan-id 1002 
set protocols mpls label-switched-path vMX1-to-vMX2-02 from 10.100.100.1
set protocols mpls label-switched-path vMX1-to-vMX2-02 to 10.100.100.2
set protocols mpls label-switched-path vMX1-to-vMX2-02 primary vMX1-to-vMX2-02-pri
set protocols mpls label-switched-path vMX1-to-vMX2-02 secondary vMX1-to-vMX2-02-sby
set protocols mpls path vMX1-to-vMX2-02-pri 10.100.12.2 strict
set protocols mpls path vMX1-to-vMX2-02-sby 10.100.100.2 loose
set policy-options policy-statement rule_00002 term 10 from community 00002
set policy-options community 00002 members target:64512:00002
set policy-options policy-statement rule_00002 term 10 from rib mpls.0
set policy-options policy-statement rule_00002 term 10 from community 00002
set policy-options policy-statement rule_00002 term 10 then install-nexthop strict
set policy-options policy-statement rule_00002 term 10 then install-nexthop lsp vMX1-to-vMX2-02
set policy-options policy-statement rule_00002 term 10 then accept
set routing-options forwarding-table export rule_00002
set routing-instances rt00002 instance-type vpls
set routing-instances rt00002 protocols vpls site rt00002 site-identifier 1
set routing-instances rt00002 protocols vpls no-tunnel-services
set routing-instances rt00002 interface ge-0/0/1.1002
set routing-instances rt00002 route-distinguisher 64512:00002
set routing-instances rt00002 vrf-target target:64512:00002
~
set interfaces ge-0/0/1 unit 1064 encapsulation vlan-vpls
set interfaces ge-0/0/1 unit 1064 vlan-id 1064 
set protocols mpls label-switched-path vMX1-to-vMX2-64 from 10.100.100.1
set protocols mpls label-switched-path vMX1-to-vMX2-64 to 10.100.100.2
set protocols mpls label-switched-path vMX1-to-vMX2-64 primary vMX1-to-vMX2-64-pri
set protocols mpls label-switched-path vMX1-to-vMX2-64 secondary vMX1-to-vMX2-64-sby
set protocols mpls path vMX1-to-vMX2-64-pri 10.100.12.2 strict
set protocols mpls path vMX1-to-vMX2-64-sby 10.100.100.2 loose
set policy-options policy-statement rule_00064 term 10 from community 00064
set policy-options community 00064 members target:64512:00064
set policy-options policy-statement rule_00064 term 10 from rib mpls.0
set policy-options policy-statement rule_00064 term 10 from community 00064
set policy-options policy-statement rule_00064 term 10 then install-nexthop strict
set policy-options policy-statement rule_00064 term 10 then install-nexthop lsp vMX1-to-vMX2-64
set policy-options policy-statement rule_00064 term 10 then accept
set routing-options forwarding-table export rule_00064
set routing-instances rt00064 instance-type vpls
set routing-instances rt00064 protocols vpls site rt00064 site-identifier 1
set routing-instances rt00064 protocols vpls no-tunnel-services
set routing-instances rt00064 interface ge-0/0/1.1064
set routing-instances rt00064 route-distinguisher 64512:00064
set routing-instances rt00064 vrf-target target:64512:00064
```

以上になります。