The Jinja2 template

Let's also modify the playbook accordingly. In chapter8_8.yml, we will make the following changes:

  1. Change the source file to nxos.j2
  2. Change the destination file to be a variable
  3. 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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.37.161