ansible and cisco
一、ansible playbook
- playbook
playbook 翻译过来就是剧本,什么是剧本?就是编剧写好了所有的情景话语,演员去执行、表达出来。在ansible中,我们就是编剧,而路由器交换机等设备就是演员。所以这个剧本需要我们去写,然后由设备去执行。(讲得不太专业,大家可以自行百度ansible playbook),playbook要以yaml格式编写。
- playbook可以做什么
之前的我们的文章演示了单条命令执行,playbook相对于之前的模式更加灵活,我们可以在playbook中加入循环、判断、调用多个模块、执行多个命令等等…所以playbook是(装13)必不可少的。
二、playbook的使用
- YAML
YAML跟JSON和XML的区别不大,都有特定的格式。大家可以去看一下YAML的简单用法。一定要注意空格,缩进只能使用空格缩进。
- 模块
需要查看网络模块可以点击这里查看。我们今天主要使用的是ios_command和ios_config模块,如下图:
点击模块名,可以看到里面的参数和使用的例子,如下图:
- ios_command
ios_command 模块,可以在IOS的路由器交换机上面使用,该模块只能在(# )特权模式下使用,而不能在(config# )全局配置模式下使用。
- ios_config
ios_config 模块,同上,不过只能在(config# )全局配置模式下使用,而不能在(# )特权模式下使用
1) 第一个playbook的使用
第一个playbook比较简单,以下注释我以编剧的身份来解释:
[root@yunwei ~]# cat ywsn_playbook1.yaml
--- # yaml以---开头 # 一定要注意以下参数的空格,-和: 后面一定要有空格!!!
- name: show clock # 剧本的名字
hosts: cisco-2 # 表演的演员 -- 执行的主机(在hosts文件中)
gather_facts: false # 检查演员的基本信息 -- 检查主机配置等信息
connection: local # 剧本在谁的手里
tasks: # 剧情
- name: show clock # 剧情的名字(第1集)
ios_command: # 使用ios_command模块
commands: # 使用ios_command模块下的commands参数(即命令执行)
- show clock # 执行的命令
register: print_output # 保存在一个文件中(执行的目录下会多出一个.retry文件)
- debug: var=print_output.stdout_lines # 将文件中的内容打印出来
- name: configure loop interfact # 剧情的名字(第2集)
ios_config: # 使用ios_config模块
parents: int loop 0 # 使用 parents参数,表示执行了这个命令之后,才能执行lines
lines: # 使用lines参数,执行多条命令
- ip add 1.1.1.1 255.255.255.0
- no sh
register: print_output
- debug: var=print_output
... # yaml以...结尾
执行效果如下(动图):
2) 第二个playbook
第二个playbook,我将实现免密登录和循环。playbook如下:
[root@yunwei ~]# cat ywsn_playbook2.yaml
---
- name: yunweishaonian ansible and cisco 2
hosts: cisco-1
gather_facts: false
connection: local
vars: # 定义一个变量
authinfo: # 变量名称
username: "cisco" # 变量值
password: "123456" # 变量值
tasks:
- name: config loop interfact
ios_config:
provider: "{{ authinfo }}" # 验证,验证时使用前面定义的变量
lines: # 命令
- switchport trunk encapsulation dot1q
- switchport mode trunk
parents: "{{ item }}" # 父配置 -- 执行这个,再执行上面的lines(子配置)
with_items: # 循环以下变量,循环的值为 item
- int Ethernet 1/0
- int Ethernet 1/1
- int Ethernet 1/2
- name: show int trunk
ios_command:
provider: "{{ authinfo }}"
commands:
- show int trunk
register: print_output
- debug: var=print_output.stdout_lines
...
[root@yunwei ~]#
执行结果如下(动图):
3) 最后一个playbook
最后一个playbook,实现引用外部文件的变量、判断。
外部文件config.yaml内容,里面是字典dict,当循环inter的时候,会有三个key,分别是switch、ip、port
---
inter:
- {switch: 192.168.108.251, ip: 1.1.1.1, port: loop 1}
- {switch: 192.168.108.252, ip: 2.2.2.2, port: loop 2}
- {switch: 192.168.108.253, ip: 3.3.3.3, port: loop 3}
...
hosts 文件
[root@yunwei ~]# cat /etc/ansible/hosts
[cisco]
192.168.108.251
192.168.108.252
192.168.108.253
[root@yunwei ~]#
最终代码:
[root@yunwei ~]# cat ywsn_playbook3.yaml
---
- name: yunweishaonian ansible and cisco 2
hosts: cisco
gather_facts: false
connection: local
vars_files: # 变量文件,多个变量文件的变量名请不要重复
- config.yaml
vars:
authinfo:
username: "cisco"
password: "123456"
tasks:
- name: config int loop
ios_config:
provider: "{{ authinfo }}"
lines:
- ip add {{ item.ip }} 255.255.255.0
- no sh
parents:
- interface {{ item.port }}
with_items: "{{ inter }}" # 循环inter 字典,得到switch\ip\port三个key
when: (item.switch == inventory_hostname) # 判断 switch的值是否等于登录的IP,inventory_hostname是一个默认参数,判断之后,执行上parents和lines
- name: config ospf
ios_config:
provider: "{{ authinfo }}"
lines:
- network 0.0.0.0 255.255.255.255 area 0
parents:
- router ospf 10
- name: save switch configure # 保存配置
ios_config:
provider: "{{ authinfo }}"
save_when: modified # 保存配置的条件是,配置发生变化。
...
运行效果如下(动图):
查看swithc1的路由表,如图: