Fabric
Role

Step 1 - Open The Main Task File for the create_fabric Role

For 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


Step 2 - Add Tasks to Create & Manage Fabrics in NDFC

Copy the below tasks into the roles/create_fabric/tasks/main.yml file that uses a couple modules including the dcnm_fabric module to configure the fabric in NDFC based on your previously variables. 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_all

- name: Create NDFC Fabric {{ fabric_settings.FABRIC_NAME }}
  cisco.dcnm.dcnm_fabric:
    config: ["{{ fabric_settings }}"]
    state: merged
  tags:
    - cf_vxlan
    - cf_all


Step 3 - Create a Top Level fabric.yml Ansible Playbook

Now 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/fabric.yml
cat << EOF > ~/workspace/ndfclab/ansible/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


Step 4 - Execute Ansible Playbook

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 fabric.yml --tags cf_vxlan

Ansible Tags

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.

Upon a successful run of the playbook your output should look as follows:

  PLAY [Build VXLAN EVPN Fabric on NDFC] *********************************************************************************************************************************************

  TASK [create_fabric : Role Entry Point - [create_fabric]] **************************************************************************************************************************
  ok: [10.15.0.26] => {
      "msg": [
          "----------------------------------------------------------------",
          "+             Calling Role - [create_fabric]                   +",
          "----------------------------------------------------------------"
      ]
  }

  TASK [create_fabric : Create NDFC Fabric fabric-stage] *****************************************************************************************************************************
  ok: [10.15.0.26]

  PLAY RECAP *************************************************************************************************************************************************************************
  10.15.0.26                 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

If you examine the output above you can see that the playbook run show's green for all tasks and the recap shows ok only and no changed status for creating fabric-stage. This is because the fabric already exists in NDFC and Ansible modules are typically idempotent, meaning, they are smart enough to not try to create it again. This is super useful as you can run the same playbook multiple times and it will only make changes if something is different, thus not adversely affecting your fabric.

Idempotency

An operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any intervening actions.


Step 5 - Return to VSCode & Close All Open Tabs

With DevOps toolchains, it comes naturally that you deal with several files. To 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 for managing switch inventory in your fabric.