てくなべ (tekunabe)

ansible / network automation / 学習メモ

Ansible ネットワークモジュールにおける facts の扱い

■ はじめに

Ansible のネットワークモジュールは、通常のモジュールとは facts の扱いが異なります。 この記事では、ネットワークモジュール固有の facts の扱い方についてご紹介します。 想定バージョンは Ansible 2.5.2 です。


■ 通常の facts は ネットワーク機器側ではなく Ansible ホスト側

ios_commandjunos_config などのネットワークモジュールは、Ansible ホスト側で Python スクリプトが実行されます。 そのため、 gather_facts: yes (デフォルト) で取得される facts は、操作対象のネットワーク機器側ではなく、Ansible ホスト側の facts になります。


■ ネットワーク機器側の facts は専用モジュールで取得

ネットワーク機器側のホスト名や、インターフェース情報などの facts を取得するにためには、 ios_facts や junos_facts など、ネットワークプラットフォームごとに用意された専用の *_facts モジュールを利用します。

サンプルPlaybook

Junosの facts を取得するjunos_facts モジュールを利用するサンプルです。

gather_subset オプションを省略した場合は、コンフィグ以外の facts を取得します。

- hosts: junos
  connection: netconf
  gather_facts: no

  tasks:
    - name: facts
      junos_facts:

    - name: debug
      debug:
        msg: "{{ ansible_facts }}"

ここでは、Ansible ホスト側の facts は必要ないとしているので、 gather_facts: no にしています。 また、msg: "{{ ansible_facts }}" と指定しているように、Ansible 2.5 から facts は ansible_facts.* という名前空間配下でも参照可能です

実行結果

(ansible25) [vagrant@centos7 vagrant]$ ansible-playbook -i inventory junos_test.yml --tag=d

PLAY [junos] *************************************************************************************************

TASK [facts] *************************************************************************************************
ok: [172.16.0.1]

TASK [debug] *************************************************************************************************
ok: [172.16.0.1] => {
    "msg": {
        "net_filesystems": [
            "/dev/ad0s1a",
            "devfs",
            "/dev/md0",
            "/cf",
            "devfs",
            "procfs",
            "/dev/bo0s1e",
            "/dev/md1",
            "/cf/var/jail",
            "/cf/var/log",
            "devfs",
            "/dev/md2"
        ],
        "net_gather_subset": [
            "hardware",
            "default",
            "interfaces"
        ],
        "net_has_2RE": false,
        "net_hostname": "vsrx1",
        "net_interfaces": {
            ".local.": {
                "admin-status": "up",
                "macaddress": "Unspecified",
                "mtu": "Unlimited",
                "oper-status": "up",
                "speed": "Unlimited",
                "type": "Loopback"
            },
            "dsc": {
                "admin-status": "up",
                "macaddress": "Unspecified",
                "mtu": "Unlimited",
                "oper-status": "up",
                "speed": "Unspecified",
                "type": "Software-Pseudo"
            },
            "ge-0/0/0": {
                "admin-status": "up",
                "macaddress": "08:00:27:ae:**:**",
                "mtu": "1514",
                "oper-status": "up",
                "speed": "1000mbps",
                "type": null
            },
            "ge-0/0/1": {
                "admin-status": "up",
                "macaddress": "08:00:27:af:**:**",
                "mtu": "1514",
                "oper-status": "up",
                "speed": "1000mbps",
                "type": null
            },
            (略)
            }
        },
        "net_memfree_mb": 76448,
        "net_memtotal_mb": 1031572,
        "net_model": "firefly-perimeter",
        "net_modules": [
            {
                "name": "Midplane"
            },
            {
                "name": "System IO"
            },
            {
                "description": "FIREFLY-PERIMETER RE",
                "name": "Routing Engine"
            },
            {
                "description": "Virtual FPC",
                "name": "FPC 0"
            },
            {
                "name": "Power Supply 0"
            }
        ],
        "net_routing_engines": {
            "null": {
                "cpu_background": "0",
                "cpu_idle": "100",
                "cpu_interrupt": "0",
                "cpu_system": "0",
                "cpu_user": "0",
                "last_reboot_reason": "Router rebooted after a normal shutdown.",
                "load_average_fifteen": "0.00",
                "load_average_five": "0.02",
                "load_average_one": "0.00",
                "memory_control_plane": "594",
                "memory_control_plane_used": "321",
                "memory_control_plane_util": "54",
                "memory_data_plane": "430",
                "memory_data_plane_used": "211",
                "memory_data_plane_util": "49",
                "memory_system_total": "1024",
                "memory_system_total_used": "532",
                "memory_system_total_util": "52",
                "model": "FIREFLY-PERIMETER RE",
                "slot": null,
                "start_time": "2018-04-11 13:23:07 UTC",
                "status": "Testing",
                "up_time": "1 day, 17 hours, 40 minutes, 58 seconds"
            }
        },
        "net_serialnum": "a9039f******",
        "net_version": null
    }
}

PLAY RECAP ***************************************************************************************************
172.16.0.1                 : ok=2    changed=0    unreachable=0    failed=0

(ansible25) [vagrant@centos7 vagrant]$

ホスト名 vsrx1 や、インターフェースの情報などが facts として取得できたことが分かります。


■ 補足

https://www.ansible.com/blog/coming-soon-networking-features-in-ansible-2.5 上記の Ansible 2.5 正式リリース前の公式ブログに、

Fact gathering for network_cli and netconf connection methods are not turned on by default.

という記述がありますが、実際は異なるようです。ネットワークモジュールのベストプラクティスはあくまでも、必要でない限りは、gather_facts: no を指定しておくことのようです。