LoginSignup
7
7

More than 5 years have passed since last update.

Juniper x PyEZ, Ansible など自動化はじめ

Last updated at Posted at 2016-12-17

この記事は SRA Advent Calendar 2016 参加記事です。

流行りということで、自動化に手を付け出しています。
幸い、今運用している機器が、Juniper, NetApp, A10 ADC とそれぞれ NETCONF, REST API で操作出来る機器が揃っています。これは自動化するっきゃない。

まずはJuniper

Junos であれば、以下のコンフィグを投入することで、PyEZ, Ansible で操作することが出来ます。
set system services netconf ssh

PyEZ

まずは PyEZ からの操作について。
vSRX と PyEZ の下準備についてはこちら

PyEZ は情報取得など、ワンショットで実行するもので使おうかと考えています。

以下のコードはバージョンの取得です。以下の例は Device をタプルにして、複数ホストに使えるようにしてます。

getVersion.py
from jnpr.junos import Device

Devices = [ { "hostname" : 'vSRX01' , "user": 'sgmt' }, ]

for device in Devices:
    dev = Device(host=device["hostname"], user=device["user"], port='22')
    dev.open()
    print "Hostname:{:<10} Version:{}".format( dev.facts["hostname"], dev.facts["version"] )
実行結果
% python getVersion.py
Hostname:vSRX01     Version:12.1X47-D20.7

Ansible

次は Ansible
予め、ansible-galaxy で Juniper.junos をインストールしておく必要があります。
* Juniper を Ansible, Python で操作する準備

投入するコンフィグを set 形式で用意しておき、それを投入する playbook

tasks/main.yml
- name: add user
  junos_config:
    src: config/users.set
    src_format: set
    backup: yes
    provider: "{{ netconf }}"
  tags: users
vars/main.yml
netconf:
  host: "{{ inventory_hostname }}"
  port: 22
  username: sgmt
playbook
- hosts: vSRX
  roles:
    - Juniper.junos
    - FireWall
  connection: local
  gather_facts: no
config/users.set
set system login user opr uid 2002
set system login user opr class operator
set system login user opr authentication ssh-rsa "ssh-rsa 公開鍵hogehoge opr@hoge"
実行結果
ansible-playbook FireWall.yml -i hosts.dev -t users

PLAY [vSRX] ********************************************************************

TASK [FireWall : add user] *****************************************************
changed: [vSRX01]

PLAY RECAP *********************************************************************
vSRX01                     : ok=1    changed=1    unreachable=0    failed=0

変更内容を確認してみます。期待通り追加されてます。

show|compare
sgmt@vSRX01# show | compare rollback 1
[edit system login]
+    user opr {
+        uid 2002;
+        class operator;
+        authentication {
+            ssh-rsa "ssh-rsa 公開鍵hogehoge opr@hoge"; ## SECRET-DATA
+        }
+    }

[edit]
sgmt@vSRX01#

せっかくの SRX なので、ファイアウォール関連を Ansible で投入すると、、、

tasks/main.yml
- name: define zones
  junos_config:
    src: config/zones.set
    src_format: set
    backup: yes
    provider: "{{ netconf }}"
  tags: zones

- name: define policies
  junos_config:
    src: config/policies.set
    src_format: set
    backup: yes
    confirm: 3
    provider: "{{ netconf }}"
  tags: policies

zone の定義

config/zones.set
set security zones security-zone untrust interfaces ge-0/0/0.0
set security zones security-zone trust interfaces ge-0/0/1.0 host-inbound-traffic system-services ping
set security zones security-zone DMZ interfaces ge-0/0/2.0
set security zones security-zone DMZ host-inbound-traffic system-services ping
set security zones security-zone DMZ host-inbound-traffic system-services ssh

ポリシーの定義

config/policies.set
set security zones security-zone DMZ address-book address www range-address 10.0.1.30 to 10.0.1.39
set security policies from-zone untrust to-zone DMZ policy service match source-address any
set security policies from-zone untrust to-zone DMZ policy service match destination-address www
set security policies from-zone untrust to-zone DMZ policy service match application junos-http
set security policies from-zone untrust to-zone DMZ policy service match application junos-https
set security policies from-zone untrust to-zone DMZ policy service then permit

これだと、set形式のコマンドを書けないと行けませんが、
from, to のzone, match の source, destination address, application を json で出力する Web 画面を用意しておき、それを set 形式に変換するスクリプトを用意しておけば、自動化できそうです。 commit confirmed も task に commit 書いておけば大丈夫

NetApp

次は NetApp
NetApp は Oncommand Unified Manager で複数クラスターを監理できますが、OnCommand API Services サーバを立て、Oncommand Unified Manager に接続すると、REST API で操作できます。

aggregate や volume の使用量を取得して週次のレポートを作成したりしています。

A10 ADC

A10 は aXAPI が何もせずに使えます。こちらもまだ情報取得系のみで使っている状態です。

まとめ

API 等で操作出来る機器が増えていますので、今まで手作業だったオペレーションを自動化し、作業工数削減、作業品質向上につなげていきたいですね。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7