Operation and Maintenance Junior Series-ansbile and cisco (2)

Operation and Maintenance Junior Series-ansbile and cisco (2)

Sao Nian Operation and Maintenance Youth

ansible and cisco (2)

1. Ansible playbook
2. Use of playbook
1) Use of the first playbook
2) Second playbook
3) Last playbook
3. Last

一、ansible playbook

  • playbook
    playbook translates to a script, what is a script? It is the screenwriter who writes all the situational words, and the actors execute and express them. In ansible, we are screenwriters, and devices such as routers and switches are actors. So this script needs to be written by us, and then executed by the device. (It's not very professional, you can Baidu ansible playbook yourself), the playbook should be written in yaml format.

  • What the playbook can do The
    previous article demonstrated the execution of a single command. The playbook is more flexible than the previous mode. We can add loops, judge, call multiple modules, execute multiple commands, etc. in the playbook... So the playbook is ( Pack 13) essential.

Second, the use of playbook

  • YAML
    YAML is not much different from JSON and XML, and has a specific format. You can take a look at the simple usage of YAML. Be sure to pay attention to spaces, indentation can only be indented with spaces.

  • The module
    needs to view the network module can click here to view. What we mainly use today is the ios_command and ios_config modules, as shown below:

Operation and Maintenance Junior Series-ansbile and cisco (2)

点击模块名,可以看到里面的参数和使用的例子,如下图:

Operation and Maintenance Junior Series-ansbile and cisco (2)

  • ios_command The
    ios_command module can be used on IOS router switches. This module can only be used in the (#) privileged mode, but not in the (config#) global configuration mode.

  • ios_config
    ios_config module, same as above, but can only be used in (config#) global configuration mode, not in (#) privileged mode

1) Use of the first playbook

The first playbook is relatively simple, the following notes I will explain as a screenwriter:


 1[root@yunwei ~]# cat ywsn_playbook1.yaml 
 2---  # yaml以---开头  # 一定要注意以下参数的空格,-和: 后面一定要有空格!!!
 3- name: show clock  # 剧本的名字
 4  hosts: cisco-2    # 表演的演员 -- 执行的主机(在hosts文件中)
 5  gather_facts: false # 检查演员的基本信息 -- 检查主机配置等信息
 6  connection: local # 剧本在谁的手里
 7  tasks:  # 剧情
 8    - name: show clock # 剧情的名字(第1集)
 9      ios_command:    # 使用ios_command模块
10        commands:     # 使用ios_command模块下的commands参数(即命令执行)
11          - show clock # 执行的命令
12      register: print_output # 保存在一个文件中(执行的目录下会多出一个.retry文件)
13    - debug: var=print_output.stdout_lines # 将文件中的内容打印出来
14
15    - name: configure loop interfact # 剧情的名字(第2集)
16      ios_config:  # 使用ios_config模块
17        parents: int loop 0  # 使用 parents参数,表示执行了这个命令之后,才能执行lines
18        lines:  # 使用lines参数,执行多条命令
19          - ip add 1.1.1.1 255.255.255.0
20          - no sh
21      register: print_output
22    - debug: var=print_output
23
24... # yaml以...结尾

The execution effect is as follows (the animation is slower):

Operation and Maintenance Junior Series-ansbile and cisco (2)

2) The second playbook

In the second playbook, I will implement password-free login and looping. The playbook is as follows:


 1[root@yunwei ~]# cat ywsn_playbook2.yaml 
 2---
 3- name: yunweishaonian ansible and cisco 2 
 4  hosts: cisco-1
 5  gather_facts: false
 6  connection: local
 7
 8  vars:  # 定义一个变量
 9    authinfo: # 变量名称
10      username: "cisco"  # 变量值
11      password: "123456" # 变量值
12
13  tasks:
14    - name: config loop interfact
15      ios_config:
16        provider: "{{ authinfo }}"  # 验证,验证时使用前面定义的变量
17        lines:  # 命令
18          - switchport trunk encapsulation dot1q
19          - switchport mode trunk
20        parents: "{{ item }}"  # 父配置 -- 执行这个,再执行上面的lines(子配置)
21      with_items:  # 循环以下变量,循环的值为 item
22        - int Ethernet 1/0
23        - int Ethernet 1/1
24        - int Ethernet 1/2
25    - name: show int trunk
26      ios_command:
27        provider: "{{ authinfo }}"
28        commands:
29          - show int trunk
30      register: print_output
31    - debug: var=print_output.stdout_lines
32
33...
34
35[root@yunwei ~]# 

The execution results are as follows (the animation is slower):

Operation and Maintenance Junior Series-ansbile and cisco (2)

3) The last playbook

The last playbook implements variables and judgments referencing external files.

The content of the external file config.yaml, which is the dictionary dict. When looping inter, there will be three keys, namely switch, ip, port


1---
2inter:
3  - {switch: 192.168.108.251, ip: 1.1.1.1, port: loop 1}
4  - {switch: 192.168.108.252, ip: 2.2.2.2, port: loop 2}
5  - {switch: 192.168.108.253, ip: 3.3.3.3, port: loop 3}
6...

hosts file


1[root@yunwei ~]# cat /etc/ansible/hosts
2[cisco]
3192.168.108.251
4192.168.108.252
5192.168.108.253
6[root@yunwei ~]# 

Final code:


 1[root@yunwei ~]# cat ywsn_playbook3.yaml 
 2---
 3- name: yunweishaonian ansible and cisco 2
 4  hosts: cisco
 5  gather_facts: false
 6  connection: local
 7  vars_files:  # 变量文件,多个变量文件的变量名请不要重复
 8    - config.yaml  
 9
10  vars:
11    authinfo:
12      username: "cisco"
13      password: "123456"
14
15  tasks:
16    - name: config int loop
17      ios_config:
18        provider: "{{ authinfo }}"
19        lines:
20          - ip add {{ item.ip }} 255.255.255.0
21          - no sh
22        parents:
23          - interface {{ item.port }}
24      with_items: "{{ inter }}"  # 循环inter 字典,得到switch\ip\port三个key
25      when: (item.switch == inventory_hostname)  # 判断 switch的值是否等于登录的IP,inventory_hostname是一个默认参数,判断之后,执行上parents和lines
26
27    - name: config ospf
28      ios_config:
29        provider: "{{ authinfo }}"
30        lines:
31          - network 0.0.0.0 255.255.255.255 area 0
32        parents:
33          - router ospf 10
34
35    - name: save switch configure  # 保存配置
36      ios_config: 
37        provider: "{{ authinfo }}"
38        save_when: modified  # 保存配置的条件是,配置发生变化。
39...

The running effect is as follows (the animation is slower):

Operation and Maintenance Junior Series-ansbile and cisco (2)

Check the routing table of swithc1, as shown in the figure:

Operation and Maintenance Junior Series-ansbile and cisco (2)

Three, finally

Operation and Maintenance Junior Series-ansible and cisco I guess this is the end, click on the lower right corner of the brother who is watching [Looking], okay, thank you~

Guess you like

Origin blog.51cto.com/15082392/2656469