运维少年系列 - ansible and cisco(1)

运维少年系列 - ansible and cisco(1),第1张

概述运维少年系列-ansibleandcisco(1)骚年运维少年ansibleandcisco一、系列文章说明二、先决条件三、ansible简介四、安装ansible五、使用ansible管理单个switch1)拓扑图2)配置3)adhoc命令六、使用ansible管理多个switch一、系列文章说明本系列文章主要讲如何使用ansible去 运维少年系列 - ansible and cisco(1)

骚年 运维少年

ansible and cisco

一、系列文章说明@H_404_7@二、先决条件@H_404_7@三、ansible 简介@H_404_7@四、安装ansible@H_404_7@五、使用ansible管理单个switch@H_404_7@1)拓扑图@H_404_7@2)配置@H_404_7@3)ad hoc命令@H_404_7@六、使用ansible管理多个switch

一、系列文章说明

本系列文章主要讲如何使用ansible去批量管理和配置cisco交换机路由器。当然,本系列文章也会分为2-3篇去讲,从单个设备到多个设备,无playbook到有playbook。

二、先决条件CCNA知识了解YAML了解linux的使用三、ansible 简介

ansible是一个基于python的自动化运维工具,其主要使用到的python模块有我们之前讲过的paramiko和没讲过的jinja2,ansible提供了一个更加简单的方式去管理大批量的设备。如果读者需要了解更具体的解释,请自行百度.

四、安装ansible

centos 使用yum来安装ansible

 1[root@yunwei ~]# yum install ansible -y 2DependencIEs Resolved 3 4============================================================================================================================================================================================= 5 Package                                              Arch                                   Version                                           Repository                               Size 6============================================================================================================================================================================================= 7Installing: 8 ansible                                              noarch                                 2.4.2.0-2.el7                                     extras                                  7.6 M 9Installing for dependencIEs:10 PyYAML                                               x86_64                                 3.10-11.el7                                       base                                    153 k11 libyaml                                              x86_64                                 0.1.4-11.el7_0                                    base                                     55 k12 python-babel                                         noarch                                 0.9.6-8.el7                                       base                                    1.4 M13 python-cffi                                          x86_64                                 1.6.0-5.el7                                       base                                    218 k14 python-enum34                                        noarch                                 1.0.4-1.el7                                       base                                     52 k15 python-httplib2                                      noarch                                 0.9.2-1.el7                                       extras                                  115 k16 python-IDna                                          noarch                                 2.4-1.el7                                         base                                     94 k17 python-jinja2                                        noarch                                 2.7.2-3.el7_6                                     updates                                 518 k18 python-paramiko                                      noarch                                 2.1.1-9.el7                                       updates                                 269 k19 python-passlib                                       noarch                                 1.6.5-2.el7                                       extras                                  488 k20 python-ply                                           noarch                                 3.4-11.el7                                        base                                    123 k21 python-pycparser                                     noarch                                 2.14-1.el7                                        base                                    104 k22 python-six                                           noarch                                 1.9.0-2.el7                                       base                                     29 k23 python2-cryptography                                 x86_64                                 1.7.2-2.el7                                       base                                    502 k24 python2-jmespath                                     noarch                                 0.9.0-3.el7                                       extras                                   39 k25 python2-pyasn1                                       noarch                                 0.1.9-7.el7                                       base                                    100 k26 sshpass                                              x86_64                                 1.06-2.el7                                        extras                                   21 k2728Transaction Summary29=============================================================================================================================================================================================30Install  1 Package (+17 Dependent packages)

可以看到安装了很多python的模块,比如paramiko和jinja2

安装完毕之后,会自动生成/etc/ansible/目录
1[root@yunwei ~]# ls /etc/ansible/2ansible.cfg  hosts  roles3[root@yunwei ~]# 

ansible.cfg

ansible.cfg是ansible的配置文件,关于ansible的默认配置都在里面,可以修改。

hosts@H_404_7@hosts文件是主机名/IP文件,这个文件指定了ansible的作用域。五、使用ansible管理单个switch1)拓扑图

2)配置修改ansible配置文件@H_404_7@修改ansible配置文件,将一下行注释去掉,这一行的意思是,在ssh登录的时候,是检查kNown_hosts文件中是否有该IP的密钥。默认是检查的,如果不存在,那么就登录失败,这时候可以通过手动ssh一次,ssh会自动将密钥添加入kNown_hosts文件中,或者通过取消注释,使其不检查文件。
1host_key_checking = False
清空并配置hosts文件@H_404_7@只有主机存在于hosts文件中,我们才能对这个主机做 *** 作。
1[root@yunwei ansible]# echo > hosts2[root@yunwei ansible]# cat hosts3[cisco]  # 组名4192.168.108.251 # 组成员5192.168.108.2526192.168.108.2537[centos]8192.168.100.2259[root@yunwei ansible]# 
使用ansible命令查看主机是否可达(注意:我的GNS上的IOS不支持一下Ping模块)
1[root@yunwei ansible]# ansible 192.168.100.225 -m Ping -u root -k2SSH password: 3192.168.100.225 | SUCCESS => {4    "changed": false, 5    "Ping": "pong"6}
参数解释m:指定使用的模块,所有的网络模块可以点这里查看u:指定登录的用户名k:交互式输入密码3)ad hoc命令

ad hoc命令,一般只会执行一条命令,功能单一。

使用ansible查询交换机的vlan信息
 1[root@yunwei ansible]# ansible 192.168.108.251 -m raw -a "show vlan" -u cisco -k 2SSH password:  3192.168.108.251 | SUCCESS | rc=0 >> 4 5 6VLAN name                             Status    Ports 7---- -------------------------------- --------- ------------------------------- 81    default                          active    Et0/0, Et0/2, Et0/3, Et1/0 9                                                Et1/1, Et1/2, Et1/3, Et2/010                                                Et2/1, Et2/2, Et2/3, Et3/011                                                Et3/1, Et3/2, Et3/31210   V10                              active    1320   V20                              active    1430   V30                              active    1540   V40                              active    1650   V50                              active    1760   V60                              active    1870   V70                              active    1980   V80                              active    2090   V90                              active    21100  V100                             active    221002 fddi-default                     act/unsup 231003 token-ring-default               act/unsup 241004 fddinet-default                  act/unsup 251005 trnet-default                    act/unsup 2627VLAN Type  SAID       MTU   Parent RingNo BrIDgeNo Stp  BrdgMode Trans1 Trans228---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------291    enet  100001     1500  -      -      -        -    -        0      0   3010   enet  100010     1500  -      -      -        -    -        0      0   3120   enet  100020     1500  -      -      -        -    -        0      0   3230   enet  100030     1500  -      -      -        -    -        0      0   3340   enet  100040     1500  -      -      -        -    -        0      0   3450   enet  100050     1500  -      -      -        -    -        0      0   3560   enet  100060     1500  -      -      -        -    -        0      0   3670   enet  100070     1500  -      -      -        -    -        0      0   3780   enet  100080     1500  -      -      -        -    -        0      0   3890   enet  100090     1500  -      -      -        -    -        0      0   39100  enet  100100     1500  -      -      -        -    -        0      0   401002 fddi  101002     1500  -      -      -        -    -        0      0   411003 tr    101003     1500  -      -      -        -    -        0      0   421004 fdnet 101004     1500  -      -      -        IEee -        0      0   431005 trnet 101005     1500  -      -      -        ibm  -        0      0   4445Remote SPAN VLANs46------------------------------------------------------------------------------474849Primary Secondary Type              Ports50------- --------- ----------------- ------------------------------------------51Shared connection to 192.168.108.251 closed.525354[root@yunwei ansible]# 

动图

参数解释:m:指定参数,raw 官方解释如下:Another is speaking to any devices such as routers that do not have any Python installed. In any other case, using the shell or command module is much more appropriate. 该模块可以对路由器等没有安装python的设备机进行通信。a:输入命令六、使用ansible管理多个switch

还记得我们的hosts文件吗?我们在里面写入了我们需要的IP,并给他们分了组,其实我们可以直接对组进行 *** 作。

hosts文件
1[root@yunwei ansible]# cat hosts2[cisco-1]3192.168.108.2514[cisco-2]5192.168.108.2526192.168.108.2537[centos]8192.168.100.225
对组进行 *** 作@H_404_7@对组进行 *** 作,只需要在ansible 后面加上组名就好啦!
 1[root@yunwei ansible]# ansible cisco-2 -m raw -a 'show clock' -u cisco -k 2SSH password:  3192.168.108.252 | SUCCESS | rc=0 >> 4 5*05:45:41.908 UTC Wed Jun 12 2019Shared connection to 192.168.108.252 closed. 6 7 8192.168.108.253 | SUCCESS | rc=0 >> 910*05:45:42.139 UTC Wed Jun 12 2019Warning: Permanently added '192.168.108.253' (RSA) to the List of kNown hosts.11Shared connection to 192.168.108.253 closed.1213[root@yunwei ansible]# 

可以看到支队cisco-2组下面的两个Ip地址进行了 *** 作,其余的并没有执行命令。@H_404_7@

总结

以上是内存溢出为你收集整理的运维少年系列 - ansible and cisco(1)全部内容,希望文章能够帮你解决运维少年系列 - ansible and cisco(1)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1188714.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存