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.
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 statically defined switches and could very well include switches you would wish to add via POAP
(POAP is out of scope for this section and available in the reference content).
The selectattr
filter is used to filter out any 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: Role Entry Point - [add_inventory]
ansible.builtin.debug:
msg:
- "----------------------------------------------------------------"
- "+ Calling Role - [add_inventory] +"
- "----------------------------------------------------------------"
tags:
- ai_fabric
- ai_all
- name: Add switches to NDFC Fabric {{ fabric_settings.FABRIC_NAME }}
cisco.dcnm.dcnm_inventory:
fabric: "{{ fabric_settings.FABRIC_NAME }}"
config: "{{ fabric_inventory | selectattr('poap', 'undefined') | list }}"
deploy: false
save: true
state: merged
tags:
- ai_fabric
- ai_all
fabric.yml
Ansible PlaybookEnsure you are in your VSCode Terminal and open the fabric.yml file using the VSCode code keyword as before.
code-server -r ~/workspace/ndfclab/ansible/fabric.yml
add_inventory
Under the roles: Section of Main Playbook
Your 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
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 fabric.yml
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:
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] TASK [add_inventory : Role Entry Point - [add_inventory]] ************************************************************************************************************************** ok: [10.15.0.26] => { "msg": [ "----------------------------------------------------------------", "+ Calling Role - [add_inventory] +", "----------------------------------------------------------------" ] } TASK [add_inventory : Add Switches to NDFC Fabric fabric-stage] ******************************************************************************************************************** [WARNING]: Managing fabric switches can take a while. Please be patient... ok: [10.15.0.26] PLAY RECAP ************************************************************************************************************************************************************************* 10.15.0.26 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 real 0m10.750s user 0m5.594s sys 0m0.999s
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 and modules are idempotent.
As you build each role, they will all run each time you execute the 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.
It's also important to note here, that 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.
How could you limit task execution to only the tasks in the add_inventory
role?
Use the --tags ai_all
option when executing the fabric.yml
playbook. Feel free to give it a try!
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.