インフラ開発部 松田です。
今回は弊社で検証中のAnsibleについて書きます。
Ansibleを使ったサーバ構築の記事は最近よく見かけますが、私が触る機会の多いネットワーク機器も操作できる(Ansibleはエージェントレス)ということで、Ansible+NW機器について色々書いていきます。
初回はJunosのバージョンをAnsibleを使用して取得してみます!
Ansible AnsibleとはPythonベースの構成管理ツールです。 ただ構成管理以外にも任意のコマンドをリモートで実行し、結果を取得するなどオーケストレーションツールとしてもAnsibleは利用できます。
詳細は入門Ansible著者のそこはかとなく書くよん。をご参照ください。
Junos Juniper Networksのアプライアンス製品に搭載されているFreeBSDをベースに開発されたOS。SSG/NetScreenシリーズに搭載されていたScreenOSとは操作や設定が大きく異なる。
環境 今回は以下の環境で検証を実施しました。
-
OS X 10.9.5
VirtualBox 4.3.18
Vagrant 1.6.3
Homebrew 0.9.5
ansible 1.8.1
環境準備 VirtalBox上にJunosのホストを立て検証します。
まずはvagrantにJunosのイメージを持ってきます。
$ vagrant box add https://vagrantcloud.com/juniper/boxes/ffp-12.1X46-D25.7 ==> box: Loading metadata for box 'https://vagrantcloud.com/juniper/boxes/ffp-12.1X46-D25.7' This box can work with multiple providers! The providers that it can work with are listed below. Please review the list and choose the provider you will be working with. 1) virtualbox 2) vmware_desktop Enter your choice: 1 ==> box: Adding box 'juniper/ffp-12.1X46-D25.7' (v0.1.6) for provider: virtualbox box: Downloading: https://vagrantcloud.com/juniper/boxes/ffp-12.1X46-D25.7/versions/0.1.6/providers/virtualbox.box ==> box: Successfully added box 'juniper/ffp-12.1X46-D25.7' (v0.1.6) for 'virtualbox'!
確認。
$ vagrant box list centos (virtualbox, 0) coreos-alpha (virtualbox, 367.0.0) juniper/ffp-12.1X46-D25.7 (virtualbox, 0.1.6)
続いて初期化。
$ vagrant init juniper/ffp-12.1X46-D25.7 A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
Vagrantfileは変更せずそのまま起動。
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'juniper/ffp-12.1X46-D25.7'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'juniper/ffp-12.1X46-D25.7' is up to date... ==> default: Setting the name of the VM: junos_default_1417588215459_87691 ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat ==> default: Forwarding ports... default: 22 => 2222 (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2222 default: SSH username: root default: SSH auth method: private key default: Warning: Connection timeout. Retrying... default: Warning: Connection timeout. Retrying... default: Warning: Connection timeout. Retrying... default: Warning: Connection timeout. Retrying... default: Warning: Connection timeout. Retrying... ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message.
接続情報をconfigへ。
$ vagrant ssh-config >> ~/.ssh/config
config内のhost名を任意のものに変更してログイン。 (ここではdefault→junosに変更)
$ ssh junos --- JUNOS 12.1X46-D25.7 built 2014-09-06 01:40:34 UTC root@%
Ansibleで操作するためにJunosのnetconfを有効化。
root@% cli root> configure Entering configuration mode [edit] root# set system services netconf ssh [edit] root# commit commit complete [edit] root# show system services ssh; netconf { ssh; } web-management { http { interface ge-0/0/0.0; } }
Ansibleの準備 インストール。
$ brew install ansible ==> Installing ansible dependency: libyaml ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/libyaml-0.1.6.mavericks.bottle.1.tar.gz ######################################################################## 100.0% ==> Pouring libyaml-0.1.6.mavericks.bottle.1.tar.gz
Ansible GalaxyからJuniper.junosのrole(playbookを分割して再利用や共有する機能)を取得。
$ ansible-galaxy install Juniper.junos - downloading role 'junos', owned by Juniper - downloading role from https://github.com/Juniper/ansible-junos-stdlib/archive/1.0.0.tar.gz - extracting Juniper.junos to /usr/local/etc/ansible/roles/Juniper.junos - Juniper.junos was installed successfully
Juniper.junosに必要なモジュールをインストール。
$ sudo pip install junos-eznc 〜実行結果は長いので割愛〜
最後にinventoryファイル(対象nodeを記載するファイル)、 playbook(ansibleで実行する手順書のようなもの)を準備。
[html title="hosts-junos"] [junos] junos [/html]
[html title="playbook.yml"]
- name: Show facts playbook
hosts: junos
roles:
- Juniper.junos connection: local gather_facts: no
tasks:
- name: get facts junos_get_facts: host={{ inventory_hostname }} user=root register: junos
- name: show facts debug: msg="{{ junos.facts.version }}" [/html]
ansibleを実行!
$ ansible-playbook -i hosts-junos playbook.yml PLAY [Show config playbook] *************************************************** TASK: [get facts] ************************************************************* ok: [junos] TASK: [show facts] ************************************************************ ok: [junos] => { "msg": "12.1X46-D25.7" } PLAY RECAP ******************************************************************** junos : ok=2 changed=0 unreachable=0 failed=0
まとめ vagrantで検証用のJunosを構築し、ansibleを使ってJunosのステータスを取得しました。 Juniper.junosには他にも設定の追加(junos_install_config)やOSのアップデート(junos_install_os)、nodeの再起動(junos_shutdown)などのモジュールが含まれていたので機会があったら試してみたいと思います。
参考 Ansible for Junos OS VAGRANT CLOUD Juniper.junos Ansible Modules