admin管理员组文章数量:1337159
ansible网络设备自动巡检
- 前言
- 环境搭建
- 过程演练
- 结果验证
前言
一般中大型公司需要对网络设备进行定期巡检,当设备量比较大且巡检指标较多的时候,该项工作往往费时费力,同时如果完全采用人工巡检的话,还容易出现人为因素上的失误。
通过ansible
工具对网络设备进行自动化巡检,可以提高工作效率并且降低人为因素的失误。
本博客以ansible自动巡检华为ensp模拟设备,通过检查设备“是否开启snmp配置”为例,讲解网络设备高效巡检过程,相关输入参数如下:
ansible版本
: 2.8.1
设备厂商
: 华为路由器
主控节点
: Centos7服务器
环境搭建
网络环境(拓扑):
设备清单:
- R1:10.0.1.1(管理ip)
- R2:10.0.2.2(管理ip)
- R3:10.0.3.3(管理ip)
控制环境:
操作系统: CentOS7
ansible: 2.8.1 (此处采用docker自定义镜像,详情可参考我另外的博客《CentOS安装指定版本应用(ansible)》、《docker安装部署ansible并打包》、《ansible使用之——国产设备适配》、《配置华为设备ssh登录实现网络设备与VMWare虚拟机信息交互》)
过程演练
- 编辑inventory文件
hosts
[devices]
10.0.1.1
10.0.2.2
10.0.3.3
[devices:vars]
ansible_ssh_user: huawei
ansible_ssh_pass: Josen@12345
- 编辑
ansible
脚本hello.yml
---
- hosts: devices
vars:
ansible_connection: network_cli
ansible_ssh_user: huawei
ansible_ssh_pass: "Josen@12345"
ansible_network_os: vrp2
ansible_become: no
tasks:
- name: hello
cli_command:
command: "dis cur | include snmp"
register: run
- name: debug
debug:
msg: "{{ run.stdout | get_snmp }}"
- 编辑filter
import textfsm
class FilterModule(object):
def filters(self):
return {
'get_snmp': get_snmp
}
def get_snmp(data):
fsm_path = '/opt/ansible/plugins/textfsm/snmp.fsm'
with open(fsm_path, 'r') as fr:
fsm = textfsm.TextFSM(fr)
res = fsm.ParseTextToDicts(data)
print(res)
if res:
result = {"result": "yes"}
else:
result = {"result": "the device has not config the snmp"}
return result
textfsm文件
Value community (.+)
Value version (.+)
Start
^snmp-agent community read cipher ${community}
^snmp-agent sys-info version ${version}
结果验证
执行ansible-playbook -i hosts hello.yml
,结果如下:
版权声明:本文标题:ansible使用之——网络设备自动巡检 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1740415486a2273459.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论