Python Network Programming
上QQ阅读APP看书,第一时间看更新

Ansible 2.5 connection example

We have briefly talked about the addition of network connection changes in Ansible playbooks, starting with version 2.5. Along with the changes, Ansible also released a network best practices document, https://docs.ansible.com/ansible/latest/network/user_guide/network_best_practices_2.5.html. Let's build an example based on the best practices guide. For our topology, we will reuse the topology in Chapter 5, Low-Level Network Device Interactions, with two IOSv devices. Since there are multiple files involved in this example, the files are grouped into a subdirectory named ansible_2-5_example

Our inventory file is reduced to the group and the name of the hosts: 

$ cat hosts
[ios-devices]
iosv-1
iosv-2

We have created a host_vars directory with two files. Each corresponds to the name specified in the inventory file: 

$ ls -a host_vars/
. .. iosv-1 iosv-2

The variable file for the hosts contains what was previously included in the CLI variable. The additional variable of ansible_connection specifies network_cli as the transport:

$ cat host_vars/iosv-1
---
ansible_host: 172.16.1.20
ansible_user: cisco
ansible_ssh_pass: cisco
ansible_connection: network_cli
ansible_network_os: ios
ansbile_become: yes
ansible_become_method: enable
ansible_become_pass: cisco

$ cat host_vars/iosv-2
---
ansible_host: 172.16.1.21
ansible_user: cisco
ansible_ssh_pass: cisco
ansible_connection: network_cli
ansible_network_os: ios
ansbile_become: yes
ansible_become_method: enable
ansible_become_pass: cisco

Our playbook will use the ios_config module with the backup option enabled. Notice the use of the when condition in this example so that if there are other hosts with a different operating system, this task will not be applied:

$ cat my_playbook.yml
---
- name: Chapter 4 Ansible 2.5 Best Practice Demonstration
connection: network_cli
gather_facts: false
hosts: all
tasks:
- name: backup
ios_config:
backup: yes
register: backup_ios_location
when: ansible_network_os == 'ios'

When the playbook is run, a new backup folder will be created with the configuration backed up for each of the hosts: 

$ ansible-playbook -i hosts my_playbook.yml

PLAY [Chapter 4 Ansible 2.5 Best Practice Demonstration] ***********************

TASK [backup] ******************************************************************
ok: [iosv-2]
ok: [iosv-1]

PLAY RECAP *********************************************************************
iosv-1 : ok=1 changed=0 unreachable=0 failed=0
iosv-2 : ok=1 changed=0 unreachable=0 failed=0

$ ls -l backup/
total 8
-rw-rw-r-- 1 echou echou 3996 Jul 11 19:01 iosv-1_config.2018-07-11@19:01:55
-rw-rw-r-- 1 echou echou 3996 Jul 11 19:01 iosv-2_config.2018-07-11@19:01:55

$ cat backup/iosv-1_config.2018-07-11@19\:01\:55
Building configuration...


Current configuration : 3927 bytes
!
! Last configuration change at 01:46:00 UTC Thu Jul 12 2018 by cisco
!
version 15.6
service timestamps debug datetime msec
service timestamps log datetime msec
...

This example illustrates the network_connection variable and the recommended structure based on network best practices. We will look at offloading variables into the host_vars directory and conditionals in Chapter 8, The Python Automation Framework – Beyond Basics. This structure can also be used for the Juniper and Arista examples in this chapter. For the different devices, we will just use different values for network_connection