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

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

骚年 运维少年

ansible and cisco

一、系列文章说明
二、先决条件
三、ansible 简介
四、安装ansible
五、使用ansible管理单个switch
1)拓扑图
2)配置
3)ad hoc命令
六、使用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 k
11 libyaml                                              x86_64                                 0.1.4-11.el7_0                                    base                                     55 k
12 python-babel                                         noarch                                 0.9.6-8.el7                                       base                                    1.4 M
13 python-cffi                                          x86_64                                 1.6.0-5.el7                                       base                                    218 k
14 python-enum34                                        noarch                                 1.0.4-1.el7                                       base                                     52 k
15 python-httplib2                                      noarch                                 0.9.2-1.el7                                       extras                                  115 k
16 python-idna                                          noarch                                 2.4-1.el7                                         base                                     94 k
17 python-jinja2                                        noarch                                 2.7.2-3.el7_6                                     updates                                 518 k
18 python-paramiko                                      noarch                                 2.1.1-9.el7                                       updates                                 269 k
19 python-passlib                                       noarch                                 1.6.5-2.el7                                       extras                                  488 k
20 python-ply                                           noarch                                 3.4-11.el7                                        base                                    123 k
21 python-pycparser                                     noarch                                 2.14-1.el7                                        base                                    104 k
22 python-six                                           noarch                                 1.9.0-2.el7                                       base                                     29 k
23 python2-cryptography                                 x86_64                                 1.7.2-2.el7                                       base                                    502 k
24 python2-jmespath                                     noarch                                 0.9.0-3.el7                                       extras                                   39 k
25 python2-pyasn1                                       noarch                                 0.1.9-7.el7                                       base                                    100 k
26 sshpass                                              x86_64                                 1.06-2.el7                                        extras                                   21 k
27
28Transaction Summary
29=============================================================================================================================================================================================
30Install  1 Package (+17 Dependent packages)

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

  • 安装完毕之后,会自动生成/etc/ansible/目录

1[root@yunwei ~]# ls /etc/ansible/
2ansible.cfg  hosts  roles
3[root@yunwei ~]# 
  • ansible.cfg

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

  • hosts
    hosts文件是主机名/IP文件,这个文件指定了ansible的作用域。

五、使用ansible管理单个switch

1)拓扑图

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

2)配置

  • 修改ansible配置文件
    修改ansible配置文件,将一下行注释去掉,这一行的意思是,在ssh登录的时候,是检查known_hosts文件中是否有该IP的密钥。默认是检查的,如果不存在,那么就登录失败,这时候可以通过手动ssh一次,ssh会自动将密钥添加入known_hosts文件中,或者通过取消注释,使其不检查文件。

1host_key_checking = False
  • 清空并配置hosts文件
    只有主机存在于hosts文件中,我们才能对这个主机做操作。

1[root@yunwei ansible]# echo > hosts
2[root@yunwei ansible]# cat hosts
3[cisco]  # 组名
4192.168.108.251 # 组成员
5192.168.108.252
6192.168.108.253
7[centos]
8192.168.100.225
9[root@yunwei ansible]# 
  • 使用ansible命令查看主机是否可达(注意:我的GNS上的IOS不支持一下ping模块)

1[root@yunwei ansible]# ansible 192.168.100.225 -m ping -u root -k
2SSH 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/0
10                                                Et2/1, Et2/2, Et2/3, Et3/0
11                                                Et3/1, Et3/2, Et3/3
1210   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 
26
27VLAN Type  SAID       MTU   Parent RingNo BridgeNo Stp  BrdgMode Trans1 Trans2
28---- ----- ---------- ----- ------ ------ -------- ---- -------- ------ ------
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   
44
45Remote SPAN VLANs
46------------------------------------------------------------------------------
47
48
49Primary Secondary Type              Ports
50------- --------- ----------------- ------------------------------------------
51Shared connection to 192.168.108.251 closed.
52
53
54[root@yunwei ansible]# 

动图

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

  • 参数解释:
    • 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 hosts
2[cisco-1]
3192.168.108.251
4[cisco-2]
5192.168.108.252
6192.168.108.253
7[centos]
8192.168.100.225
  • 对组进行操作
    对组进行操作,只需要在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 >>
 9
10*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.
12
13[root@yunwei ansible]# 

可以看到支队cisco-2组下面的两个Ip地址进行了操作,其余的并没有执行命令。
运维少年系列 - ansible and cisco(1)

猜你喜欢

转载自blog.51cto.com/15082392/2656473