Setup
Ansible
  • Introduction
  • Development Environment
  • NX-API Overview
  • NX-API Python
  • Ansible NXOS
  • pyATS
  • NetDevOps
  • Bonus: Postman
  • Bonus: YANG

Ansible Setup

Ansible is an open source community project by RedHat and is the simplest way to automate your IT. Ansible can be used across entire IT 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 is a major supported vender and here you will focus on Ansible networking automation specific to Open NXOS.


Step 1 - Install Ansible

Return to your Visual Studio Code Terminal window.

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

    
        cd /home/cisco/Documents/nxapilab
        pip install ansible==9.1.0
    

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

    
        ansible --version
    

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

    ansible [core 2.16.3]
      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.6/envs/nxapilab/lib/python3.11/site-packages/ansible
      ansible collection location = /home/cisco/.ansible/collections:/usr/share/ansible/collections
      executable location = /home/cisco/.pyenv/versions/nxapilab/bin/ansible
      python version = 3.11.6 (main, Feb  4 2024, 09:53:02) [GCC 9.4.0] (/home/cisco/.pyenv/versions/3.11.6/envs/nxapilab/bin/python3.11)
      jinja version = 3.1.3
      libyaml = True

Step 2 - Setup Ansible Playbook Directory Structure

Ansible calls it's configuration and orchestration framework "playbooks" which are a collection of "play(s)" or tasks for configuration management and deployment to a device or multiple devices in a synchronous or asynchronous fashion. While playbooks can be represented as a single file, Ansible best practices recommend a particular directory structure for playbooks that you will be building below. This directory structure lets you organize files into smaller configuration files for better reuse, particularly into "roles."

Roles in Ansible are a way to automatically load certain variables, tasks, etc depending on a categorization. A role can be defined for example by device type or protocol. In this lab, you are working with two distinct device types, spine and leaf. When you get to defining our configuration tasks and variables, distinctly differentiating between device types into separate roles can allow for all spine type devices to be configured the same and all leaf type devices to be configured the same. Additionally, you could create a third role called common for any overlapping configuration tasks between the spine and leaf switches. Before creating the configuration tasks using Ansible network modules, let us create the appropriate directory structure.

In your Terminal window you should be at the top level of your project directory. Create a directory called ansible-nxos, then change directory into your newly created playbooks directory.

    
        mkdir ansible-nxos
        cd ansible-nxos
    

A pwd on the Terminal CLI should look as follows and have you in your ansible-nxos directory:

    
        pwd
    
    /home/cisco/Documents/nxapilab/ansible-nxos

Within the ansible-nxos directory create three more directories; group_vars, host_vars, and roles.

    
        mkdir group_vars
        mkdir host_vars
        mkdir roles
        cd roles
    

Step 3 - Create Reusable Roles

You are going to use Ansible Galaxy to create your roles within the roles directory. Ansible Galaxy is the official community for downloading or creating roles and also where Ansible Collections, such as the NXOS collection you'll be using here, are hosted.

Create common role directory structure:

    
        ansible-galaxy init common
    

Upon successful creation of the common role, you will see the below line printed to the Terminal window:

    - Role common was created successfully

Create underlay role directory structure:

    
        ansible-galaxy init underlay
    

Upon successful creation of the underlay role, you will see the below line printed to the Terminal window:

    - Role underlay was created successfully

Create overlay role directory structure:

    
        ansible-galaxy init overlay
    

Upon successful creation of the overlay role, you will see the below line printed to the Terminal window:

    - Role overlay was created successfully

You can quickly view the structure of the directories and files Ansible Galaxy created by using tree. You will mainly be working within the defaults, tasks, and vars directories. The defaults and vars directories contain variables to be used by the tasks, and the tasks will be placed within the tasks directory.

    
        cd ..
        tree roles/
    
tree roles/
roles/
├── common
│   ├── README.md
│   ├── defaults
│   │   └── main.yml
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   ├── tests
│   │   ├── inventory
│   │   └── test.yml
│   └── vars
│       └── main.yml
├── overlay
│   ├── README.md
│   ├── defaults
│   │   └── main.yml
│   ├── files
│   ├── handlers
│   │   └── main.yml
│   ├── meta
│   │   └── main.yml
│   ├── tasks
│   │   └── main.yml
│   ├── templates
│   ├── tests
│   │   ├── inventory
│   │   └── test.yml
│   └── vars
│       └── main.yml
└── underlay
    ├── README.md
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── meta
    │   └── main.yml
    ├── tasks
    │   └── main.yml
    ├── templates
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml

27 directories, 24 files

In the next section, you will define these roles with tasks using NXOS collection modules and variables into practice.