4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ヤマハ公式Ansibleモジュールでヤマハルータを操作する。(その1:モジュールのインストール)

Last updated at Posted at 2021-04-04

はじめに

先日、ヤマハAnsible Galaxyでヤマハのネットワーク機器をAnsibleで制御するためのモジュールを公開しました。

これはさっそく使ってみなければと思い、試してみた結果を書き記します。

ヤマハ公式モジュール

以下のAnsible GalaxyGitHubのページにヤマハモジュールがあります。

Ansibleの2.10以降の構成は今までの2.9以前のバージョンと構成が大きく異なり、ヤマハモジュールは2.10以降を前提に作成されているようなので、2.9以前では動作しません。

dnfaptなどOS標準のパッケージ管理ツールでAnsibleをインストールする場合、インストールされるバージョンは2021年4月現在、2.9が最新となるため、ヤマハ公式モジュールを使用する場合、pipで最新バージョンのAnsibleをインストールする必要があります。

Ansibleインストール

今回はCentOS 8.3の最小構成のマシンにAnsibleをインストールしてみます。

既にAnsibleがインストールされているマシンにインストールする場合などは一度インストール済みのAnsibleを削除して実行してください。

pipをアップグレード後、pipコマンドでAnsibleをインストールすれば最新版がインストールできるはずです。

また、paramikoモジュールも必要となるため、一緒にインストールしておきます。

尚、全てroot権限で実行すること。

Ansibleのインストール
dnf install python3
pip3 install --upgrade pip
pip3 install ansible
pip3 install paramiko

ansible --versionコマンドを実行し、以下のように2.10以上のバージョンが表示されれば問題ありません。

Ansibleバージョン確認
ansible 2.10.7
  config file = None
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/lib/python3.6/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.6.8 (default, Aug 24 2020, 17:57:11) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

ヤマハ公式モジュールのインストール

Ansible Galaxyのコレクションで配布されているモジュールをインストールします。

ヤマハ公式モジュールのインストール
ansible-galaxy collection install yamaha_network.rtx

2021/4/9追記

yamaha_network.rtxで利用するansible.netcommonのバージョンが低いと差分比較処理がうまく動かないようなので、ansible.netcommonも更新しておきます。
※ちなみに私の場合、インストール済みだった1.5.0では想定通りの動きになりませんでした。

```shell:ansible.netcommon`コレクションのインストール
ansible-galaxy collection install ansible.netcommon


```shell:ansible.netcommon`コレクションインストール後の確認
ansible-galaxy collection list ansible.netcommon
コマンド結果
# /root/.ansible/collections/ansible_collections
Collection        Version
----------------- -------
ansible.netcommon 2.0.1

# /usr/local/lib/python3.6/site-packages/ansible_collections
Collection        Version
----------------- -------
ansible.netcommon 1.5.0

事前ssh接続

Playbookを実行する前に対象のヤマハ機器にsshで接続できるか確認しておきます。

フィンガープリントが表示された場合はyesで受け入れておきましょう。

ssh接続
ssh -l [一般ユーザ名] [IPアドレス]

Ansible実行準備

ヤマハ公式ページを参考にインベントリファイルとPlaybookを作成します。

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

私が持っているヤマハ機器は「NVR500」のため、参考例から書き換えておきます。

インベントリファイルの作成(inventory)
[NVR500]
192.168.100.1

Playbookの作成

ホスト名のみ書き換えていますが、公式の例そのままコピペ。

ansible_become_passwordAdministratorになる際のパスワードを入力します。

修正したPlaybook(rtx.yml)
---
- hosts: NVR500
  connection: network_cli

  tasks:
    - name: get configuration
      yamaha_network.rtx.rtx_command:
        commands:
        - show config
      register: result

    - name: debug
      debug:
        msg: "{{ result.stdout_lines[0] }}"

    - name: set description
      yamaha_network.rtx.rtx_config:
        lines:
          - description 1 yamaha

  vars:
    ansible_network_os: yamaha_network.rtx.rtx
    ansible_user: [一般ユーザ名]
    ansible_ssh_pass: [一般ユーザパスワード]
    ansible_become: true
    ansible_become_password: [Administratorパスワード]

Playbookの実行

以下を実行し、実行結果でヤマハ機器のコンフィグが出力されれば成功です。

Playbookの実行
ansible-playbook -i inventory rtx.yml

おわりに

ヤマハ機器を使っている企業は多いかと思うので、公式モジュールで自動化できるのはありがたいですね。

モジュール自体はまだAnsibleを介してコマンド実行するモジュールしかないため、設定を行う場合、どこまで冪等性が保てるかという問題はありますが、例で紹介されていたコンフィグバックアップ等、運用系の作業をコード化するのに有用だと感じます。

今までサーバにExpect入れて無理やりコンフィグ採取シェル等書いて作業していたことを考えると、ヤマハ機器へのログイン処理を考えなくて済むだけでもありがたい!

次回は公式モジュールで何をどこまでできるかを色々試してみたいと思います。

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?