使用华为ansible CE模块管理华为交换机
使用华为ansible CE模块管理华为交换机
华为自己开源了CE交换机ansible管理模块,用ansible管理交换机那是相当的方便,现在ansible CE模块数量已经到了60多个,ansible脚本已经能满足大部分的自动运维管理工作了
测试环境
- VMware 虚拟机,ubuntu20.04-lts;
- 华为ESNP模拟器,V100R300C00SPC100;
安装部署
python配置
ubuntu虚拟机安装部署好后,系统自带python3,需要安装pip3,再通过pip3安装ncclient模块
apt install python3-pip
pip3 install ncclient
我的虚拟机上两条命令就完成python的配置,在另外一台树莓派上ansible脚本没有运行成功,报错python没有安装paramiko模块,还有某些依赖包没有安装,不一一列举了,百度一下就能解决
ansible
ansible安装配置很简单,直接用apt命令安装就行,且自带华为的CE模块,不需要去华为的Github上去找源码安装,GitHub上的已经三年没更新,bug一堆
apt install ansible
配置ansible的配置文件/etc/ansible/ansible.cfg,这里要把host_key_cheking = False 取消注释,原因是不注释掉,如果本机从来没有ssh登录交换机,ansible检查用户目录下.ssh/known_hosts文件,发现没有交换机的key值,不会执行命令脚本
/etc/ansible/hosts文件,配置登录参数和交换机清单
[all:vars]
ansible_connection=local
ansible_ssh_user=huawei
ansible_ssh_pass=Changeme_123
ansible_ssh_port=22
[cloudengine]
192.168.2.10
ansible playbook脚本测试
脚本文件ce.yml,测试ce_interface模块,批量打开交换机的端口
下图可见1-9口原处于shutdown状态
ce.yml文件如下,作用是打开2-9端口
- name: CloudEngine ce_interface test
hosts: cloudengine
connection: local
gather_facts: no
vars:
cli:
host: "{{ inventory_hostname }}"
port: "{{ ansible_ssh_port }}"
username: "{{ ansible_ssh_user }}"
password: "{{ ansible_ssh_pass }}"
transport: cli
tasks:
- name: "Admin up interfaces"
ce_interface:
interface: "{{ item }}"
admin_state: up
provider: "{{ cli }}"
with_items:
- ge1/0/2
- ge1/0/3
- ge1/0/4
- ge1/0/5
- ge1/0/6
- ge1/0/7
- ge1/0/8
- ge1/0/9
执行ansible-playbook ce.yml,结果如下
去交换机上看结果,2-9端口都处于up状态了
再用dis c c c命令查看交换机配置变更历史
实验成功!