Setup
Ansible
  • Introduction
  • Development Environment
  • NDFC
  • ND/NDFC REST API
  • NDFC Postman
  • NDFC Python
  • NDFC Ansible
  • NDFC CI/CD

Ansible and NDFC

Ansible is an open source community project by RedHat and is one of the simplest ways to automate your network. Ansible can be used across entire teams, ranging from systems administrators to network administrators to developers and managers. Ansible provides an enterprise-ready, task-based, agentless architecture automation solution for not only servers and software, but also networking starting in Ansible 2.1. Further, the Ansible backend makes extensive use of Python.

Cisco has developed many Ansible Collections that integrate with Cisco products to provide powerful building blocks that can be used to manage your network. The Collection we will be using in this lab integrates with NDFC. We will dive into more details about Ansible Collections later in this lab; first we need to install Ansible.



Ansible Setup

Step 1 - Install Ansible

Return to your Visual Studio Code Terminal window.

Create a directory called ansible under the /home/cisco/Documents/ndfclab project directory.


mkdir -p ~/workspace/ndfclab/ansible
cd ~/workspace/ndfclab/ansible

Within the ~/workspace/ndfclab/ansible directory create additional directories;

  • group_vars
  • Directories for staging, production and global(all) variables under group_vars
  • Directories for roles and collections

This directory structure will keep things organized for clarity and demonstrate some other ways to work with file structures in Ansible.

Reminder

Remember that you can click the copy button in the upper right hand corner of each Visual Sudio Code Termnial section in this lab guide and then paste it into the actual VSCode terminal instead of typing everything out!

Simply hover your mouse pointer above the upper right hand corner of the window below to make the copy button appear.


mkdir group_vars
mkdir -p group_vars/all
mkdir -p group_vars/stage
mkdir -p group_vars/prod
mkdir roles
mkdir collections

Install Ansible using pip install ansible-core==2.17.10 by copying or typing the command into your VSCode Terminal.


pip install ansible-core==2.17.10

Verify Ansible was installed by checking the version. You'll be working with Ansible Core release 2.17.10.


ansible --version

Upon a successful installation and verification of the Ansible version, your output should look as follows:

    ansible [core 2.17.10]
      config file = None
      configured module search path = ['/home/cisco/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
      ansible python module location = /home/cisco/.pyenv/versions/3.11.9/envs/ndfclab/lib/python3.11/site-packages/ansible
      ansible collection location = /home/cisco/.ansible/collections:/usr/share/ansible/collections
      executable location = /home/cisco/.pyenv/versions/ndfclab/bin/ansible
      python version = 3.11.9 (main, Jan  1 2025, 21:04:08) [GCC 9.4.0] (/home/cisco/.pyenv/versions/3.11.9/envs/ndfclab/bin/python)
      jinja version = 3.1.5
      libyaml = True

Step 2 - Install JMESPath

Install jmespath using pip install jmespath==1.0.1 which is a Python package for a JSON query language and will be used by an Ansible plugin in this lab.


pip install jmespath==1.0.1


Step 3 - Create Ansible Config File

Create an ansible.cfg file to disable hostkey checking and set your python interpreter for the purposes of this lab. Additionally, NDFC Ansible modules require the Ansible persistent_connection to have some values modified. The command_timeout and connect_timeout are required to be set to a 1000 seconds or greater. If this is something you forget to do in your environment outside of this lab, not to worry, the modules will notify you upon execution time.

Creating Files In This Lab

Througout this lab you will be creating files. We want to make this as easy as possible so in this particular case, all you need to do is cut and paste the following into your VSCode terminal window.

The ansible.cfg file will be created, populated and saved in one step!


touch ~/workspace/ndfclab/ansible/ansible.cfg
cat << EOF > ~/workspace/ndfclab/ansible/ansible.cfg

[defaults]
interpreter_python = "$PYENV_VIRTUAL_ENV/bin/python"
host_key_checking = False
collections_path = ./collections/
collections_on_ansible_version_mismatch = ignore

[persistent_connection]
command_timeout=1000
connect_timeout=1000

EOF


Introduction to Ansible Content Collections

While a complete discussion around Ansible Content Collections is outside the scope of this Lab, this section provides some basic background information to ensure that you understand basic collection terms and functions.

Ansible Content Collections

Ansible Content Collections are a packaging format for bundling and distributing Ansible content such as plugins, roles and modules. They can be released independent of other Ansible collections or the ansible-base engine so features can be made available sooner to users. They are installed using the ansible-galaxy collection install command

Ansible Galaxy Command Syntax

ansible-galaxy collection install [namespace.collection]

The NDFC Ansible collection namespace for this lab is cisco and the collection name is dcnm.

This represents the Fully Qualified Collection Name(FQCN) for the NDFC Ansible collection.

Step 4 - Install NDFC Ansible Collection

Ansible requires this collection of NDFC building blocks or modules to connect and configure a VXLAN EVPN Fabric using NDFC. This collection called cisco.dcnm needs to be installed on your development server.


ansible-galaxy collection install cisco.dcnm -p ./collections

Use the following ansible-galaxy command to verify the collection was installed properly.


ansible-galaxy collection list | grep -A 5 "ndfclab/ansible"

You should see the following output:

    # /home/cisco/Documents/ndfclab/ansible/collections/ansible_collections
    Collection                               Version
    ---------------------------------------- -------
    cisco.dcnm                               3.7.0 

Ansible Documentation

All documentation for the NDFC Ansible collection can be found on Ansible Galaxy. Navigate to Ansible Galaxy to see the list of modules and review the documentation.