In this part of the lab you will add a spine device and two leaf devices
add_inventory
RoleFor the add_inventory role tasks, ensure you are in your VSCode Terminal and open the main.yml file found in the roles/add_inventory/tasks directory using the VSCode code keyword as before.
code-server -r ~/workspace/ndfclab/ansible/roles/add_inventory/tasks/main.yml
Copy the below tasks into the roles/add_inventory/tasks/main.yml
file that uses various Ansible built-in modules
to import other tasks depending on the inventory management needed or desired.
The imported tasks will use add switch inventory to your fabric statically or via Power On Auto Provisioning (POAP).
The last imported task will add switches to an external fabric once it's created later in this lab.
- ansible.builtin.debug:
msg:
- "----------------------------------------------------------------"
- "+ Calling Role - [add_inventory] +"
- "----------------------------------------------------------------"
tags:
- ai_fabric
- ai_poap
- ai_external
- ai_all
- name: Include Tasks to Add Devices to Local Fabric
ansible.builtin.import_tasks: add_fabric_devices.yml
tags:
- ai_fabric
- ai_all
- name: Include Tasks To Query For POAP Enabled Switches
ansible.builtin.import_tasks: add_fabric_devices_poap.yml
when: fabric_inventory | selectattr('poap', 'defined') | list | length>0
tags:
- ai_poap
- ai_all
- name: Include Tasks to Add Devices to External Fabric
ansible.builtin.import_tasks: add_fabric_external_devices.yml
tags:
- ai_external
- ai_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.
add_fabric_devices.yml
Subtask File in the add_inventory
Role
Open the add_fabric_devices.yml
tasks file that will be populated and used to create your initial fabric in NDFC.
code-server -r ~/workspace/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_devices.yml
add_fabric_devices.yml
for Statically Adding Switches
Copy the below task that uses the dcnm_inventory
module to add your switches to your newly created fabric but will not deploy configuration to the switches yet.
This task will add your switches based on the data in your fabric_settings
and fabric_inventory
variable data.
Your fabric_inventory
variable data is a list of dictionaries that contains both statically defined switches as well as a switch you will add via POAP later in this lab.
The selectattr
filter is used to filter out the POAP switch from the list of switches to be added to the fabric while allowing you to use the same variable data for all switch inventory information.
As you build out your playbook and populate the roles created in the Ansible NDFC Roles Setup section,
it's important to draw your attention to the parameter setting deploy: false
.
This is being done in this lab to optimize execution time.
You will see this throughout the lab as you build out other roles and their respective tasks.
Deployment will be handled by the deploy
role.
---
- name: Add switches to Fabric {{ fabric_settings.FABRIC_NAME }} in NDFC
cisco.dcnm.dcnm_inventory:
fabric: "{{ fabric_settings.FABRIC_NAME }}"
config: "{{ fabric_inventory | selectattr('poap', 'undefined') | list }}"
deploy: false
save: true
state: merged
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.
add_fabric_devices_poap.yml
& add_fabric_external_devices.yml
Just like the last section, you need to add empty files for placeholders that will be imported as tasks and will be filled in with tasks later in this lab:
ansible.builtin.import_tasks: add_fabric_devices_poap.yml
ansible.builtin.import_tasks: add_fabric_external_devices.yml
touch ~/workspace/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_devices_poap.yml
cat << EOF > ~/workspace/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_devices_poap.yml
---
EOF
touch ~/workspace/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_external_devices.yml
cat << EOF > ~/workspace/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_external_devices.yml
---
EOF
build_fabric.yml
Ansible PlaybookEnsure you are in your VSCode Terminal and open the build_fabric.yml file using the VSCode code keyword as before.
code-server -r ~/workspace/ndfclab/ansible/build_fabric.yml
add_inventory
Under the roles: Section of Main Playbook
Your build_fabric.yml file should already be populated from the previous section. With the file open, you only need to add the highlighted line, which should be line number 11
in your file. You can do this by highlighting the text in the lab guide and copying then pasting in your file or typing the line in your file. After one of those actions, press the
return key such that there is a new line after where you entered - add_inventory
.
- create_fabric
.
---
# 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
- add_inventory # Add This Line Under The create_fabric role from the previous section
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.
Make sure you are in the root Ansible directory
cd ~/workspace/ndfclab/ansible
From the root ansible project directory execute the following command.
time ansible-playbook -i hosts.stage.yml build_fabric.yml --tags ai_fabric
Again, the actual configuration deployment will not take place yet, but this add_inventory
role is doing the following:
Later in the lab you will deploy the configuration to the switches all at once which will take a little bit longer to:
Go ahead try the next step while the Ansible playbook is running so you can see the switches getting added into NDFC.
In your NDFC browser, access fabric-stage and navigate to the switches view.
Here you will start to see the spine and leaf devices being discovered and added to the staging
fabric.
You will see the switches transistion to different states during discovery:
This might be a good time for a cup of coffee or tea while the switches get added to your staging fabric!
You included the time
command in front of the ansible-playbook
command in order to time how long it takes.
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 [WARNING]: file /home/cisco/Documents/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_devices_poap.yml is empty and had no tasks to include [WARNING]: file /home/cisco/Documents/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_external_devices.yml is empty and had no tasks to include PLAY [Build VXLAN EVPN Fabric on NDFC] ******************************************************************************************************************************************************************** TASK [add_inventory : ansible.builtin.debug] ************************************************************************************************************************************************************** ok: [10.15.0.11] => { "msg": [ "----------------------------------------------------------------", "+ Calling Role - [add_inventory] +", "----------------------------------------------------------------" ] } TASK [add_inventory : Add switches to Fabric fabric-stage in NDFC] **************************************************************************************************************************************** [WARNING]: Managing fabric switches can take a while. Please be patient... changed: [10.15.0.11] PLAY RECAP ************************************************************************************************************************************************************************************************ 10.15.0.11 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 real 4m14.593s user 0m7.080s sys 0m2.423s
If you examine the output above you will notice that both the create_fabric
and add_inventory
roles ran
even though you already executed the create_fabric
role in the previous lab section. The reason both roles ran is that
you did not include any tags to control which tasks were executed so all tasks ran in both roles. This highlights the importance of
making sure that the Ansible playbooks are idempotent.
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.
As you build each role they will all run each time you execute the build_fabric.yml
playbook unless you specify tags to control
execution. It's imporant to note however that no work will be done to modify the fabric if the fabric is already in the desired state.
In this particular lab example it took around 3-4 minutes [real 4m14.593s]
to add the switches to the fabric but next time you execute the role it will be much quicker because the switches are already added and in the correct state.
It's also important to note here, that it only took 3-4 minutes because the dcnm_inventory
module parameter for deploy
was set to false
as you will perform the configuration deployment from NDFC to the actual switches a little bit later in the lab all one time when you rerun the playbook. Again, you are building things out in NDFC first, then will push everything to the switches all at once.
Check NDFC again and make sure the switches are added. The Mode should show as Normal and the Discovery Status should show as Ok.
If you want to put this to the test, re-run the build_fabric.yml
playbook and take note of how much quicker it runs the second time and pay
attention to the changed flag at the end. It should be set to changed=0
.
time ansible-playbook -i hosts.stage.yml build_fabric.yml
[WARNING]: file /home/cisco/Documents/ndfclab/ansible/roles/create_fabric/tasks/manage_external_fabric.yml is empty and had no tasks to include [WARNING]: file /home/cisco/Documents/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_devices_poap.yml is empty and had no tasks to include [WARNING]: file /home/cisco/Documents/ndfclab/ansible/roles/add_inventory/tasks/add_fabric_external_devices.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] ***************************************************************************************************************************************************** ok: [10.15.0.11] => (item=fabric-stage) TASK [create_fabric : Check If Fabric Exists in NDFC Log] ************************************************************************************************************************************************* ok: [10.15.0.11] => { "msg": "Fabric fabric-stage Already Exists" } TASK [create_fabric : Create Fabric fabric-stage in NDFC] ************************************************************************************************************************************************* skipping: [10.15.0.11] TASK [add_inventory : ansible.builtin.debug] ************************************************************************************************************************************************************** ok: [10.15.0.11] => { "msg": [ "----------------------------------------------------------------", "+ Calling Role - [add_inventory] +", "----------------------------------------------------------------" ] } TASK [add_inventory : Add switches to Fabric fabric-stage in NDFC] **************************************************************************************************************************************** [WARNING]: Managing fabric switches can take a while. Please be patient... ok: [10.15.0.11] PLAY RECAP ************************************************************************************************************************************************************************************************ 10.15.0.11 : ok=7 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 real 0m7.943s user 0m3.438s sys 0m0.653s
This time it only took about 8 seconds [real 0m7.943s]
because the fabric is already in the desired state!
How could you limit task execution to only the tasks in the add_inventory
role?
Use the --tags ai_all
option when executing the build_fabric.yml
playbook
On the keyword press Ctrl + K + W
. This should close all open tabs to clear your workspace for the next section.
Continue to the next section to define the Ansible tasks for setting up vPC domains between leaf switches.