Before starting this task, make sure you are on the stage branch
git branch --show-current
stage
Before starting this section, it might be helpful to close out your file tabs at the top of your VSCode editor from the previous lab task. This is certainly not mandatory but might keep the flow more organized and uncluttered.
In this part of the lab we will be working with Jinja2 tempates. Your lab has two sub-directories in the confiure_overlay
role directory to store the Jinja2 templates and rendered configuration files.
This use of Jinja2 templates makes roles highly reusable and allows for the separation of configuration data from the actual configuration files. This is a best practice in Ansible development.
The diagram above demonstrates how the main playbook tasks will call the Jinja2 templates to render the configuration files that will be used to deploy the VRFs and Networks to the fabric.
ansible.builtin.template
moduleroles/configure_overlay/templates/cisco_live_attach_vrfs.j2
Jinja2 templateroles/configure_overlay/files/cisco_live_attach_vrfs.yml
using the VRF variable data defined in group_vars/all/overlay.yml
vrf_config
which is then passed to the cisco.dcnm.dcnm_vrf
module to create the VRF objects in NDFC
In this task you will examine the playbook file below that calls the cisco.dcnm.dcnm_vrf
and cisco.dcnm.dcnm_network
Ansible modules. This playbook will be used to
create the VRF and Networks that are part of the overlay using the Jinja2 templates we learned about above.
---
# -------------------
# CREATE VRF SECTION
# -------------------
- name: Create file to hold rendered VRF information
ansible.builtin.template:
src: cisco_live_attach_vrfs.j2
dest: "{{ role_path }}/files/cisco_live_attach_vrfs.yml"
- name: Create and store generated VRF configuration
ansible.builtin.set_fact:
vrf_config: "{{ lookup('file', 'cisco_live_attach_vrfs.yml') | from_yaml }}"
# -----------------------
# CREATE NETWORK SECTION
# -----------------------
- name: Create file to hold rendered Network information
ansible.builtin.template:
src: cisco_live_attach_nets.j2
dest: "{{ role_path }}/files/cisco_live_attach_nets.yml"
- name: Create and store generated Network configuration
ansible.builtin.set_fact:
net_config: "{{ lookup('file', 'cisco_live_attach_nets.yml') | from_yaml }}"
# --------------------------------------------------------------------
# Manage VRF Configuration on NDFC
# --------------------------------------------------------------------
- name: Manage NDFC Fabric VRFs
cisco.dcnm.dcnm_vrf:
fabric: "{{ fabric.name }}"
state: replaced
config: "{{ vrf_config }}"
# --------------------------------------------------------------------
# Manage Network Configuration on NDFC
# --------------------------------------------------------------------
- name: Manage NDFC Fabric Networks
cisco.dcnm.dcnm_network:
fabric: "{{ fabric.name }}"
state: replaced
config: "{{ net_config }}"
The playbook above uses a Jinja2 template to render the VRF configuration. The template is located in the
roles/configure_overlay/templates
folder and is named cisco_live_attach_vrfs.j2
. Below is the content of the template.
---
# This file is auto-generated
# DO NOT EDIT MANUALLY
#
{% for vrf in vrfs %}
- vrf_name: {{ vrf['vrf_name'] }}
{# ------------------------------------------------------ #}
{# Properties Section #}
{# ------------------------------------------------------ #}
vrf_id: {{ vrf['vrf_id'] }}
vlan_id: {{ vrf['vlan_id'] }}
{# ------------------------------------------------------ #}
{# Attach Group Section #}
{# ------------------------------------------------------ #}
attach:
{% for switch in vrf_attach_group.all_leaf %}
- ip_address: {{ switch['ip_address'] }}
{% endfor %}
deploy: true
{% endfor %}
overlay.yml
under the group_vars/all folder defines variables
for all VRFs and Network objects that will be configured in the overlay.
---
vrfs:
- vrf_name: &refvrf_devnet vrf_devnet
vrf_id: 150001
vlan_id: 2000
attach_group: all_leaf
networks:
- net_name: network_devnet1
vrf_name: *refvrf_devnet
net_id: 130001
vlan_id: 2301
vlan_name: network_devnet1_vlan2301
gw_ip_subnet: "10.10.10.1/24"
attach_group: esxi
- net_name: network_devnet2
vrf_name: *refvrf_devnet
net_id: 130002
vlan_id: 2302
vlan_name: network_devnet2_vlan2302
gw_ip_subnet: "10.10.11.1/24"
attach_group: esxi
This generates the following configuration for the VRFs:
---
# This file is auto-generated
# DO NOT EDIT MANUALLY
#
- vrf_name: vrf_devnet
vrf_id: 150001
vlan_id: 2000
attach:
- ip_address: 10.15.30.12
- ip_address: 10.15.30.13
deploy: true
File /home/cisco/CiscoLive/DEVWKS-3928/group_vars/stage/overlay.yml defines the switch specific settings for the overlay.
Create the file and add content to the file using the following commands.
touch /home/cisco/CiscoLive/DEVWKS-3928/group_vars/stage/overlay.yml
cat << EOF > /home/cisco/CiscoLive/DEVWKS-3928/group_vars/stage/overlay.yml
---
vrf_attach_group:
all_leaf:
- ip_address: 10.15.30.12
- ip_address: 10.15.30.13
attach_group:
esxi:
- ip_address: 10.15.30.12
ports:
- Ethernet1/15
- ip_address: 10.15.30.13
ports:
- Ethernet1/15
EOF