Ansible - NXOS

published
Run in Cisco Cloud IDE

Kickstart your Cisco NXOS automation journey with Ansible. This project will help you get started
with your Network Automation journey by show casing a simple Ansible setup which you can run in
a VSCode Devcontainer
or on any Linux machine with Python ^3.11 installed. It includes a couple of playbooks
which are used to demonstrate how easy it can be to write automation tasks for NXOS in Ansible.
Be it to configure your system or simply to get status information.

Installation

There are two ways how you can make use of this project. You can run it in a
VSCode Devcontainer
or you can install all dependencies directly on your Linux host.

Getting the code

To get the code simply clone it from Github or GitLab

git clone https://gitlab.com/anyweb/cisco-code-exchange/ansible-nxos.git

VSCode Devcontainer

To use this project with the pre-configured VSCode Devcontainer you must make sure that
you have the pre-requisites installed.
After that just go to the project folder where you cloned this project into and open VSCode

cd ansible-nxos
code .

Once the VSCode window has opened you can start the Devcontainer with:

  1. Hit Ctrl-Shift-p on your keyboard to launch the command prompt.
  2. Search for Dev Containers: Reopen in Container or Dev Containers: Rebuild Container
    in the search bar and hit enter

A container with all dependencies installed will be built and started so you can get started
without thinking about it too much.

Linux

If you don't want to or can't use VSCode Devcontainer you still can make use of this project by
following these steps.

With poetry

If Poetry is available on your system, you can install dependencies
with:

poetry install --no-root
ansible-galaxy collection install -r ./requirements_ansible.yml --force

Without poetry

Otherwise you can use Pip to install
all dependencies with:

pip install -r ./requirements_python-dev.txt
ansible-galaxy collection install -r ./requirements_ansible.yml --force

Configuration

NX-OS Authentication

All playbooks in this project connect via NX-API to your switches. Make sure that this
feature is enabled.

nxos# configure terminal
nxos(config)# feature nxapi

User name and password for authentication can be passed to ansible via environment variables.

export NXOS_USER="my-api-user"
export NXOS_PASSWORD="my-super-secure-password"

NX-OS Backup Location

Playbooks which provide a backup functionality will save the backup to the
./backup folder. You can overwrite this behavior with the NXOS_BACKUP_LOCATION environment
variable.

export NXOS_BACKUP_LOCATION="/path/to/my/backup/location"

NX-OS Configuration File

Playbook which provide a configuration functionality will load the configuration variables
from the nxos-config.yml file. You can overwrite this behavior
with the NXOS_CONFIGURATION_FILE environment variable.

export NXOS_CONFIGURATION_FILE="/path/to/my/configuration/file"

Usage

The following playbooks are available for demonstration purposes:

Playbook Description Configuration environment variable Configuration default
backup_config.yml Backup running-config to file NXOS_BACKUP_LOCATION ./backup -> NX-OS Backup Location
configure_vlans.yml Configure VLANs NXOS_CONFIGURATION_FILE ./vars/nxos-config.yml -> NX-OS Configuration File
configure_vrf.yml Configure VRFs NXOS_CONFIGURATION_FILE ./vars/nxos-config.yml -> NX-OS Configuration File
gather_facts.yml Gather and display all NX-OS facts
show_vlans.yml Display output of show vlan brief
show_vrfs.yml Display output of show vrf

To execute a playbook you can use the command ansible-playbook playbooks/{{ playbook_name }}.

Example:

ansible-playbook playbooks/show_vlans.yml

DevNet Sandbox

To try out this project you can use the Cisco NX-OS Sandbox Lab.
Sandbox details such as username & password can be found here.

Author(s)

This project was written and is maintained by the following individuals:

View code on GitHub

Code Exchange Community

Get help, share code, and collaborate with other developers in the Code Exchange community.View Community
Disclaimer:
Cisco provides Code Exchange for convenience and informational purposes only, with no support of any kind. This page contains information and links from third-party websites that are governed by their own separate terms. Reference to a project or contributor on this page does not imply any affiliation with or endorsement by Cisco.