AnsibleでJunosのバージョン情報を取得
インフラ開発部 松田です。
今回は弊社で検証中の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のイメージを持ってきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ 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'! |
確認。
1 2 3 4 |
$ vagrant box list centos (virtualbox, 0) coreos-alpha (virtualbox, 367.0.0) juniper/ffp-12.1X46-D25.7 (virtualbox, 0.1.6) |
続いて初期化。
1 2 3 4 5 |
$ 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は変更せずそのまま起動。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
$ 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へ。
1 |
$ vagrant ssh-config >> ~/.ssh/config |
config内のhost名を任意のものに変更してログイン。
(ここではdefault→junosに変更)
1 2 3 |
$ ssh junos --- JUNOS 12.1X46-D25.7 built 2014-09-06 01:40:34 UTC root@% |
Ansibleで操作するためにJunosのnetconfを有効化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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の準備
インストール。
1 2 3 4 5 |
$ 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を分割して再利用や共有する機能)を取得。
1 2 3 4 5 |
$ 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に必要なモジュールをインストール。
1 2 |
$ 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を実行!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$ 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

関連記事
-
-
GCP上でCoreOSクラスタを作ってコンテナ起動をしてみた
Google Cloud Platform(GCP)上でCoreOS + etcd + fleet + docker でコンテナ起動まで行ったので紹介したいと思います。 CoreOSの起動 CoreOSは、コンテナの実行環境を構築することに特化したLinuxディストリビューションです。 なお、この …
-
-
【クラウド初心者向け】Google Cloud Platform(GCP)でWebサイトを公開してみよう!
はじめに みなさんこんにちは、プロダクト開発本部の亀梨です。 普段はXmediaOneというメディアプランニング・広告運用管理・トラッキング・マーケティング分析を行う 統合プラットフォームの開発を担当しています。 背景 わたくしは最近プライベートで開発したWebサービスをインターネット上に公開しまし …
-
-
AWSのcredentialsを注意して取り扱う話
はじめに 最近ではオンプレミスでサーバを自前で用意する他に、クラウドサーバを使う機会が増えているかと思います。 弊社では、Amazon Web Services (AWS)を利用しており、多くの処理をAWS上で行っています。 AWSを利用していくにあたっては、アクセス情報(credentials)を …
-
-
Burpの使い方!
こんにちは、第二ソリューション開発部の谷口です。 受託開発の部署で開発を担当してます。 APIを扱う機会が多く、今回は通信内容を確認するためのローカルプロキシツール「Burp」について書かせて頂きます。 Burpとは Webアプリケーション開発時の検証において、Webサーバとブラウザ間の通信内容を確 …
- PREV
- 自社サービスのDocker化(前編)
- NEXT
- いま必要なのは「アナリティクスアプローチ」