はじめに
2026/09/17 リリースの ansible.netcommon コレクション 8.7.0 で、network_cli コネクションプラグイン使用時に「実際にどんなコマンドを実行して、どんなレスポンスが返ってきたのか」をファイルに記録する機能が加わりました。

ネットワーク系のPlaybook のデバッグとして、Ansible とネットワーク機器の間のやりとりを覗き込んで見てみたいと思ったことはないでしょうか?
通信レイヤーでなにか工夫すればできるかも知れませんが、Ansible の世界で簡単にできればなと思っていました。なので、個人的には待望の機能で、こっそりプルリクを追っていました。
さっそく試してみました。
- 検証環境
- ansible-core 2.21.3
- ansible.netcommon コレクション 8.7.0
- cisco.ios コレクション 11.6.0
検証 Playbook
Cisco IOS に対する以下のタスクで試してみます。
---
- name: Test play
hosts: ios01
gather_facts: false
tasks:
- name: Gather IOS facts
cisco.ios.ios_facts:
gather_subset:
- interfaces
この cisco.ios.ios_facts モジュールのこのオプションを実行したときにどんなコマンドが実行されるでしょうか。cisco.ios.ios_command のように、Playbook の書き手が直接 show コマンドを指定するわけではないので、パッと見では分かりません。ソースを追えば分かるでしょう。ただ、実際にどうだったかを手軽に、かつレスポンスを含めて確認できるのは、今回の新機能ならではないでしょうか。
実行
デフォルトでは従来通り記録はされません。ANSIBLE_NETWORK_CLI_RECORD という環境変数に 1 を指定して実行すると記録されます。
% ANSIBLE_NETWORK_CLI_RECORD=1 ansible-playbook -i inventory.ini ios_show.yml
PLAY [Test play] *************************************************************************************************
TASK [Gather IOS facts] ******************************************************************************************
ok: [ios01]
PLAY RECAP *******************************************************************************************************
ios01 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Playbook 実行ログ自体は普段と変わりません。
記録の確認
記録は /tmp/transcript-recordings/ ディレクトリ配下の .jsonlファイルとして残ります。(環境変数 ANSIBLE_NETWORK_CLI_RECORD_PATH で変更可)
% ls -1 /tmp/transcript-recordings
192.168.1.11_22.jsonl
ファイル名に含まれる _22 はポート番号です。
中身はこんな感じです。レスポンスはだいぶ省略しています。ちなみに、複数回 Playbook を実行すると追記されていきました。
{"command": "terminal length 0", "response": "", "timestamp": 1789652162.5164092}
{"command": "terminal width 512", "response": "", "timestamp": 1789652162.686284}
{"command": "terminal width 0", "response": "", "timestamp": 1789652162.840522}
{"command": "show version", "response": "Cisco IOS Software, (略)", "timestamp": 1789652163.004417}
{"command": "show version", "response": "Cisco IOS Software, (略)", "timestamp": 1789652163.20715}
{"command": "show inventory", "response": "NAME: \"IOSv\", DESCR: \"IOSv chassis, (略)", "timestamp": 1789652163.419025}
{"command": "show interfaces", "response": "GigabitEthernet0/0 is up, line protocol is up \n(略)", "timestamp": 1789652163.6333282}
{"command": "show ip interface", "response": "GigabitEthernet0/0 is up, line protocol is up\n(略)", "timestamp": 1789652163.8225641}
{"command": "show ipv6 interface", "response": "", "timestamp": 1789652163.9971762}
{"command": "show lldp", "response": "% LLDP is not enabled", "timestamp": 1789652164.14175}
{"command": "show cdp", "response": "Global CDP information:\n(略)", "timestamp": 1789652164.290456}
{"command": "show cdp neighbors detail", "response": "-------------------------\nDevice ID: (略)", "timestamp": 1789652164.5402439}
{"command": "terminal length 0", "response": "", "timestamp": 1789652162.5164092}
{"command": "terminal width 512", "response": "", "timestamp": 1789652162.686284}
{"command": "terminal width 0", "response": "", "timestamp": 1789652162.840522}
{"command": "show version", "response": "Cisco IOS Software, (略)", "timestamp": 1789652163.004417}
{"command": "show version", "response": "Cisco IOS Software, (略)", "timestamp": 1789652163.20715}
{"command": "show inventory", "response": "NAME: \"IOSv\", DESCR: \"IOSv chassis, (略)", "timestamp": 1789652163.419025}
{"command": "show interfaces", "response": "GigabitEthernet0/0 is up, (略)", "timestamp": 1789652163.6333282}
{"command": "show ip interface", "response": "GigabitEthernet0/0 is up, (略)", "timestamp": 1789652163.8225641}
{"command": "show ipv6 interface", "response": "", "timestamp": 1789652163.9971762}
{"command": "show lldp", "response": "% LLDP is not enabled", "timestamp": 1789652164.14175}
{"command": "show cdp", "response": "Global CDP information:\n(略)", "timestamp": 1789652164.290456}
{"command": "show cdp neighbors detail", "response": "-------------------------\nDevice ID: (略)", "timestamp": 1789652164.5402439}
いろんな show コマンドとその結果が見れました。モジュール自身が実行するコマンドの他にも、ターミナルプラグイン(今回は cisco.ios.ios)が実行している terminal length 0 などのコマンドも見れました。
ただ、よく見ると2セット出力されています(タイムスタンプが同じ行がある)が詳細は分かりません。
1行だけ切り取って普通の JSON にするとこんな感じです。フォーマットは直感的です。
{
"command": "show version",
"response": "Cisco IOS Software, (略)\n\n\n\nConfiguration register is 0x0",
"timestamp": 1789652163.004417
}
今回初めて知りましたが cisshgo という、Cisco IOS などの機器をテスト目的でエミュレートするツールで使うためのフォーマットのようです。これはこれでおもしろそう・・・。
日本語参考記事: 多数のNW機器をシミュレートするcisshgoについて | NTTテクノクロスブログ
設定系モジュールでも試してみる
参照系ではなく設定系のモジュールを使った例も試してみます。
- name: Test Configuration
cisco.ios.ios_config:
lines:
- description test
parents:
- interface GigabitEthernet0/0
結果は以下のとおりです。設定コマンド以外にもいろいろ出力されますね。show running-config が含まれるのは、事前にコンフィグを取得して、モジュールで指定したコンフィグがすでに設定されてるか確認するためのはずです。
{"command": "terminal length 0", "response": "", "timestamp": 1789651961.155453}
{"command": "terminal width 512", "response": "", "timestamp": 1789651961.339106}
{"command": "terminal width 0", "response": "", "timestamp": 1789651961.500611}
{"command": "show version", "response": "Cisco IOS Software, (略)", "timestamp": 1789651961.675512}
{"command": "show running-config", "response": "Building configuration...\n\n (略)\nend", "timestamp": 1789651963.862564}
{"command": "configure terminal", "response": "Enter configuration commands, one per line. End with CNTL/Z.", "timestamp": 1789651964.0429828}
{"command": "interface GigabitEthernet0/0", "response": "", "timestamp": 1789651964.256902}
{"command": "description test", "response": "", "timestamp": 1789651964.439345}
{"command": "end", "response": "", "timestamp": 1789651964.57701}
{"command": "terminal length 0", "response": "", "timestamp": 1789651961.155453}
{"command": "terminal width 512", "response": "", "timestamp": 1789651961.339106}
{"command": "terminal width 0", "response": "", "timestamp": 1789651961.500611}
{"command": "show version", "response": "Cisco IOS Software, (略)", "timestamp": 1789651961.675512}
{"command": "show running-config", "response": "Building configuration...\n\n (略)\nend", "timestamp": 1789651963.862564}
{"command": "configure terminal", "response": "Enter configuration commands, one per line. End with CNTL/Z.", "timestamp": 1789651964.0429828}
{"command": "interface GigabitEthernet0/0", "response": "", "timestamp": 1789651964.256902}
{"command": "description test", "response": "", "timestamp": 1789651964.439345}
{"command": "end", "response": "", "timestamp": 1789651964.57701}
今のところドキュメントには未掲載
今のところ、ansible.netcommon コレクションのドキュメントには本件の記載が見当たりません。なので、network_cli コネクションプラグインのコードにあるコメントを見るのが良いかなと思います。
ansible.netcommon/plugins/connection/network_cli.py at e15c19a707a8e490d5f61bb8a445cd044c321c70 · ansible-collections/ansible.netcommon · GitHub
あまり常用的に使うものでもないのかもしれません。
おわりに
ansible.netcommon コレクション 8.7.0 で導入された、ネットワーク機器とのコマンド・レスポンスを記録できる機能を試してみました。
個人的にはデバッグ用途で期待していた機能ですが、cisshgo にテストのための材料収集にも使えそうで、さらに興味が湧きました。