Chapter 8 Data Models and Dynamic Templates
For diagrams, tables, or exact code formatting, .
Exact Cisco configuration commands, including syntax, nesting, and spacing, are required to develop idempotent configurations. If the spacing is off the playbook will still execute and make the changes, however during check mode or future executions of the playbook the incorrectly spaced lines of configuration will always show up as a change, even if the commands are already present on the device.
What is a Template? Ansible uses Jinja2 templates to enable dynamic expressions and access variables in the group_vars and host_vars data dictionaries. All templates are compiled on the local Linux host at run time and then deployed to the target host. This is done to minimize the amount of information that is passed to the target device allowing only what is required to execute the playbook.
Static / Explicit Statements
With network device configurations there are a lot of static configuration commands without any real valuable information that can be abstracted into a data model. Often it is desired to explicitly execute commands and not require any dynamic data from the dictionary. Many global configurations follow this pattern. Consider the following template, the first template, 01_ios_global.j2, which covers all devices, regardless of platform, and the global configuration commands we will explicitly run on each device:
* Important note *: All of the IP addresses, routes, and other “data” used in these examples are purely fictitious and not intended to function on a network. The format and syntax is correct however the IP address values have all been randomly picked using private IP addresses.
no service pad
service tcp-keepalives-in
service tcp-keepalives-out
service timestamps debug datetime msec localtime
service timestamps log datetime msec localtime
service password-encryption
service compress-config
service counters max age 5
service compress-config
no ip bootp server
diagnostic bootup level complete
errdisable recovery interval 60
aaa new-model
clock timezone est -5 0
clock summer-time EDT recurring
ip arp proxy disable
udld enable
no ip domain-lookup
vtp domain ayn
vtp mode transparent
login on-failure log
login on-success log
spanning-tree mode rapid-pvst
spanning-tree extend system-id
ip ssh version 2
no ip forward-protocol nd
no ip http server
no ip http secure-server