Triệu chứng chính: playbook báo unable to open shell, command timeout triggered, persistent connection idle timeout triggered, Authentication failed, hoặc module Junos báo ncclient is not installed / không kết nối được port 830, trong khi ssh tay vào router vẫn bình thường.
Lỗi thường gặp
| Triệu chứng | Nguyên nhân | Cách xử lý |
|---|---|---|
unable to open shell |
Thiếu/sai ansible_network_os, thiếu collection, sai mật khẩu, hoặc prompt lạ |
Đặt đúng ansible_network_os (cisco.iosxr.iosxr, junipernetworks.junos.junos), bật log (xem dưới) để thấy lỗi thật |
command timeout triggered, timeout value is 30 secs |
Lệnh chạy lâu hơn ansible_command_timeout (mặc định 30s): show tech, commit lớn, show run chassis lớn |
ansible_command_timeout: 180 cho group/host đó |
persistent connection idle timeout triggered |
Kết nối nền không nhận gì trong ansible_connect_timeout (mặc định 30s) |
Tăng ansible_connect_timeout, kiểm tra kết nối chập chờn |
connection type ssh is not valid for this module |
Dùng module mạng với ansible_connection: ssh |
Đổi sang ansible.netcommon.network_cli hoặc ansible.netcommon.netconf |
Authentication failed dù ssh tay được |
ssh tay dùng key/agent hoặc ~/.ssh/config, còn Ansible dùng password; hoặc AAA chặn |
Đặt ansible_user / ansible_password hay ansible_ssh_private_key_file rõ ràng |
ansible-pylibssh không cài / lỗi libssh |
network_cli ưu tiên libssh khi có ansible-pylibssh |
pip install ansible-pylibssh, hoặc ép ansible_network_cli_ssh_type: paramiko |
no acceptable kex algorithm / Incompatible ssh peer |
Thiết bị cũ chỉ có thuật toán SSH cũ, thư viện mới đã bỏ | Thử đổi ansible_network_cli_ssh_type, nâng SSH phía thiết bị; không hạ cấp crypto trên máy điều khiển |
Lỗi host key / known_hosts |
Router mới hoặc vừa đổi key | SSH tay một lần để nhận key, hoặc ANSIBLE_HOST_KEY_CHECKING=False chỉ trong lab |
Junos: ncclient is not installed |
Module Junos mặc định qua netconf |
pip install ncclient trong cùng venv chạy Ansible |
| Junos: connection refused port 830 | Chưa bật NETCONF | set system services netconf ssh, đặt ansible_port: 830 khi dùng netconf |
| IOS XR: task treo ở bước config | Session config khác đang giữ configure exclusive, hoặc còn Uncommitted changes |
Trên router: show configuration sessions, clear configuration sessions <id> |
Lỗi ngẫu nhiên khi -f lớn |
TACACS+/AAA hoặc router giới hạn số phiên SSH đồng thời | Giảm -f, hoặc serial: trong play |
| Chạy lại vẫn lỗi cũ dù đã sửa biến | Socket persistent cũ còn sống | Xoá ~/.ansible/pc/* rồi chạy lại |
Cấu hình đúng
ansible_network_os: cisco.iosxr.iosxr
ansible_connection: ansible.netcommon.network_cli
ansible_user: "{{ lookup('env', 'NET_USER') }}"
ansible_password: "{{ lookup('env', 'NET_PASS') }}"
ansible_command_timeout: 120
ansible_connect_timeout: 60
# ansible_network_cli_ssh_type: paramiko # bật nếu libssh có vấn đềansible_network_os: junipernetworks.junos.junos
ansible_connection: ansible.netcommon.netconf
ansible_port: 830
ansible_user: "{{ lookup('env', 'NET_USER') }}"
ansible_password: "{{ lookup('env', 'NET_PASS') }}"
ansible_command_timeout: 120Mật khẩu nên lấy từ biến môi trường hoặc vault, xem SOPS, age và ansible-vault. Inventory đầy đủ: Thu thập inventory IOS XR bằng Ansible.
Các bước khoanh vùng
- Mạng và SSH:
ssh user@host(network_cli) hoặcssh -p 830 user@host -s netconf(netconf) từ chính máy chạy Ansible. - Collection và thư viện: đúng collection, đúng venv có
paramiko/ansible-pylibssh/ncclient. - Ad-hoc 1 host: chạy 1 lệnh show qua module, loại trừ lỗi playbook.
- Bật log persistent connection: lỗi thật của
network_clinằm trong log, không hiện trên màn hình. - Tăng timeout có chủ đích: chỉ tăng cho group/task cần, không tăng toàn cục để che lỗi mạng.
Kiểm tra nhanh
# Collection và thư viện Python đang dùng
ansible-galaxy collection list | grep -Ei 'iosxr|junos|netcommon'
python3 -m pip list 2>/dev/null | grep -Ei 'ansible|paramiko|pylibssh|ncclient'
# Thử 1 host bằng ad-hoc
ansible -i inventory/hosts.yml pe1-hni -m cisco.iosxr.iosxr_command -a "commands='show version'"
ansible -i inventory/hosts.yml mx1-dng -m junipernetworks.junos.junos_command -a "commands='show version'"
# Bật log chi tiết của persistent connection
export ANSIBLE_LOG_PATH=~/ansible-debug.log
export ANSIBLE_DEBUG=True
ansible-playbook -i inventory/hosts.yml site.yml -l pe1-hni -vvvv
grep -Ei 'error|timeout|auth' ~/ansible-debug.log | tail -30
# Xem các giá trị cấu hình đã đổi khỏi mặc định
ansible-config dump --only-changed
# Dọn socket persistent cũ
rm -f ~/.ansible/pc/*ANSIBLE_DEBUG=True ghi rất nhiều, có thể lộ output lệnh và thông tin phiên. Chỉ bật khi debug, xoá file log sau đó.