这一次的实验内容依然来自Kirk Byers的博客,源地址在https://pynet.twb-tech.com/blog/python/paramiko-ssh-part1.html

但是,这次实验只是在安装了ansible的服务器的本地做的,做的内容就是 利用Jinja2和YAML生成完整的网络配置,生成完过后,网络配置是在服务器本地的,这篇博客里不会讨论如何把生成的配置自动导入到远端的网络设备上。(其实目前我也不会,走一步看一步)

上一篇博客里已经说到了安装并测试了一个ping的基本功能,这个ping虽然ping的是远端的真机但是还是比较简单的,今天要写下来的全是本地的,但是要比上次的难一些。

简单介绍下这两种类型的文件

对Jinja2和YAML有了解的同志们应该会看着更舒畅一些,如果对Jinjin2和YAML没有了解可以戳下面的连接。

Jinja2: 简单说就是用来写模板的,在这里我们用来写网络配置的模板,因为网络设备之间很多的配置都是相似的嘛,所以要引入一个模板。搞过Django的应该对这个也不陌生,Jinja2文件后缀是 .j2

http://jinja.pocoo.org/docs/dev/  这个是它的官方文档

YAML: 简单来说有三个用途,一是用来存储变量(后面会提到的vars文件夹),二是用来写任务(后面会提到的tasks文件夹),三是用来写playbook的(被ansible-playbook 执行)。它是一种标记语言,后缀是 .yml。全称是Yet Another Markup Language。

https://en.wikipedia.org/wiki/YAML 维基百科来一发

在应用中的位置

为了更好地编辑这两种文件,我干脆把他们都放到pycharm里面去了,这样用来对比文件,或者查找替换都比较方便,而且对于Linux命令行不是很好的我来说比较方便。我开启了tiger-VNC的远程桌面,所以下面的截图里能很好的体现这两种文件的位置关系

图1

结合上一篇里那个图就能看着更清晰了。绿色框里那个文件是 site.yml 也就是一个playbook。三个红框里的是三个文件夹,也就代表了三个roles。蓝色框里是最终生成的配置。

执行过程

命令是在/home/ansibleserver/ANSIBLE/RTR-TEMPLATE这个目录下,执行命令 ansible-play site.yml

下面分析一下它执行的过程:

1. 调用site.yml,我们看看site.yml里写了什么,如下:

  1. ---
  2. - name: Generate configuration files for devices
  3. hosts: localhost
  4.  
  5. roles:
  6. - routers
  7. - access-switches

可以看出YAML的格式特点,三个短杠“-”开头,另起一行之后,“-”空格name: 开始一个play,其实一个playbook里是可以直接嵌套task的(就如同Jason Edelman的一个博客http://jedelman.com/home/network-test-automation-with-ansible/),但是我们这里暂时不这么做,因为那样的层级关系就不那么清晰了。所以我们这里的playbook写的很简单,name就是一个描述;hosts标明执行的类型是本地(localhost),区别于其他命名的用于远端的类型;roles这里比较有趣了,这里下面写的routers和access-switches决定了要遍历的文件夹,请看图1里那三个红框,这里roles写了routers和access-switches,所以它即将遍历routers和access-switches这两个文件夹。

2. hosts文件

hosts文件在我们接下来的实验里不会有什么实际的用处,我理解的它的用处就是像上面所说的标明这个play执行的类型,或者说是执行类型,是本地还是远程。hosts文件的内容如下:

  1. [root@localhost ansible]# more hosts
  2. [switch]
  3. cn-pek1-10-a01-ex-vc1.cnet.com ansible_connection=local ansible_ssh_host=cn-pek1-10-a01-ex-vc1.cnet.com
  4.  
  5. [local]
  6. localhost ansible_ssh_user=ansibleserver ansible_connection=local

上面那个[switch]就是上次用到的,上次写的命令是all,而且我又注释掉了local,所以把它包含了,这次我们只用[local],所以没有[switch]啥事儿, 下面的实验也不用管它。

3. roles文件夹内容

我们知道了这个play它要去找哪两个文件夹之后,就该看看这俩文件夹里有什么了:

(1)单一型号

以access-switches这个文件夹为例,这个比较简单易懂,因为就只有一台设备,即使有多台设备也无所谓,因为型号一样,就没什么变换。

第一个main.yml是task,标明要做什么;第二个main.yml是变量,里面装的全是变量;access-switch.j2是模板,模板嘛,就是配置中总是一成不变的那部分。

task下的main.yml如下:

  1. ---
  2. - name: Generate access-switch configurations files
  3. template: src=access-switch.j2 dest=/home/ansibleserver/ANSIBLE/CFGS/{{item.hostname}}.txt
  4. with_items: switches_2960

第一行的name还是仅仅是一个描述。第二行就决定了它要做啥了,标明了这是一个通过模板生成文件的action,模板是access-switch.j2,生成之后的目的文件是{{item.hostname}}.txt,{{item.hostname}}这种写法很有趣。现在还不知道item是个啥,所以要看第三行with_items: switches_2960,标明item是switches_2960, 这个名字是 switches_2960去哪里找呢,去vars里面去找。

下面是部分模板的代码:

  1. 部分的模板.j2
    {% for interface in cisco_2960_interfaces%} //这相当于是个for循环,根据vars里定义的interface的不同,这里会产生N个配置相同的interface
  2. interface {{interface}}
  3. switchport access vlan 893
  4. switchport mode access
  5. switchport port-security maximum 20
  6. ip arp inspection limit rate 500
  7. storm-control broadcast level bps 5m 3m
  8. storm-control action shutdown
  9. storm-control action trap
  10. spanning-tree portfast
  11. spanning-tree bpduguard enable
  12. ip dhcp snooping limit rate 100
  13. {% endfor%}

以上是一个很典型的适用于Jinja2的for循环。for interface in cisco_2960_interfaces是for循环的开头,cisco_2960_interfaces是要去被遍历的目的地,这个目的地就是变量。这个时候就要去打开vars文件夹下的main.yml来看一下,我目前的感觉是,它会自动的去找vars文件夹,然后自动的去找存了变量的那一行,找到“cisco_2960_interfaces”就开始利用。

  1. 部分的vars下的main.yml
    cisco_2960_interfaces:
  2. - FastEthernet0/1
  3. - FastEthernet0/2
  4. - FastEthernet0/3
  5. - FastEthernet0/4
  6.  
  7. switches_2960:
  8. - { hostname: cn-pek1-15-2960-1 }

当然可以利用的不止for循环,针对hostname这个变量,可以用下面这种方法

  1. 部分的模板.j2
    service timestamps debug datetime msec
    service timestamps log datetime msec
    service password-encryption
    !
    hostname {{item.hostname} //要变的就是这一行
    !
    boot-start-marker
    boot-end-marker

当它看到item.hostname它会自己去找vars里的switches_2960这个值,就如同上面的{{item.hostname}}一样

  1. switches_2960:
  2. - { hostname: cn-pek1-15-2960-1 }

这里就一个hostname,它既会被调用到item.hostname这里,完成对成品配置文件的命名,也会被调用到hostname {{item.hostname}}这里完成配置文件的编写。

下面是生成的成品配置的一部分内容:

  1. service timestamps debug datetime msec
  2. service timestamps log datetime msec
  3. service password-encryption
  4. !
  5. hostname cn-pek1-15-2960-1 //对照上面那段,得知这里是从{{item.hostname}}得来的
  6. !
  7. boot-start-marker
  8. boot-end-marker
  9.  
  10. << snip >>
  11.  
  12. interface FastEthernet0/1 //vars里就定义了四个interface,当然实际的交换机interface哪有那么少,我是懒得写了,我觉得那些就可以通过SNMP来抓了,然后放到vars里
     switchport access vlan 893
     switchport mode access
     switchport port-security maximum 20
     ip arp inspection limit rate 500
     storm-control broadcast level bps 5m 3m
     storm-control action shutdown
     storm-control action trap
     spanning-tree portfast
     spanning-tree bpduguard enable
     ip dhcp snooping limit rate 100
    interface FastEthernet0/2
     switchport access vlan 893
     switchport mode access
     switchport port-security maximum 20
     ip arp inspection limit rate 500
     storm-control broadcast level bps 5m 3m
     storm-control action shutdown
     storm-control action trap
     spanning-tree portfast
     spanning-tree bpduguard enable
     ip dhcp snooping limit rate 100
    interface FastEthernet0/3
     switchport access vlan 893
     switchport mode access
     switchport port-security maximum 20
     ip arp inspection limit rate 500
     storm-control broadcast level bps 5m 3m
     storm-control action shutdown
     storm-control action trap
     spanning-tree portfast
     spanning-tree bpduguard enable
     ip dhcp snooping limit rate 100
    interface FastEthernet0/4
     switchport access vlan 893
     switchport mode access
     switchport port-security maximum 20
     ip arp inspection limit rate 500
     storm-control broadcast level bps 5m 3m
     storm-control action shutdown
     storm-control action trap
     spanning-tree portfast
     spanning-tree bpduguard enable
     ip dhcp snooping limit rate 100

(2)多型号

先看tasks文件夹下的main.yml

  1. ---
  2. #- name: Generate configuration files //#是用来注释掉jinja2的符号
  3. # template: src=router.j2 dest=/home/ansibleserver/ANSIBLE/CFGS/{{item.hostname}}.txt
  4. # with_items: test_routers
  5. - name: Generate configurations files
  6. template: src=router-881.j2 dest=/home/ansibleserver/ANSIBLE/CFGS/{{item.hostname}}.txt
  7. with_items: routers_881
  8.  
  9. - name: Generate configurations files
  10. template: src=router-1921.j2 dest=/home/ansibleserver/ANSIBLE/CFGS/{{item.hostname}}.txt
  11. with_items: routers_1921

着重需要看的是template这一行,src变成了 router-881.j2和router-1921.j2,with_items分别变成了routers_881和routers_1921,但是我这里并不打算写两个完整的模板,因为他们的型号虽然不同,但是其实配置的内容大同小异,所以就有了下面这个问题:

设想一下,不同型号的设备之间的配置,可能大同小异,那么就可以抽离其中相同的部分作为一个base模板,然后两种型号不同的地方,分别写两个各自的模板,来解决类似“代码复用”的问题。这就有了下面这种构建模板的形式,请看routers这个role下的三个文件夹中的templates文件夹

base.j2就是两个型号的路由器相同的配置了,base.j2如下,这里里面有很多的变量添加进来,还用到了if判断,所以就不放部分代码了,下面是base.j2的全部代码

  1. no service pad
  2. service tcp-keepalives-in
  3. service tcp-keepalives-out
  4. service timestamps debug datetime msec localtime show-timezone
  5. service timestamps log datetime msec localtime show-timezone
  6. service password-encryption
  7. !
  8. hostname {{item.hostname}}
  9. !
  10. boot-start-marker
  11. boot-end-marker
  12. !
  13. logging buffered 32000
  14. no logging console
  15. enable secret 0 {{item.secret}} //这些地方就类似于之前的{{item.hostname}}了
  16. !
  17. aaa new-model
  18. !
  19. !
  20. aaa authentication login default local-case
  21. aaa authorization exec default local
  22. !
  23. !
  24. !
  25. !
  26. !
  27. aaa session-id common
  28. !
  29. !
  30. !
  31. memory-size iomem 10
  32. clock timezone {{item.timezone}} {{item.timezone_offset}}
  33. clock summer-time {{item.timezone_dst}} recurring
  34. !
  35. no ip source-route
  36. ip options drop
  37. !
  38. !
  39. !
  40. !
  41. ip dhcp bootp ignore
  42. {% if item.DHCP %} //嗯。。if循环,这个会先去检查vars里这个值是不是真的,一会儿贴上vars
  43. no ip dhcp conflict logging
  44. ip dhcp excluded-address {{item.dhcp_exclude1_start}} {{item.dhcp_exclude1_end}}
  45. !
  46. ip dhcp pool POOL1
  47. network {{item.dhcp_network}} {{item.dhcp_netmask}}
  48. default-router {{item.dhcp_gateway}}
  49. dns-server 8.8.8.8 8.8.4.4
  50. {% endif %}
  51. !
  52. !
  53. ip cef
  54. no ip domain lookup
  55. ip domain name whatever.com
  56. {% if item.CBAC %} //同样是if循环
  57. ip inspect name INTERNET cuseeme
  58. ip inspect name INTERNET dns
  59. ip inspect name INTERNET ftp
  60. ip inspect name INTERNET h323
  61. ip inspect name INTERNET icmp
  62. ip inspect name INTERNET imap
  63. ip inspect name INTERNET pop3
  64. ip inspect name INTERNET netshow
  65. ip inspect name INTERNET rcmd
  66. ip inspect name INTERNET realaudio
  67. ip inspect name INTERNET rtsp
  68. ip inspect name INTERNET sqlnet
  69. ip inspect name INTERNET streamworks
  70. ip inspect name INTERNET tftp
  71. ip inspect name INTERNET vdolive
  72. ip inspect name INTERNET pptp
  73. ip inspect name INTERNET tcp router-traffic
  74. ip inspect name INTERNET udp router-traffic
  75. {% endif %}
  76. no ipv6 cef
  77. !
  78. !
  79. !
  80. !
  81. username admin privilege 15 secret 0 {{item.secret}}
  82. !
  83. !
  84. ip ssh version 2
  85. !
  86. !
  87. !
  88. !
  89. !
  90. !
  91. !
  92. !
  93. !
  94. !
  95. !
  96. !
  97. {% block interfaces %} //现在来到了第一个block,遇到block的地方就意味着这个地方两种型号的路由器就不一样了。我喜欢把这种地方称为“对节点”
  98. {% endblock %}
  99. !
  100. no ip http server
  101. no ip http secure-server
  102. !
  103. !
  104. {% block nat %} //第二个block,意味着两种型号的路由器在NAT这块儿也不一样
  105. {% endblock %}
  106.  
  107. ip route 0.0.0.0 0.0.0.0 {{item.public_gateway}}
  108. !
  109. ip access-list extended INTERNET
  110. permit icmp any any host {{item.public_ip}}
  111. ip access-list extended NAT
  112. permit ip {{item.internal_network}} 0.0.0.255 any
  113. !
  114. line vty 0 4
  115. exec-timeout 20 0
  116. logging synchronous
  117. transport input ssh
  118. transport output ssh
  119. !
  120. ntp update-calendar
  121. ntp server 1.1.1.1
  122. ntp server 2.2.2.2
  123. end

下面是router-881.j2的全部内容,这些内容也就是router-881独有的,凡是它不独有的(重复的配置),它都去call base.j2去找。

  1. !
  2. !
  3. {% extends "base.j2"%}//意味着这个Jinja2文件是要拓展base.j2这个文件的,当然实际上在router-881.j2被task调用的时候,它传递给task的内容实际是router-881的这点儿内容加上base.j2
  4. {% block interfaces %} //从这一行到结尾,都是interface这个block
  5.  
  6. {% for interface in cisco_881_l2_interfaces %}
  7. interface {{interface}}
  8. switchport access vlan 10
  9. spanning-tree portfast
  10. !
  11. !
  12. {% endfor %}
  13. interface FastEthernet4
  14. ip address {{item.public_ip}} {{item.public_netmask}}
  15. ip access-group INTERNET in
  16. no ip redirects
  17. no ip proxy-arp
  18. ip nat outside
  19. {% if item.CBAC %} ip inspect INTERNET out
  20. {% endif %}
  21. ip virtual-reassembly
  22. duplex auto
  23. speed auto
  24. no cdp enable
  25. !
  26. !
  27. interface Vlan1
  28. no ip address
  29. !
  30. !
  31. interface Vlan10
  32. description Internal LAN
  33. ip address {{item.internal_ip}} 255.255.255.0
  34. ip nat inside
  35. ip virtual-reassembly
  36. !
  37. {% endblock%}
  38. !
  39. !
  40. {% block nat %}
  41. ip nat inside source list NAT interface FastEthernet4 overload
  42. {% endblock%}
  43. !

下面是router-1921.j2的内容

  1. !
  2. !
  3. {% extends "base.j2"%}
  4.  
  5. {% block interfaces %} //同样是block,但是1921有两个block,下面还有个nat的block
  6.  
  7. {% for interface in cisco_881_l2_interfaces %}
  8. interface {{interface}}
  9. switchport access vlan 10
  10. spanning-tree portfast
  11. {% endfor %}
  12.  
  13. interface GigabitEthernet0/0
  14. ip address {{item.public_ip}} {{item.public_netmask}}
  15. ip access-group INTERNET in
  16. no ip redirects
  17. no ip proxy-arp
  18. ip nat outside
  19. {% if item.CBAC %} ip inspect INTERNET out
  20. {% endif %}
  21. ip virtual-reassembly
  22. duplex auto
  23. speed auto
  24. no cdp enable
  25. !
  26. !
  27. interface GigabitEthernet0/1
  28. description Internal LAN
  29. ip address {{item.internal_ip}} 255.255.255.0
  30. no ip redirects
  31. no ip proxy-arp
  32. ip nat inside
  33. ip virtual-reassembly
  34. duplex auto
  35. speed auto
  36. {% endblock%}
  37. !
  38. !
  39. {% block nat %}
  40. ip nat inside source list NAT interface GigabitEthernet0/0 overload
  41. {% endblock%}
  42. !

不同的block被调用到他们该被调用的地方,也就是base.j2的“对节点”这里如下:

对接点1

  1. {% block interfaces %} //现在来到了第一个block,遇到block的地方就意味着这个地方两种型号的路由器就不一样了。我喜欢把这种地方称为“对节点”
  2. {% endblock %}

对接点2

  1. {% block nat %} //第二个block,意味着两种型号的路由器在NAT这块儿也不一样
  2. {% endblock %}

下面是vars文件夹下的main.yml

  1. ---
  2. #test_routers:
  3. # - { hostname: twb-sf-rtr1, secret: apassword, timezone: PST, timezone_dst: PDT, timezone_offset: -8, DHCP: true, dhcp_exclude1_start: 10.1.1.1, dhcp_exclude1_end: 10.1.1.99, dhcp_network: 10.1.1.0, dhcp_netmask: 255.255.255.0, dhcp_gateway: 10.1.1.1, CBAC: true, public_ip: 6.6.6.6, public_netmask: 255.255.255.0, public_gateway: 6.6.6.1, vlan10_ip: 10.1.1.1, vlan10_network: 10.1.1.0 }
  4. # - { hostname: twb-sf-rtr2, secret: apassword, timezone: PST, timezone_dst: PDT, timezone_offset: -8, DHCP: true, dhcp_exclude1_start: 10.1.1.1, dhcp_exclude1_end: 10.1.1.99, dhcp_network: 10.1.1.0, dhcp_netmask: 255.255.255.0, dhcp_gateway: 10.1.1.1, CBAC: true, public_ip: 6.6.6.6, public_netmask: 255.255.255.0, public_gateway: 6.6.6.1, vlan10_ip: 10.1.1.1, vlan10_network: 10.1.1.0 }
  5. # - { hostname: twb-la-rtr1, secret: apassword, timezone: PST, timezone_dst: PDT, timezone_offset: -8, DHCP: false, dhcp_exclude1_start: 10.1.1.1, dhcp_exclude1_end: 10.1.1.99, dhcp_network: 10.1.1.0, dhcp_netmask: 255.255.255.0, dhcp_gateway: 10.1.1.1, CBAC: false, public_ip: 6.6.6.6, public_netmask: 255.255.255.0, public_gateway: 6.6.6.1, vlan10_ip: 10.1.1.1, vlan10_network: 10.1.1.0 }
  6. # - { hostname: twb-la-rtr2, secret: apassword, timezone: PST, timezone_dst: PDT, timezone_offset: -8, DHCP: false, dhcp_exclude1_start: 10.1.1.1, dhcp_exclude1_end: 10.1.1.99, dhcp_network: 10.1.1.0, dhcp_netmask: 255.255.255.0, dhcp_gateway: 10.1.1.1, CBAC: false, public_ip: 6.6.6.6, public_netmask: 255.255.255.0, public_gateway: 6.6.6.1, vlan10_ip: 10.1.1.1, vlan10_network: 10.1.1.0 }
  7. # - { hostname: twb-den-rtr1, secret: apassword, timezone: MST, timezone_dst: MDT, timezone_offset: -7, DHCP: true, dhcp_exclude1_start: 10.1.1.1, dhcp_exclude1_end: 10.1.1.99, dhcp_network: 10.1.1.0, dhcp_netmask: 255.255.255.0, dhcp_gateway: 10.1.1.1, CBAC: true, public_ip: 6.6.6.6, public_netmask: 255.255.255.0, public_gateway: 6.6.6.1, vlan10_ip: 10.1.1.1, vlan10_network: 10.1.1.0 }
  8. #
  9. #cisco_881_l2_interfaces:
  10. # - FastEthernet0
  11. # - FastEthernet1
  12. # - FastEthernet2
  13. # - FastEthernet3
  14. #
  15.  
  16. routers_881:
  17. - { hostname: twb-sf-rtr1, secret: apassword, timezone: PST, timezone_dst: PDT, timezone_offset: -8, DHCP: true, dhcp_exclude1_start: 10.1.1.1, dhcp_exclude1_end: 10.1.1.99, dhcp_network: 10.1.1.0, dhcp_netmask: 255.255.255.0, dhcp_gateway: 10.1.1.1, CBAC: true, public_ip: 6.6.6.6, public_netmask: 255.255.255.0, public_gateway: 6.6.6.1, internal_ip: 10.1.1.1, internal_network: 10.1.1.0 }
  18.  
  19. routers_1921:
  20. - { hostname: twb-sf-rtr2, secret: apassword, timezone: PST, timezone_dst: PDT, timezone_offset: -8, DHCP: true, dhcp_exclude1_start: 10.1.1.1, dhcp_exclude1_end: 10.1.1.99, dhcp_network: 10.1.1.0, dhcp_netmask: 255.255.255.0, dhcp_gateway: 10.1.1.1, CBAC: true, public_ip: 6.6.6.6, public_netmask: 255.255.255.0, public_gateway: 6.6.6.1, internal_ip: 10.1.1.1, internal_network: 10.1.1.0 }

这次的vars比较丰富。

我们执行的命令依然是 ansible-playbook site.yml

下面是生成的成品配置

第一个配置是twb-sf-rtr1.txt也就是881的

  1. !
  2. !
  3. no service pad
  4. service tcp-keepalives-in
  5. service tcp-keepalives-out
  6. service timestamps debug datetime msec localtime show-timezone
  7. service timestamps log datetime msec localtime show-timezone
  8. service password-encryption
  9. !
  10. hostname twb-sf-rtr1
  11. !
  12. boot-start-marker
  13. boot-end-marker
  14. !
  15. logging buffered 32000
  16. no logging console
  17. enable secret 0 apassword
  18. !
  19. aaa new-model
  20. !
  21. !
  22. aaa authentication login default local-case
  23. aaa authorization exec default local
  24. !
  25. !
  26. !
  27. !
  28. !
  29. aaa session-id common
  30. !
  31. !
  32. !
  33. memory-size iomem 10
  34. clock timezone PST -8
  35. clock summer-time PDT recurring
  36. !
  37. no ip source-route
  38. ip options drop
  39. !
  40. !
  41. !
  42. !
  43. ip dhcp bootp ignore
  44. no ip dhcp conflict logging
  45. ip dhcp excluded-address 10.1.1.1 10.1.1.99
  46. !
  47. ip dhcp pool POOL1
  48. network 10.1.1.0 255.255.255.0
  49. default-router 10.1.1.1
  50. dns-server 8.8.8.8 8.8.4.4
  51. !
  52. !
  53. ip cef
  54. no ip domain lookup
  55. ip domain name whatever.com
  56. ip inspect name INTERNET cuseeme
  57. ip inspect name INTERNET dns
  58. ip inspect name INTERNET ftp
  59. ip inspect name INTERNET h323
  60. ip inspect name INTERNET icmp
  61. ip inspect name INTERNET imap
  62. ip inspect name INTERNET pop3
  63. ip inspect name INTERNET netshow
  64. ip inspect name INTERNET rcmd
  65. ip inspect name INTERNET realaudio
  66. ip inspect name INTERNET rtsp
  67. ip inspect name INTERNET sqlnet
  68. ip inspect name INTERNET streamworks
  69. ip inspect name INTERNET tftp
  70. ip inspect name INTERNET vdolive
  71. ip inspect name INTERNET pptp
  72. ip inspect name INTERNET tcp router-traffic
  73. ip inspect name INTERNET udp router-traffic
  74. no ipv6 cef
  75. !
  76. !
  77. !
  78. !
  79. username admin privilege 15 secret 0 apassword
  80. !
  81. !
  82. ip ssh version 2
  83. !
  84. !
  85. !
  86. !
  87. !
  88. !
  89. !
  90. !
  91. !
  92. !
  93. !
  94. !
  95.  
  96. interface FastEthernet0
  97. switchport access vlan 10
  98. spanning-tree portfast
  99. !
  100. !
  101. interface FastEthernet1
  102. switchport access vlan 10
  103. spanning-tree portfast
  104. !
  105. !
  106. interface FastEthernet2
  107. switchport access vlan 10
  108. spanning-tree portfast
  109. !
  110. !
  111. interface FastEthernet3
  112. switchport access vlan 10
  113. spanning-tree portfast
  114. !
  115. !
  116. interface FastEthernet4
  117. ip address 6.6.6.6 255.255.255.0
  118. ip access-group INTERNET in
  119. no ip redirects
  120. no ip proxy-arp
  121. ip nat outside
  122. ip inspect INTERNET out
  123. ip virtual-reassembly
  124. duplex auto
  125. speed auto
  126. no cdp enable
  127. !
  128. !
  129. interface Vlan1
  130. no ip address
  131. !
  132. !
  133. interface Vlan10
  134. description Internal LAN
  135. ip address 10.1.1.1 255.255.255.0
  136. ip nat inside
  137. ip virtual-reassembly
  138. !
  139. !
  140. no ip http server
  141. no ip http secure-server
  142. !
  143. !
  144. ip nat inside source list NAT interface FastEthernet4 overload //可以看到这里和上面的interface那里是881独有的
  145.  
  146. ip route 0.0.0.0 0.0.0.0 6.6.6.1
  147. !
  148. ip access-list extended INTERNET
  149. permit icmp any any host 6.6.6.6
  150. ip access-list extended NAT
  151. permit ip 10.1.1.0 0.0.0.255 any
  152. !
  153. line vty 0 4
  154. exec-timeout 20 0
  155. logging synchronous
  156. transport input ssh
  157. transport output ssh
  158. !
  159. ntp update-calendar
  160. ntp server 1.1.1.1
  161. ntp server 2.2.2.2
  162. end

第二个配置是twb-sf-rtr2.txt也就是1921的

  1. !
  2. !
  3. no service pad
  4. service tcp-keepalives-in
  5. service tcp-keepalives-out
  6. service timestamps debug datetime msec localtime show-timezone
  7. service timestamps log datetime msec localtime show-timezone
  8. service password-encryption
  9. !
  10. hostname twb-sf-rtr2
  11. !
  12. boot-start-marker
  13. boot-end-marker
  14. !
  15. logging buffered 32000
  16. no logging console
  17. enable secret 0 apassword
  18. !
  19. aaa new-model
  20. !
  21. !
  22. aaa authentication login default local-case
  23. aaa authorization exec default local
  24. !
  25. !
  26. !
  27. !
  28. !
  29. aaa session-id common
  30. !
  31. !
  32. !
  33. memory-size iomem 10
  34. clock timezone PST -8
  35. clock summer-time PDT recurring
  36. !
  37. no ip source-route
  38. ip options drop
  39. !
  40. !
  41. !
  42. !
  43. ip dhcp bootp ignore
  44. no ip dhcp conflict logging
  45. ip dhcp excluded-address 10.1.1.1 10.1.1.99
  46. !
  47. ip dhcp pool POOL1
  48. network 10.1.1.0 255.255.255.0
  49. default-router 10.1.1.1
  50. dns-server 8.8.8.8 8.8.4.4
  51. !
  52. !
  53. ip cef
  54. no ip domain lookup
  55. ip domain name whatever.com
  56. ip inspect name INTERNET cuseeme
  57. ip inspect name INTERNET dns
  58. ip inspect name INTERNET ftp
  59. ip inspect name INTERNET h323
  60. ip inspect name INTERNET icmp
  61. ip inspect name INTERNET imap
  62. ip inspect name INTERNET pop3
  63. ip inspect name INTERNET netshow
  64. ip inspect name INTERNET rcmd
  65. ip inspect name INTERNET realaudio
  66. ip inspect name INTERNET rtsp
  67. ip inspect name INTERNET sqlnet
  68. ip inspect name INTERNET streamworks
  69. ip inspect name INTERNET tftp
  70. ip inspect name INTERNET vdolive
  71. ip inspect name INTERNET pptp
  72. ip inspect name INTERNET tcp router-traffic
  73. ip inspect name INTERNET udp router-traffic
  74. no ipv6 cef
  75. !
  76. !
  77. !
  78. !
  79. username admin privilege 15 secret 0 apassword
  80. !
  81. !
  82. ip ssh version 2
  83. !
  84. !
  85. !
  86. !
  87. !
  88. !
  89. !
  90. !
  91. !
  92. !
  93. !
  94. !
  95.  
  96. interface FastEthernet0 //
  97. switchport access vlan 10
  98. spanning-tree portfast
  99. interface FastEthernet1
  100. switchport access vlan 10
  101. spanning-tree portfast
  102. interface FastEthernet2
  103. switchport access vlan 10
  104. spanning-tree portfast
  105. interface FastEthernet3
  106. switchport access vlan 10
  107. spanning-tree portfast
  108.  
  109. interface GigabitEthernet0/0
  110. ip address 6.6.6.6 255.255.255.0
  111. ip access-group INTERNET in
  112. no ip redirects
  113. no ip proxy-arp
  114. ip nat outside
  115. ip inspect INTERNET out
  116. ip virtual-reassembly
  117. duplex auto
  118. speed auto
  119. no cdp enable
  120. !
  121. !
  122. interface GigabitEthernet0/1
  123. description Internal LAN
  124. ip address 10.1.1.1 255.255.255.0
  125. no ip redirects
  126. no ip proxy-arp
  127. ip nat inside
  128. ip virtual-reassembly
  129. duplex auto
  130. speed auto
  131. !
  132. no ip http server
  133. no ip http secure-server
  134. !
  135. !
  136. ip nat inside source list NAT interface GigabitEthernet0/0 overload //这个NAT这里就看出了1921独有的配置
  137.  
  138. ip route 0.0.0.0 0.0.0.0 6.6.6.1
  139. !
  140. ip access-list extended INTERNET
  141. permit icmp any any host 6.6.6.6
  142. ip access-list extended NAT
  143. permit ip 10.1.1.0 0.0.0.255 any
  144. !
  145. line vty 0 4
  146. exec-timeout 20 0
  147. logging synchronous
  148. transport input ssh
  149. transport output ssh
  150. !
  151. ntp update-calendar
  152. ntp server 1.1.1.1
  153. ntp server 2.2.2.2
  154. end

这两个生成出来的配置文件存放在 CFGS这个文件夹里。这样就通过YAML和Jinja2生成了配置文件。

目前还需要解决很多问题:

第一个大问题就是如何把成品的配置输入到远端的设备上去,这就产生了两个思路,一个是expect-like模式(有现成的library),一个是API模式(各个厂商不一样,onePK?PyEZ?个个系统也不一样,IOS和NXOS也不一样)。

第二个问题就是如何抓取host。

第三个问题是和Python脚本的配合,这个和第二个问题也是密切相关的。

to be continued...

 未经授权,请勿转载!
 
 

Ansible用于网络设备管理 part 1 Jinja2 YAML初窥的更多相关文章

  1. Ansible用于网络设备管理 part 2 对Jinja2 YAML 和 module的理解

    虽然很不想用“应该”这个词,但是还是写上了,的确我自己目前就是这么理解的. 那么这个理解就是,Ansible的一个key point 就是总的一个playbook是去依赖很多元素的,就像一开始那个图里 ...

  2. Ansible用于网络设备管理 part 4 使用NAPALM成品库+Gabriele的方法+循环

    上次用NAPALM实现了一个给端口加description的小功能,但是那次仅仅是用于给一个设备的一个端口加description,没有实现一次给多个设备加,也没有实现一个给多个端口加(虽然这次也没实 ...

  3. Ansible用于网络设备管理 part 3 使用NAPALM成品库

    闲话 经过了这俩月的闲暇时间的瞎逛和瞎琢磨,我发现NAPALM是一条路,NAPALM是由帅哥David Barroso和美女Elisa Jasinska创建的一个项目,都是颜值高的技术牛人啊,真是不给 ...

  4. Ansible用于网络设备管理 part 0 安装和登录网络设备

    真实在是累了,但是又怕第二天早上又忘掉,在这先大概写写. 安装: http://www.tecmint.com/install-and-configure-ansible-automation-too ...

  5. Ansible 小手册系列 十三(Jinja2)

    用于playbook中的jinja 2过滤器 更改数据格式,其结果是字符串 {{ some_variable | to_json }} {{ some_variable | to_yaml }} 对于 ...

  6. rh358 002 fact变量获取 ansible配置网络 service_facts

    通过ansible 获取网络信息 1.如何获取fact事实变量 方式1: ansible servera -m servera 方式2: 剧本 [root@workstation ansible]# ...

  7. 此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求

    FirFox打开80以外的端口,会弹出以下提示: “此地址使用了一个通常用于网络浏览以外的端口.出于安全原因,Firefox 取消了该请求.”. 解决方法如下: 在Firefox地址栏输入about: ...

  8. 火狐----此地址使用了一个通常用于网络浏览以外的端口。出于安全原因,Firefox 取消了该请求。

    FirFox打开80以外的端口,会弹出以下提示: “此地址使用了一个通常用于网络浏览以外的端口.出于安全原因,Firefox 取消了该请求.”.经网上搜索,解决方法如下: 在Firefox地址栏输入a ...

  9. Linux-TFTP之用于网络远程安装

    TFTP:Trival File Transfer Protocol,简单文件传输协议.是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂.开销不大的文件传输服务. ...

随机推荐

  1. android 编译 release 签名

    1.编译 ionic build --release android 2.签名文件 keytool -genkey -alias kwwy -keyalg RSA -validity 40000 -k ...

  2. ffmpeg中的sws_scale算法性能测试

    经常用到ffmpeg中的sws_scale来进行图像缩放和格式转换,该函数可以使用各种不同算法来对图像进行处理.以前一直很懒,懒得测试和甄 别应该使用哪种算法,最近的工作时间,很多时候需要等待别人.忙 ...

  3. 细数Qt开发的各种坑(欢迎围观)

    1:Qt的版本多到你数都数不清,多到你开始怀疑人生.从4.6开始到5.8,从MSVC编译器到MINGW编译器,从32位到64位,从Windows到Linux到MAC.MSVC版本还必须安装对应的VS2 ...

  4. 使用sp_addextendedproperty添加描述信息

    -- For table EXECUTE sp_addextendedproperty N'MS_Description', '描述内容', N'user', N'dbo', N'table', N' ...

  5. Qt label加边框

    myLabel->setStyleSheet("border:1px solid black"); 简要说明:border就是边缘.px是像素,1px就是边框的宽度.soli ...

  6. Android代码优化——使用Android lint工具

    作为移动应用开发者,我们总希望发布的apk文件越小越好,不希望资源文件没有用到的图片资源也被打包进apk,不希望应用中使用了高于minSdk的api,也不希望AndroidManifest文件存在异常 ...

  7. 【转】web集群时session同步的3种方法

    转载请注明作者:海底苍鹰地址:http://blog.51yip.com/server/922.html 在做了web集群后,你肯定会首先考虑session同步问题,因为通过负载均衡后,同一个IP访问 ...

  8. MyBatis XML 映射配置文件

    配置文件的基本结构 configuration —— 根元素 properties —— 定义配置外在化 settings —— 一些全局性的配置 typeAliases —— 为一些类定义别名 ty ...

  9. Enjoy Android

    大趋势所迫,开始学习Android, @mark一下

  10. 使用ELK(Elasticsearch + Logstash + Kibana) 搭建日志集中分析平台实践--转载

    原文地址:https://wsgzao.github.io/post/elk/ 另外可以参考:https://www.digitalocean.com/community/tutorials/how- ...