LoginSignup
11
11

More than 3 years have passed since last update.

[Ansible]NetBoxにホスト変数も保持し、ネットワーク機器の情報取得をする

Last updated at Posted at 2020-12-23

■はじめに

NetBoxというOSSのIPAM/DCIMツールがあります。
Ansibleと組みあわせてみると、結構面白いです。

■今回やってみること

NetBoxをAnsibleのインベントリーとして使い、
ネットワーク機器(今回はCiscoCSR1000V)の情報取得をしたいのですが、

ホスト変数もインベントリー指定ではなく、NetBoxで保持し、管理しておきたい
というのが今回のテーマです。

そうすれば、
Ansible実行サーバーで各ノードの認証情報や必要パラメーターを保持する必要がなくなり、
NetBoxに認証情報や必要なパラメーターを一括して保持できる
ので、
セキュリティや取り回しの向上が期待できるのではないかと思います。

■今回やってみることの図

image.png

■NetBoxの登録状況

①Tokyo/Racks/testrack/Cisco CSR 1000Vの画面です。
CiscoCSR1000Vが以下の画像のようにNetBoxに登録してあります。

登録内容の詳細な説明は省かせていただきますが、
CIscoCSR1000Vの地理的な位置、格納されているラック、IPv4アドレスなど様々な情報を登録しています。

image.png

②CiscoCSR 1000v のConfig Contextの画面です。
ここでホスト変数をJSONデータで保持しておきます。

ここではAnsible接続情報に必要なansible_connection
対象機器認証情報のansible_passwordansible_userが保持されています。

image.png

■NetBoxからインベントリーを取得するyamlファイル(netbox.yml)作成

netbox.yml

---
plugin: netbox                        # netboxプラグイン
api_endpoint: https:/xxx.xxx.xxx.xxx   # NetBoxAPIエンドポイントのipアドレス
token: xxxxxxxxxxxxxxxxxxxxxxxxxxxx   # Netbox認証トークン
group_by:
  - manufacturers                     # manufactures単位で取得
compose:                              # NetBoxのデバイスオブジェクトから作成するカスタムansibleホスト変数のリスト
  ansible_network_os: platform.slug   # platformのios指定
  ansible_connection: local_context_data.ansible_connection  # local_context_data配下の接続方式の指定
  ansible_user: local_context_data.ansible_user              # local_context_data配のユーザー名の指定
  ansible_password: local_context_data.ansible_password      # local_context_data配下のパスワードの指定


■ CiscoCSR1000Vの機器情報取得をするplaybook(ios_show.yml)作成

ios_show.yml

---
- hosts: manufacturers_cisco
  gather_facts: no

  tasks:
    - name: show test
      ios_command:
        commands:
          - show version    # show version を実行
      register: result

    - name: debug test
      debug:
        msg: "{{ result }}"  # コマンド実行結果を表示

■Playbookの実行

$ ansible-playbook -i netbox.yml ios_show.yml


PLAY [manufacturers_cisco] *******************************************************************

TASK [show test] ****************************
ok: [Cisco CSR 1000V]
TASK [debug test] ******************************************************************************************************************************************************
ok: [Cisco CSR 1000V] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false,
        "failed": false,
        "stdout": [
            "Cisco IOS XE Software, Version 17.01.01\nCisco IOS Software [Amsterdam], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.1.1, RELEASE SOFTWARE (fc3)\nTechnical Support: http://www.cisco.com/techsupport\nCopyright (c) 1986-2019 by Cisco Systems, Inc.\nCompiled Fri 22-Nov-19 03:39 by mcpre\n\n\nCisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc.\nAll rights reserved.  Certain components of Cisco IOS-XE software are\nlicensed under the GNU General Public License (\"GPL\") Version 2.0.  The\nsoftware code licensed under GPL Version 2.0 is free software that comes\nwith ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such\nGPL code under the terms of GPL Version 2.0.  For more details, see the\ndocumentation or \"License Notice\" file accompanying the IOS-XE software,\nor the applicable URL provided on the flyer accompanying the IOS-XE\nsoftware.\n\n\nROM: IOS-XE ROMMON\n\nip-172-31-42-187 uptime is 1 hour, 14 minutes\nUptime for this control processor is 1 hour, 16 minutes\nSystem returned to ROM by reload\nSystem image file is \"bootflash:packages.conf\"\nLast reload reason: reload\n\n\n\nThis product contains cryptographic features and is subject to United\nStates and local country laws governing import, export, transfer and\nuse. Delivery of Cisco cryptographic products does not imply\nthird-party authority to import, export, distribute or use encryption.\nImporters, exporters, distributors and users are responsible for\ncompliance with U.S. and local country laws. By using this product you\nagree to comply with applicable laws and regulations. If you are unable\nto comply with U.S. and local laws, return this product immediately.\n\nA summary of U.S. laws governing Cisco cryptographic products may be found at:\nhttp://www.cisco.com/wwl/export/crypto/tool/stqrg.html\n\nIf you require further assistance please contact us by sending email to\nexport@cisco.com.\n\nLicense Level: ax\nLicense Type: N/A(Smart License Enabled)\nNext reload license Level: ax\n\n\nSmart Licensing Status: UNREGISTERED/No Licenses in Use\n\ncisco CSR1000V (VXE) processor (revision VXE) with 2074934K/3075K bytes of memory.\nProcessor board ID 9QBEJYDG2DJ\n1 Gigabit Ethernet interface\n32768K bytes of non-volatile configuration memory.\n3976156K bytes of physical memory.\n6188032K bytes of virtual hard disk at bootflash:.\n\nConfiguration register is 0x2102"
        ],
        "stdout_lines": [
            [
                "Cisco IOS XE Software, Version 17.01.01",
                "Cisco IOS Software [Amsterdam], Virtual XE Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 17.1.1, RELEASE SOFTWARE (fc3)",
                "Technical Support: http://www.cisco.com/techsupport",
                "Copyright (c) 1986-2019 by Cisco Systems, Inc.",
                "Compiled Fri 22-Nov-19 03:39 by mcpre",
                "",
                "",
                "Cisco IOS-XE software, Copyright (c) 2005-2019 by cisco Systems, Inc.",
                "All rights reserved.  Certain components of Cisco IOS-XE software are",
                "licensed under the GNU General Public License (\"GPL\") Version 2.0.  The",
                "software code licensed under GPL Version 2.0 is free software that comes",
                "with ABSOLUTELY NO WARRANTY.  You can redistribute and/or modify such",
                "GPL code under the terms of GPL Version 2.0.  For more details, see the",
                "documentation or \"License Notice\" file accompanying the IOS-XE software,",
                "or the applicable URL provided on the flyer accompanying the IOS-XE",
                "software.",
                "",
                "",
                "ROM: IOS-XE ROMMON",
                "",
         (略)
                "",
                "",
                "Smart Licensing Status: UNREGISTERED/No Licenses in Use",
                "",
                "cisco CSR1000V (VXE) processor (revision VXE) with 2074934K/3075K bytes of memory.",
                "Processor board ID 9QBEJYDG2DJ",
                "1 Gigabit Ethernet interface",
                "32768K bytes of non-volatile configuration memory.",
                "3976156K bytes of physical memory.",
                "6188032K bytes of virtual hard disk at bootflash:.",
                "",
                "Configuration register is 0x2102"
            ]
        ]
    }
}

PLAY RECAP *************************************************************************************************************************************************************
Cisco CSR 1000V            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Playbookが成功しました。

Ansibleサーバー側では機器の情報は保持せず、
機器情報はNetBoxにホスト変数も含めて一括して保持しておき、
対象機器の情報取得をすることができました。

■まとめ

ホスト変数をPlaybook実行時にインベントリーと一緒にNetBoxから取得する
YAMLファイルと情報取得のPlaybookの書き方をご紹介しました。

NetBoxはAnsibleとの親和性が高く、組み合わせることで自動化の効果を高めることができそうです。

もしご興味がありましたら、ぜひNetBoxとAnsible、触ってみてください。

■参考資料

NetBox 公式ドキュメント
https://netbox.readthedocs.io/en/stable/
NetBoxインベントリーリソース Ansibleドキュメント
https://docs.ansible.com/ansible/latest/collections/netbox/netbox/nb_inventory_inventory.html

11
11
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
11
11