Ansible 网络设备自动化教程
Ansible 是当前最流行的 IT 自动化工具,在网络自动化领域同样表现出色。它采用无代理(Agentless)架构,通过 SSH 或 NETCONF 直接连接网络设备执行配置操作,无需在被管理设备上安装任何软件。Ansible 支持 Cisco、Juniper、Arista、Huawei 等主流网络设备,是实现 NetDevOps 的核心工具。本文介绍如何在搬瓦工 VPS 上安装 Ansible 并用于网络设备自动化管理。
一、安装 Ansible
apt update
apt install python3-pip python3-venv -y
# 创建虚拟环境
python3 -m venv /opt/ansible-env
source /opt/ansible-env/bin/activate
# 安装 Ansible 和网络模块
pip install ansible ansible-pylibssh paramiko netaddr
# 安装网络设备集合
ansible-galaxy collection install cisco.ios
ansible-galaxy collection install junipernetworks.junos
ansible-galaxy collection install arista.eos
ansible-galaxy collection install community.network
# 验证安装
ansible --version
二、配置清单文件(Inventory)
# 创建项目目录
mkdir -p /opt/network-automation && cd /opt/network-automation
# 创建清单文件
cat > inventory.yml << 'EOF'
all:
children:
routers:
hosts:
router1:
ansible_host: 192.168.1.1
ansible_network_os: cisco.ios.ios
router2:
ansible_host: 192.168.1.2
ansible_network_os: junipernetworks.junos.junos
switches:
hosts:
switch1:
ansible_host: 192.168.1.10
ansible_network_os: cisco.ios.ios
switch2:
ansible_host: 192.168.1.11
ansible_network_os: arista.eos.eos
vars:
ansible_user: admin
ansible_password: password123
ansible_connection: ansible.netcommon.network_cli
ansible_become: yes
ansible_become_method: enable
EOF
三、基本 Playbook 示例
3.1 获取设备信息
cat > gather_facts.yml << 'EOF'
---
- name: 收集网络设备信息
hosts: routers
gather_facts: no
tasks:
- name: 收集设备信息
cisco.ios.ios_facts:
gather_subset: all
when: ansible_network_os == 'cisco.ios.ios'
- name: 显示设备型号和版本
debug:
msg: "{{ ansible_net_model }} - {{ ansible_net_version }}"
EOF
ansible-playbook -i inventory.yml gather_facts.yml
3.2 批量配置 NTP
cat > configure_ntp.yml << 'EOF'
---
- name: 配置所有设备的 NTP 服务器
hosts: all
gather_facts: no
tasks:
- name: 配置 Cisco IOS NTP
cisco.ios.ios_ntp_global:
config:
servers:
- server: 216.239.35.0
prefer: true
- server: 216.239.35.4
state: merged
when: ansible_network_os == 'cisco.ios.ios'
- name: 配置 Junos NTP
junipernetworks.junos.junos_ntp_global:
config:
servers:
- server: 216.239.35.0
prefer: true
state: merged
when: ansible_network_os == 'junipernetworks.junos.junos'
EOF
3.3 备份设备配置
cat > backup_configs.yml << 'EOF'
---
- name: 备份所有网络设备配置
hosts: all
gather_facts: no
tasks:
- name: 备份 Cisco IOS 配置
cisco.ios.ios_config:
backup: yes
backup_options:
dir_path: /opt/network-automation/backups
filename: "{{ inventory_hostname }}_{{ lookup('pipe', 'date +%Y%m%d') }}.cfg"
when: ansible_network_os == 'cisco.ios.ios'
EOF
四、高级用法
4.1 使用 Jinja2 模板
# 创建配置模板
cat > templates/interface.j2 << 'EOF'
{% for intf in interfaces %}
interface {{ intf.name }}
description {{ intf.description }}
ip address {{ intf.ip }} {{ intf.mask }}
no shutdown
{% endfor %}
EOF
# 在 Playbook 中使用模板
cat > configure_interfaces.yml << 'EOF'
---
- name: 配置接口
hosts: routers
gather_facts: no
vars:
interfaces:
- name: GigabitEthernet0/1
description: "Uplink to Core"
ip: 10.0.1.1
mask: 255.255.255.0
tasks:
- name: 应用接口配置
cisco.ios.ios_config:
src: templates/interface.j2
EOF
4.2 合规性检查
cat > compliance_check.yml << 'EOF'
---
- name: 网络设备合规性检查
hosts: all
gather_facts: no
tasks:
- name: 检查 SSH 是否启用
cisco.ios.ios_command:
commands: show ip ssh
register: ssh_status
when: ansible_network_os == 'cisco.ios.ios'
- name: 检查 NTP 同步状态
cisco.ios.ios_command:
commands: show ntp status
register: ntp_status
when: ansible_network_os == 'cisco.ios.ios'
- name: 输出检查结果
debug:
msg: "NTP 状态: {{ ntp_status.stdout_lines }}"
when: ntp_status is defined
EOF
五、Ansible Vault 加密凭据
# 创建加密的密码文件
ansible-vault create group_vars/all/vault.yml
# 在文件中添加敏感信息
# vault_ansible_password: realpassword123
# 运行时解密
ansible-playbook -i inventory.yml playbook.yml --ask-vault-pass
六、与 NetBox 集成
# 安装 NetBox 清单插件
pip install pynetbox
# 配置动态清单从 NetBox 获取设备列表
cat > netbox_inventory.yml << 'EOF'
plugin: netbox.netbox.nb_inventory
api_endpoint: http://netbox.example.com
token: your_netbox_api_token
validate_certs: false
group_by:
- device_roles
- platforms
EOF
总结
Ansible 是网络自动化最容易上手的工具,其无代理架构和丰富的网络模块使其成为 NetDevOps 的核心。配合 NetBox 作为数据源和 Oxidized 进行配置备份,可以构建完整的网络自动化体系。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的优惠,通过 bwh81.net 进入官网购买。