ansible 初体验:使用ansible配置cisco设备

实验环境

1台 linux red hat 虚拟机,
1台CSR 1000V;

playbook

playbook 包括密码secrets.yml和play-book.yml 。关于yml文件的写法,有几个需要注意的地方:
1.字符串可以不用引号包含起来;
2. “{{ }}” 表示引用变量.
网络 设备playbook写法可以参考:
https://docs.ansible.com/ansible/latest/modules/list_of_network_modules.html#ios

本次实验play-book.yml 如下:

---
- hosts: IOS
  gather_facts: no
  connection: local

  tasks:
   - name: OBTAIN LOGIN INFORMATION
     include_vars: secrets.yml

   - name: DEFINE PROVIDER
     set_fact:
       provider:
         host: "{{ ansible_host }}"
         username: "{{ creds['username'] }}"
         password: "{{ creds['password'] }}"
         auth_pass: "{{ creds['auth_pass'] }}"

   - name: SET IP ADDRESS
     ios_config:
       provider: "{{ provider }}"
       authorize: yes
       parents: "interface GigabitEthernet2"
       lines:
         - description SOUTH-CUSTOMER
         - ip address 10.10.20.1 255.255.255.248
         - no shutdown
     register: set_ip_result
     vars:
       ansible_persistent_log_messages: True
   - name: show result
     debug: var=set_ip_result

   - name: SAVE & BACKUP CONFIGURATION
     ios_config:
       provider: "{{ provider }}"
       authorize: yes
       save: yes
       backup: yes
     register: save_result
     vars:
       ansible_persistent_log_messages: True
   - name: show save
     debug: var=save_result

secrets.yml 如下

---

creds:
  username: "ethan"
  password: "ethan"
  auth_pass: "ethan"

host 文件

host 文件写法可参考:
https://www.jianshu.com/p/65b2407950bc
host 文件如下:

[IOS]
router_a ansible_host=192.168.124.12

运行

ansible-playbook -i hosts play-book.yml

排错

运行过程中,提示unable to open shell ,

参考官方排错文档,是由于ssh-key的原因导致。
配置文件中修改
[paramiko_connection]
host_key_auto_add = True
后解决

https://docs.ansible.com/ansible/latest/network/user_guide/network_debug_troubleshooting.html#error-connecting-to-host-hostname-returned-an-error-or-bad-address

总结

写Playbook 需要不少时间,从这个例子的体验来看,在网络场景中,ansible适用于大量设备相同配置的情况,例如acl,ntp, bgp client ;
ansible 用于网络设备软件版本升级,为不同设备配置不同参数等场景需要进一步研究学习。

你可能感兴趣的:(ansible)