【ChatGPTと一緒にAnsible化 Part1】Ciscoルータ(C890)にAnsibleを作成しようと思ったけど、Docker Rockyコンテナで格闘した話

ポイント

Rocky LinuxのDocker環境に構築したAnsibleを使用して、古いCiscoルータにBGP設定を一括適用しようとしたら…環境構築でまさかの大苦戦。

この記事は、ChatGPTとのやり取りをベースに、「Ansibleを実行するまでの戦い(環境構築編)」を記録したものです。
ansibleの適用は次回の記事で載せます。(次回の記事)

👉 関連記事:「ChatGPT-4oに相談しながら、MacにAnsible環境を構築してみた話【Docker+Rocky Linux 9】” https://www.runsurunet.com/2025/07/19/chatgpt%e3%81%ab%e7%9b%b8%e8%ab%87%e3%81%97%e3%81%aa%e3%81%8c%e3%82%89%e3%80%81mac%e3%81%abansible%e7%92%b0%e5%a2%83%e3%82%92%e6%a7%8b%e7%af%89%e3%81%97%e3%81%a6%e3%81%bf%e3%81%9f%e8%a9%b1%e3%80%90doc/

【ChatGPTと一緒にAnsible化 Part2】Ciscoルータ(C890)にAnsibleを適用した話
https://www.runsurunet.com/2025/08/02/%e3%80%90chatgpt%e3%81%a8%e4%b8%80%e7%b7%92%e3%81%abansible%e5%8c%96-part2%e3%80%91cisco%e3%83%ab%e3%83%bc%e3%82%bfc890%e3%81%abansible%e3%82%92%e9%81%a9%e7%94%a8%e3%81%97%e3%81%9f%e8%a9%b1/


ターゲットルータ

  • rt01: 10.1.100.40
  • rt02: 10.1.100.46
  • 端末からはSSHで接続
  • Cisco 890シリーズルータ × 2台
  • IOSバージョン:15.4(3)M6a
  • 設定内容: VLAN10, Loopback0, GigabitEthernet0, BGP (ピア構成)

ChatGPTとの実際のやりとりの記録(構成別に整理)

以下、わたし(筆者)とChatGPTのやりとりを会話形式で記録しつつ、問題解決の流れを解説していきます。


Step 1: 初期構成とCisco設定の準備

🧑‍💻 わたし「以下のCisco設定をansible playbookを作成してください」

📌 IPアドレス:

  • rt01:10.1.100.40/24
  • rt02:10.1.100.46/24

📌 構成内容: VLAN10, Loopback0, GigabitEthernet0, BGP設定

※各ルータの全文コンフィグはブログの一番下にも掲載しています

rt01 設定抜粋(10.1.100.40)※ページ最後に載せてます。

hostname rt01
interface Loopback0
 ip address 192.168.0.1 255.255.255.255
!
interface GigabitEthernet0
 ip address 10.1.100.40 255.255.255.0
 no shutdown
!
interface Vlan10
 ip address 192.168.10.1 255.255.255.0
!
ip routing
!
router bgp 65001
 bgp log-neighbor-changes
 network 192.168.0.0 mask 255.255.255.0
 neighbor 192.168.12.2 remote-as 65002

rt02 設定抜粋(10.1.100.46)※ページ最後に載せてます。

hostname rt02
interface Loopback0
 ip address 192.168.0.2 255.255.255.255
!
interface GigabitEthernet0
 ip address 10.1.100.46 255.255.255.0
 no shutdown
!
interface Vlan10
 ip address 192.168.10.2 255.255.255.0
!
ip routing
!
router bgp 65002
 bgp log-neighbor-changes
 network 192.168.0.0 mask 255.255.255.0
 neighbor 192.168.12.1 remote-as 65001

💻 ChatGPT「設定内容から、playbook構成例とtree構造、SSH接続情報を含むインベントリを提案します」


Step 2: group_vars を使わずPlaybookを統合化

今回は、あえてsimpleにしたかったので、変数を使わず、パラメータもベタ打ちに。

🧑‍💻 わたし「group_vars/all.ymlは作らず、cisco_config.ymlに全部含めて」

💻 ChatGPT「OKです。以下のようにPlaybookにすべて統合できます」

---
- name: Configure Cisco routers (rt01 / rt02)
  hosts: cisco
  gather_facts: no
  connection: network_cli
  tasks:
    - name: Apply base config to routers
      ios_config:
        lines:
          - hostname rt01
          - ip routing

    - name: Set hostname
      ios_config:
        lines:
          - hostname rt01

    - name: Configure loopback interface
      ios_config:
        lines:
          - interface Loopback0
          - ip address 192.168.0.1 255.255.255.255

    - name: Configure GigabitEthernet0 interface
      ios_config:
        lines:
          - interface GigabitEthernet0
          - ip address 10.1.100.40 255.255.255.0
          - no shutdown

    - name: Configure VLAN10 for SSH
      ios_config:
        lines:
          - interface Vlan10
          - ip address 192.168.10.1 255.255.255.0

    - name: Configure BGP settings
      ios_config:
        lines:
          - router bgp 65001
          - bgp log-neighbor-changes
          - network 192.168.0.0 mask 255.255.255.0
          - neighbor 192.168.12.2 remote-as 65002

🧑‍💻 わたし「tree構造を教えて」

💻 ChatGPT「以下のような構成になります:

project_root/
├── inventory/
│   └── hosts
├── playbooks/
│   └── cisco_config.yml

💻 ChatGPT「Playbookの実行コマンドはこちらです」

ansible-playbook -i inventory/hosts playbooks/cisco_config.yml -l rt02

Step 3: paramiko未導入エラーとpip不足

ここにSTEP2のplaybookを実行したものの、エラーが出て先に進まなかった。結論は、SSH接続に必要なPythonライブラリ「paramiko」が未導入だったことが原因だった。

🔍 paramikoとは? Paramikoは、Python製のSSH通信ライブラリで、Ansibleなどがネットワーク機器と通信する際のSSH接続手段として使われる。これが入っていないと、SSH接続に失敗するため、Ansibleでネットワーク機器を操作するには必須のモジュールである。

このとき、エラーの内容をChatGPTにそのまま貼り付けながら、一つずつ対処法を確認していった。

🧑‍💻 わたし「エラーが出ました。paramiko is not installed」

TASK [Apply base config to routers] ********************************************************************************************************
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
fatal: [rt02]: FAILED! => {"changed": false, "msg": "paramiko is not installed: No module named 'paramiko'"}

💻 ChatGPT「SSH接続に必要なparamikoが未導入です。pipを先に導入しましょう」

dnf install -y python3-pip
pip3 install paramiko

🧑‍💻 わたし「pipもない、dnf install python3-pipしてみる」

💻 ChatGPT「その後、paramiko を以下のコマンドでインストールしてください」

pip3 install paramiko

🧑‍💻 わたし「今度はbcryptやpynaclでsetup.pyエラー」

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-997arsh3/bcrypt/

💻 ChatGPT「以下の依存パッケージをインストールすればエラーは解消されます」

dnf install -y gcc make python3-devel libffi-devel openssl-devel libsodium-devel

🧑‍💻 わたし「Rustがないって怒られた」

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/pip-build-785_iva5/bcrypt/setup.py", line 11, in <module>
    from setuptools_rust import RustExtension
ModuleNotFoundError: No module named 'setuptools_rust'

💻 ChatGPT「Rust環境と setuptools-rust を導入してください」

dnf install -y rust cargo
pip3 install setuptools-rust

Step 4: Pythonバージョンの不一致と環境分離の罠

ここでもつまずいた。出てきたエラーをそのままChatGPTに貼り付けて、一つひとつ一緒に解決していくことにした。

🧑‍💻 わたし「paramiko成功、でもansibleは見えていない様子」

💻 ChatGPT「Ansibleが使用しているPythonとpipが使っているPythonが違う可能性が高いです」

🧑‍💻 わたし「head -n 1 /usr/bin/ansible → #!/usr/bin/python3.12」

💻 ChatGPT「Python 3.12 に paramiko を直接インストールしてください」

/usr/bin/python3.12 -m ensurepip --upgrade
/usr/bin/python3.12 -m pip install paramiko

🧑‍💻 わたし「それでもまた ‘paramiko not installed’ が出る」

💻 ChatGPT「pipが –user で入れているため、Ansibleから見えていません。–break-system-packages を使ってインストールしましょう」

/usr/bin/python3.12 -m pip install paramiko --break-system-packages

Step 5: SSH認証エラーと privilege の落とし穴

開発環境の構築まではうまくいったものの、いざ実行しようとするとエラーが発生して前に進めない状態が続いた。

そこで、再びChatGPTに壁打ちを続けながら、一つずつ問題を解消していくことにした。

🧑‍💻 わたし「ansible-playbook 実行。今度は transport shut down or saw EOF」

💻 ChatGPT「ログイン直後がすでに特権モード(#)になっているため、Ansibleが想定する ‘>’ 状態からの昇格処理に失敗しています」

💡 Ansibleは「ユーザーモード(>)」→「enableコマンドで昇格(#)」という手順を前提にしている

🧑‍💻 わたし「username runsuru privilege 1 に変更して再試行します」

💻 ChatGPT「OK、その上で login local, transport input ssh の設定がline vtyに入っているかも確認してください」

conf t
username runsuru privilege 1 password 0 cisco123
line vty 0 4
 login local
 transport input ssh
end

Step 6: parents指定の修正と最終成功!

🧑‍💻 わたし「show run | section line vty で確認、login local あり」

💻 ChatGPT「今度は login local コマンドをグローバルで2回送ってエラーになっている可能性、parents指定の提案」

🧑‍💻 わたし「PYTHONPATHなしで実行できるようにしたい」

💻 ChatGPT「おすすめは ~/.bashrc に書く方法 または pip install –break-system-packages」

🧑‍💻 わたし「~/.bashrc に書いてみます」

echo 'export PYTHONPATH=$HOME/.local/lib/python3.12/site-packages' >> ~/.bashrc
source ~/.bashrc

💡 必要であれば、別のパターンの parents 使用例や ~/.bashrc の設定記述例もお渡しできます。ご希望あればお知らせください。

Step 6: parents指定の修正と最終成功!

「必要であれば、別のパターンの parents 使用例や ~/.bashrc の設定記述例もお渡しできます。


✅ 最終実行結果(ansible-playbook

plaintext

コピーする編集する

PLAY [Configure Cisco routers (rt01 / rt02)] ********************************************************************* TASK [Apply base config to routers] ****************************************************************************** changed: [rt02] TASK [Set hostname] ********************************************************************************************** ok: [rt02] TASK [Configure loopback interface] ****************************************************************************** changed: [rt02] TASK [Configure GigabitEthernet0 interface] ********************************************************************** changed: [rt02] TASK [Configure VLAN10 for SSH] ********************************************************************************** changed: [rt02] TASK [Configure BGP settings] ************************************************************************************ changed: [rt02] TASK [Set login local for SSH] *********************************************************************************** changed: [rt02] PLAY RECAP ******************************************************************************************************* rt02 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

📝 補足:この結果から、ok=7 changed=6 で全タスクが正常に完了したことが確認できました。


まとめ

Ansibleの構築やネットワーク機器との接続は、思いがけないトラブルの連続でした。 しかし、ChatGPTにエラー内容をそのまま貼り付けて相談することで、検索よりも確実でスピーディに問題を解決することができました。

うまくいかないことがあっても、ひとつずつエラーを共有しながら壁打ちしていけば、必ず前に進める。そんな希望を感じた体験でした。

「困ったときは相棒(ChatGPT)に聞いてみる」——これは、これからの技術者の基本スタイルになっていくのかもしれません。

次回予告(Part2)

Part2では、ようやく動作したAnsible Playbookを実行し、実際にCiscoルータへ設定反映する様子を記録します。

実行ログ、設定反映の状態、差分の取り方など「Playbook実行編」をお楽しみに!

【ChatGPTと一緒にAnsible化 Part2】Ciscoルータ(C890)にAnsibleを適用した話
https://www.runsurunet.com/2025/08/02/%e3%80%90chatgpt%e3%81%a8%e4%b8%80%e7%b7%92%e3%81%abansible%e5%8c%96-part2%e3%80%91cisco%e3%83%ab%e3%83%bc%e3%82%bfc890%e3%81%abansible%e3%82%92%e9%81%a9%e7%94%a8%e3%81%97%e3%81%9f%e8%a9%b1/

ひとこと

AnsibleもChatGPTも使い方次第。 けれどハマるたびに直してくれる、これがわたしの「相棒」の力。 わたしはChatGPTに話しかけるとき、相棒と呼びます。

参考 rt01とrt02のコンフィグ

rt01#show running-config
Building configuration...

Current configuration : 2190 bytes
!
! Last configuration change at 06:38:17 UTC Wed Feb 6 2036 by runsuru
!
version 15.4
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
!
hostname rt01
!
boot-start-marker
boot-end-marker
!
!
enable secret 5 $1$QRGS$zQSuKF1HenbywLgACpwAp/
!
no aaa new-model
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!


!
!
!
!
ip cef
no ipv6 cef
!
!
!
!
!
multilink bundle-name authenticated
!
!
!
!
!
!
!
cts logging verbose
license udi pid CISCO892-K9 sn FGL154123NR
!
!
vtp mode transparent
username runsuru privilege 15 password 7 01100F175804575D72
!
redundancy
!
!
!
!
!
vlan 10
lldp run
!
!
!
!
!
!
!
!
!
!
!
interface Loopback0
 ip address 1.1.1.1 255.255.255.255
!
interface BRI0
 no ip address
 encapsulation hdlc
 shutdown
 isdn termination multidrop
!
interface FastEthernet0
 switchport access vlan 10
 no ip address
!
interface FastEthernet1
 no ip address
!
interface FastEthernet2
 no ip address
!
interface FastEthernet3
 no ip address
!
interface FastEthernet4
 no ip address
!
interface FastEthernet5
 no ip address
!
interface FastEthernet6
 no ip address
!
interface FastEthernet7
 no ip address
!
interface FastEthernet8
 no ip address
 shutdown
 duplex auto
 speed auto
!
interface GigabitEthernet0
 ip address 192.168.12.1 255.255.255.252
 duplex auto
 speed auto
!
interface Vlan1
 no ip address
!
interface Vlan10
 description "### for ssh ###"
 ip address dhcp
!
router bgp 65001
 bgp log-neighbor-changes
 network 1.1.1.1 mask 255.255.255.255
 neighbor PEERS peer-group
 neighbor PEERS remote-as 65002
 neighbor PEERS description BGP peer group to downstream routers
 neighbor PEERS password 7 045802150C2E1D1C5A
 neighbor PEERS timers 5 15
 neighbor 192.168.12.2 peer-group PEERS
!
ip forward-protocol nd
no ip http server
no ip http secure-server
!
!
ip ssh version 2
!
!
!
!
control-plane
!
!
mgcp behavior rsip-range tgcp-only
mgcp behavior comedia-role none
mgcp behavior comedia-check-media-src disable
mgcp behavior comedia-sdp-force disable
!
mgcp profile default
!
!
!
!
!
!
!
!
line con 0
line aux 0
line vty 0 4
 login local
 rotary 1
 transport input ssh
line vty 5 15
 login local
 rotary 1
 transport input ssh
!
!
end

rt02

Building configuration...

Current configuration : 2379 bytes
!
! Last configuration change at 14:59:45 UTC Wed Feb 6 2036 by runsuru
!
version 15.4
service timestamps debug datetime msec
service timestamps log datetime msec
service password-encryption
!
hostname rt02
!
boot-start-marker
boot-end-marker
!
!
enable secret 5 $1$QRGS$zQSuKF1HenbywLgACpwAp/
!
no aaa new-model
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!


!
!
!
!
ip domain name example.local
ip cef
no ipv6 cef
!
!
!
!
!
multilink bundle-name authenticated
!
!
!
!
!
!
!
cts logging verbose
license udi pid CISCO892-K9 sn FGL15462182
!
!
vtp mode transparent
username runsuru privilege 15 password 7 01100F175804575D72
!
redundancy
!
!
!
!
!
vlan 2,10
!
vlan 101
 name OSPF
!
vlan 102
 name static
!
vlan 104
 name bgp02
!
vlan 110
 name test-1
!
vlan 111
 name test-2
!
vlan 901
 name rip
!
vlan 999
 name mgmt
lldp run
!
!
!
!
!
!
!
!
!
!
!
interface Loopback0
 ip address 2.2.2.2 255.255.255.255
!
interface BRI0
 no ip address
 encapsulation hdlc
 shutdown
 isdn termination multidrop
!
interface FastEthernet0
 switchport access vlan 10
 no ip address
!
interface FastEthernet1
 no ip address
!
interface FastEthernet2
 no ip address
!
interface FastEthernet3
 no ip address
!
interface FastEthernet4
 no ip address
!
interface FastEthernet5
 no ip address
!
interface FastEthernet6
 no ip address
!
interface FastEthernet7
 no ip address
!
interface FastEthernet8
 no ip address
 shutdown
 duplex auto
 speed auto
!
interface GigabitEthernet0
 ip address 192.168.12.2 255.255.255.252
 duplex auto
 speed auto
!
interface Vlan1
 no ip address
!
interface Vlan10
 description "### for ssh ###"
 ip address dhcp
!
router bgp 65002
 bgp log-neighbor-changes
 network 2.2.2.2 mask 255.255.255.255
 neighbor PEERS peer-group
 neighbor PEERS remote-as 65001
 neighbor PEERS description BGP peer group to upstream routers
 neighbor PEERS password 7 060506324F41584B56
 neighbor PEERS timers 5 15
 neighbor 192.168.12.1 peer-group PEERS
!
ip forward-protocol nd
no ip http server
no ip http secure-server
!
!
ip ssh version 2
!
!
!
!
control-plane
!
!
mgcp behavior rsip-range tgcp-only
mgcp behavior comedia-role none
mgcp behavior comedia-check-media-src disable
mgcp behavior comedia-sdp-force disable
!
mgcp profile default
!
!
!
!
!
!
!
!
line con 0
line aux 0
line vty 0 4
 login local
 rotary 1
 transport input ssh
line vty 5 15
 login local
 rotary 1
 transport input ssh
!
!
end

Follow me!

【ChatGPTと一緒にAnsible化 Part1】Ciscoルータ(C890)にAnsibleを作成しようと思ったけど、Docker Rockyコンテナで格闘した話” に対して2件のコメントがあります。

コメントを残す