create_fabric
RoleFor the create_fabric role tasks, ensure you are in your VSCode Terminal and open the main.yml file found in roles/create_fabric/tasks directory using the VSCode code keyword as before.
code-server -r ~/workspace/ndfclab/ansible/roles/create_fabric/tasks/main.yml
Copy the below tasks into the roles/create_fabric/tasks/main.yml
file that uses various modules
including the dcnm_fabric
module to query the fabrics in NDFC then in the imported task will actually create the fabric.
The config
parameter takes the fabric name from the group_vars/staging/fabric.yml
file you created in the previous section
by referencing the fabric_settings
key, then the FABRIC_NAME
key respectively.
- name: Role Entry Point - [create_fabric]
ansible.builtin.debug:
msg:
- "----------------------------------------------------------------"
- "+ Calling Role - [create_fabric] +"
- "----------------------------------------------------------------"
tags:
- cf_vxlan
- cf_external
- cf_poap
- cf_all
- name: Include Tasks To Create Fabric
ansible.builtin.import_tasks: manage_fabric.yml
tags:
- cf_vxlan
- cf_all
- name: Include Tasks To Create External Fabric
ansible.builtin.import_tasks: manage_external_fabric.yml
tags:
- cf_external
- cf_all
After successfully populating the file above, save the file using Ctrl+s on the Windows keyboard or by clicking File then Save.
Be sure to save your file! Not saving will result in your code not executing.
manage_fabric.yml
Subtask File in the create_fabric
Role
Open the manage_fabric
tasks file that will be populated and used to create your initial fabric in NDFC.
code-server -r ~/workspace/ndfclab/ansible/roles/create_fabric/tasks/manage_fabric.yml
manage_fabric.yml
for Creating & Managing a VXLAN EVPN Fabric
This manage_fabric
tasks file contains the subtasks to the main create_fabric
role used to create the fabric in NDFC.
This task uses some Ansible built-in modules to check if the fabric already exists in NDFC, then creates the fabric if it does not exist using the dcnm_fabric
module.
The dcnm_fabric
module uses the variable data, fabric_settings
, from your group_vars/staging/fabric.yml
file to create the fabric.
---
- name: Query NDFC for Fabric
cisco.dcnm.dcnm_fabric:
state: query
config:
- FABRIC_NAME: "{{ fabric_settings.FABRIC_NAME }}"
register: create_fabric_result
tags:
- cf_vxlan
- cf_external
- cf_poap
- cf_all
- name: Intialize create_fabric_flag
ansible.builtin.set_fact:
create_fabric_flag: true
- name: Check If Fabric Exists in NDFC
ansible.builtin.set_fact:
create_fabric_flag: false
when: item.fabricName == fabric_settings.FABRIC_NAME
loop: "{{ create_fabric_result.response | json_query('[].DATA[]') }}"
loop_control:
label: "{{ item.fabricName }}"
- name: Check If Fabric Exists in NDFC Log
ansible.builtin.debug:
msg: "Fabric {{ fabric_settings.FABRIC_NAME }} Already Exists"
when: not create_fabric_flag
- name: Create Fabric {{ fabric_settings.FABRIC_NAME }} in NDFC
cisco.dcnm.dcnm_fabric:
state: merged
config: ["{{ fabric_settings }}"]
when: create_fabric_flag
After successfully populating the file above, save the file using Ctrl+s on the Windows keyboard or by clicking File then Save.
Be sure to save your file! Not saving will result in your code not executing.
manage_external_fabric.yml
If you examine the contents of roles/create_fabric/tasks/main.yml
closely you will notice
that in addition to importing tasks from manage_fabric.yml
there is another import statement to
import tasks for an external fabric:
ansible.builtin.import_tasks: manage_external_fabric.yml
In this lab, since you are creating highly usable Ansible roles and will be building out the tasks and subtasks at different times,
you need to create a placeholder file for this task now so your playbook will not fail when you run it.
You will fill in the content for the manage_external_fabric.yml
subtask later in this lab.
touch ~/workspace/ndfclab/ansible/roles/create_fabric/tasks/manage_external_fabric.yml
cat << EOF > ~/workspace/ndfclab/ansible/roles/create_fabric/tasks/manage_external_fabric.yml
---
EOF
build_fabric.yml
Ansible PlaybookNow you need to create the main or top level Ansible playbook that will be used to build the fabric. This file will be the main entry point for all of the tasks and roles that you will create and invoke to manage your fabric using NDFC and Ansible.
touch ~/workspace/ndfclab/ansible/build_fabric.yml
cat << EOF > ~/workspace/ndfclab/ansible/build_fabric.yml
---
# This is the top level build playbook that runs the various
# Ansible roles that will be used to build out the fabric
- name: Build VXLAN EVPN Fabric on NDFC
hosts: ndfc
gather_facts: false
roles:
- create_fabric
EOF
Make sure you are in your root Ansible directory
cd ~/workspace/ndfclab/ansible
From the root Ansible project directory execute the following command:
ansible-playbook -i hosts.stage.yml build_fabric.yml --tags cf_vxlan
You may have noticed that you included the --tags
option in the command above.
You specified the tag cf_vxlan
. This Ansible feature allows us to select which
task you want to run in your playbook. If you recall, earlier in this lab section you created
the roles/create_fabric/tasks/main.yml
playbook file and added tags to each
task. Go back and examine the playbook. By specifying the cf_vxlan
tag when
you run the playbook you only run tasks that have this tag assigned.
Make special note that you did not run tasks in the manage_external_fabric.yml
because
the import statement for those tasks does not include the cf_vxlan
tag! You
will be using Ansible tags throughout this lab to control Ansible task execution.
Upon a successful run of the playbook your output should look as follows:
[WARNING]: file /home/cisco/Documents/ndfclab/ansible/roles/create_fabric/tasks/manage_external_fabric.yml is empty and had no tasks to include PLAY [Build VXLAN EVPN Fabric on NDFC] ******************************************************************************************************************************************************************** TASK [create_fabric : Role Entry Point - [create_fabric]] ************************************************************************************************************************************************* ok: [10.15.0.11] => { "msg": [ "----------------------------------------------------------------", "+ Calling Role - [create_fabric] +", "----------------------------------------------------------------" ] } TASK [create_fabric : Query NDFC for Fabric] ************************************************************************************************************************************************************** ok: [10.15.0.11] TASK [create_fabric : Intialize create_fabric_flag] ******************************************************************************************************************************************************* ok: [10.15.0.11] TASK [create_fabric : Check If Fabric Exists in NDFC] ***************************************************************************************************************************************************** skipping: [10.15.0.11] TASK [create_fabric : Check If Fabric Exists in NDFC Log] ************************************************************************************************************************************************* skipping: [10.15.0.11] TASK [create_fabric : Create Fabric fabric-stage in NDFC] ************************************************************************************************************************************************* changed: [10.15.0.11] PLAY RECAP ************************************************************************************************************************************************************************************************ 10.15.0.11 : ok=4 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
If you examine the output above you can see that fabric-stage
was created. You might also notice warnings at the top of the
logs indicating that the two task files you created are actually empty and contain no tasks. This is normal since you created an empty placeholder file.
These warnings will go away once you build out the files later in the lab, however, it is nice that Ansible warns us about this!
Return to NDFC in your browser where you should still be sitting at the Fabrics dashboard. After the playbook has run, verify your staging fabric has been created by Ansible.
You should see the fabric fabric-stage
in the list of fabrics.
This fabric was created by the Ansible playbook you just ran using your variables and defaults of NDFC and is empty at this point.
If your fabric-stage is not showing, please try clicking the Refresh button in the top-right of the Fabrics pane.
With devops toolchains, it comes naturally that you deal with a number of files. In an effort to keep your workspace in VSCode tidy and focused on your current tasks, you can use a keyboard shortcut to close files at the end of each section.
Navigate back to your VSCode application. On the keyword press Ctrl + K + W
. This should close all open tabs to clear your workspace for the next section.
Move on to the next section to build the initial task to add switch inventory to your fabric.