一、需求说明

  信息系统日常运维中除了服务器,我们还有很多网络设备,比如路由器、交换机、防火墙等。虽然从专业线上分类由主机工程师、网络工程师分别管理,但是在很多小单位可能配置的岗位并不充足,往往是一人身兼数个角色。很荣幸,Ansible也支持网络设备的管理,本文介绍如何使用Ansible管理华为交换机,目前只支持管理CE系列交换机。
环境说明:

  • Ansible主控机操作系统:centos 8.0.1905
  • Ansible版本:2.9.21
  • python版本:3.6.13
  • 交换机型号:CE6800
  • 交换机软件版本:V200R005C10SPC607B607

二、配置步骤

1、查看华为相关的模块

如下所示跟华为相关的模块有68个,均以ce开头
[root@centos8 ~]# ansible-doc -l |grep -i huawei |wc -l
68
[root@centos7 ~]# ansible-doc -l |grep -i huawei
ce_aaa_server Manages AAA server global configuration on HUAWEI CloudEngine switches
ce_aaa_server_host Manages AAA server host configuration on HUAWEI CloudEngine switches
ce_acl Manages base ACL configuration on HUAWEI CloudEngine switches

hwc_network_vpc Creates a Huawei Cloud VPC
hwc_smn_topic Creates a resource of SMNTopic in Huaweicloud Cloud

2、使用ce_command模块查看交换机信息

  • 查看交换机路由信息
    使用命令:[root@centos8 ~]# ansible -m ce_command -a “commands=‘display ip routing-table’ transport=‘cli’ host=192.168.200.100 port=22 username=admin1 password=123456” localhost --connection local

执行后查看的路由信息如下:
在这里插入图片描述

  • 查看vlan信息
    使用命令:[root@centos8 ansible]# ansible -m ce_command -a “commands=‘display vlan sum’ transport=‘cli’ host=192.168.200.100 port=22 username=admin1 password=123456” localhost --connection local

在这里插入图片描述

3、配置交换机组的inventory文件

[root@centos8 ansible]# cat switchs
[hw]
192.168.200.100 ansible_ssh_port=22 ansible_ssh_user=admin1 ansible_ssh_pass=123456

4、配置playbook文件

创建备份交换机配置的playbook,备份到目录/tmp/backup
[root@centos7 ansible]# vim ce.yml

[root@centos8 ansible]# cat ce.yaml 
- name: CE switch command test.
  hosts: hw
  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: configurable backup path
    ce_config:
      lines: sysname {{ inventory_hostname }}
      provider: "{{ cli }}"
      backup: yes
      backup_options:
        filename: backup.cfg
        dir_path: /tmp/backup  

5、执行playbook任务

[root@centos8 ansible]# ansible-playbook -i switchs ce.yaml
在这里插入图片描述

三、QA

1、执行ansible-playbook时报错No module named ‘ncclient’

[root@centos8 ansible]# pip3 install ncclient

2、执行playbook时报错"Unable to decode JSON from response to exec_command(**). Received ‘None’.

在这里插入图片描述
写在最后,尝试了各种python版本、操作系统版本都是报这个错,估计与eNSP模拟器ce6800有关。折腾了3天了,只能放弃了。直接执行是成功的,执行play-book报错没有收到交换机的任何信息。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐