Ansible操作例 – Cisco IOS XRへの接続方法の説明

スポンサーリンク

AnsibleでCisco IOS XRへ接続する方法を説明します。Ansibleでサーバ機器を操作する場合はPythonスクリプトを他機器へ転送する挙動になりますが、ネットワーク機器を操作する場合は「network_cli」と呼ばれる特殊な接続方法を使用します。

ネットワーク機器を操作する時は「network_cli」を使うのが多数派です。ですが、BIG-IPは「local」と呼ばれる接続を使うように例外もある事もご留意ください。

事前準備

Cisco IOS XR SSH接続の確認

Ansibleでネットワーク機器を操作するにはsshで接続する必要があります。telnetによる接続方法もありますが、telnetは従来のTeraTermマクロのようなexpectの仕組みを使うため、非常に難解な実装を求められます。そのため、特別な理由がない限りはsshを接続許可しましょう。

IOS XRはSSH接続許可のための明示的な設定は必要ありません。それではSSHログインが可能である事を確かめましょう。

(venv-ans28) [root@ansible70 ansible]# ssh 192.168.9.61 -l cisco



IMPORTANT:  READ CAREFULLY
Welcome to the Demo Version of Cisco IOS XRv (the "Software").
The Software is subject to and governed by the terms and conditions
of the End User License Agreement and the Supplemental End User
License Agreement accompanying the product, made available at the
time of your order, or posted on the Cisco website at
www.cisco.com/go/terms (collectively, the "Agreement").
As set forth more fully in the Agreement, use of the Software is
strictly limited to internal use in a non-production environment
solely for demonstration and evaluation purposes.  Downloading,
installing, or using the Software constitutes acceptance of the
Agreement, and you are binding yourself and the business entity
that you represent to the Agreement.  If you do not agree to all
of the terms of the Agreement, then Cisco is unwilling to license
the Software to you and (a) you may not download, install or use the
Software, and (b) you may return the Software as more fully set forth
in the Agreement.


Please login with any configured user/password, or cisco/cisco


Password:


RP/0/0/CPU0:R61#

paramikoインストール

Ansibleでネットワーク機器を操作するにはparamikoと呼ばれるモジュールが必要です。paramiko未インストールの場合は以下のようなエラーメッセージが出力されます。

# ansible-playbook -i inventory.ini cisco_iosxr_show_version.yml

PLAY [cisco_iosxr] *************************************************************

TASK [send show version] *******************************************************
fatal: [cisco61]: FAILED! => {"msg": "paramiko is not installed: No module named 'paramiko'"}

PLAY RECAP *********************************************************************
cisco61                    : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

それでは、paramikoをインストールしましょう。

pip install paramiko

インベントリファイルの作成

Cisco IOS XRと接続できるよう以下のようなインベントリファイルを作成します。

cat << EOF > inventory.ini
[cisco_iosxr:vars]
ansible_user = 'cisco'
ansible_password = 'cisco'
ansible_connection = 'network_cli'
ansible_network_os = 'iosxr'

[cisco_iosxr]
cisco61 ansible_host=192.168.9.61
EOF

ansible_network_osはOSの種類によって適宜変更が必要です。IOS XRを使用する場合は、「ansible_network_os」は「iosxr」を指定します。

動作確認

「show version」を実行するプレイブックを作成します。

cat << EOF > cisco_iosxr_show_version.yml
---
- hosts: cisco_iosxr
  gather_facts: no

  tasks:
    - name: send show version
      iosxr_command:
        commands: "show version"
EOF

疎通可能である事を確認するため、実行結果が「OK」となる事を確認します。

# ansible-playbook -i inventory.ini cisco_iosxr_show_version.yml

PLAY [cisco_iosxr] *************************************************************

TASK [send show version] *******************************************************
ok: [cisco61]

PLAY RECAP *********************************************************************
cisco61                    : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

現時点でshowコマンドの出力結果は表示されていませんが、showコマンドの結果を表示する方法は「Ansible操作例 – Cisco IOS XRの操作方法の説明」で紹介します。

タイトルとURLをコピーしました