运维少年系列 - 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 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
    hosts文件是主机名/IP文件,这个文件指定了ansible的作用域。

五、使用ansible管理单个switch

1)拓扑图

2)配置

  • 修改ansible配置文件
    修改ansible配置文件,将一下行注释去掉,这一行的意思是,在ssh登录的时候,是检查known_hosts文件中是否有该IP的密钥。默认是检查的,如果不存在,那么就登录失败,这时候可以通过手动ssh一次,ssh会自动将密钥添加入known_hosts文件中,或者通过取消注释,使其不检查文件。
1host_key_checking = False
  • 清空并配置hosts文件
    只有主机存在于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
  • 对组进行操作
    对组进行操作,只需要在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地址进行了操作,其余的并没有执行命令。

©著作权归作者所有:来自51CTO博客作者mb5ffd6f0f39006的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 运维少年系列 python and cisco (1)
  2. 运维少年系列 - python and cisco(2)
  3. Linux轻量级监控软件-nmon
  4. 由于Linux系统文件同步时突然断电导致系统进入none模式问题详解
  5. python3 paramiko 巡检网络设备
  6. 运算符、流程控制、循环与文件包含
  7. 使用supervisor 管理logstash
  8. jenkins打包文件上传七牛云
  9. CCNP(ISCW)实验:配置Router 将AAA用于管理访问授权

随机推荐

  1. Run Android on Your Netbook or Desktop
  2. Android(安卓)断点续传的原理剖析与实例
  3. android系统自带的主题与样式(theme and
  4. Android 之EditText InputType说明
  5. android Application Component研究之Ser
  6. Android用户界面UI组件--AdapterView及其
  7. Android 存储选项之 ContentProvider 启
  8. [置顶] Android(安卓)实现 WheelView
  9. Android(安卓)避免主线程执行网络请求之A
  10. [置顶] Android各种Layout用到的一些重要