The Jinja2 template
Let's also modify the playbook accordingly. In chapter8_8.yml, we will make the following changes:
- Change the source file to nxos.j2
- Change the destination file to be a variable
- Provide the variable values as a dictionary that we will substitute in the template:
---
- name: Template Looping
hosts: localhost
vars:
nexus_devices: {
"nx-osv-1": {"hostname": "nx-osv-1", "username": "cisco",
"password": "cisco"}
}
tasks:
- name: create router configuration files
template:
src=./nxos.j2
dest=./{{ item.key }}.conf
with_dict: "{{ nexus_devices }}"
After running the playbook, you will find the destination file called nx-osv-1.conf with the values filled in and ready to be used:
$ cat nx-osv-1.conf
hostname nx-osv-1
feature telnet
feature ospf
feature bgp
feature interface-vlan
username cisco password cisco role network-operator