Git Setup
NetDevOps
  • Introduction
  • CXTM Basics
  • CXTM Projects
  • CXTM Test Cases
  • CXTM Test Automation
  • Revisit Imported Test Cases
  • CXTM Batches
  • CXTM Notifications
  • NetDevOps
  • CXTM Reporting
  • CXTM References
  • Bonus: Project Users
  • Bonus: CXTM REST API
  • Bonus: Secret Env Variables

Setup Git Repo

Visual Studio Code is an interactive development environment (IDE) or simply, a code editor. You may hear Visual Studio Code referenced as VS Code or just code in the industry. VS Code is free to use for many types of development and supports various extensions for syntax checking and highlighting depending on the code language you are working with. For this lab, you will be working primarily with YAML file types. You will leverage VS Code as your code editor and GitLab's CI pipeline as your runtime environment.

Step 1 - Navigate to VS Code Server

The VS Code Server is a web-based IDE version that provides a development environment for this lab.

  1. Click on the link below to access the VS Code Server.


  2. Enter the password below into the VS Code Server login.

    • Password: cisco.123



  3. Your landing page should look similar to the below screen capture.


  4. Press Ctrl + Shift + ` (back tick) to open a new terminal window in VS Code Server


Note

If the keyboard shortcut above does not work, please open the VS Code Server hamburger menu and click Terminal > New Terminal

Step 2 - Set Git User Global Settings

Enter the following commands at the VS Code terminal prompt to set your Git global settings for your user:


git config --global user.name "POD-21"
git config --global user.email "pod21@ciscolive.com"
git config --global init.defaultBranch main


Step 3 - Initialize Local Directory as Git Repo

Enter the following command to initialize your local project directory as a Git repo:


git init


Step 4 - Add Remote Pointer to Git Repo

Enter the following command to add the remote pointer to your Git repo:


git remote add origin git@10.0.205.200:CL-POD21/LTROPS-2711.git


Step 5 - Create .gitignore File

In Git repos, it is very common to have a hidden file called .gitignore. This is a reserved filename and it is used to ignore specific file extensions and/or directories from being added to the Git repo. The file below serves as an example of a few items you would want to ignore.

Enter the following in your VS Code Terminal to create the .gitignore file:


touch /home/user21/ciscolive/LTROPS-2711/.gitignore
cat <<EOF >> /home/user21/ciscolive/LTROPS-2711/.gitignore
*.env

# VScode 
.vscode/*

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# pyenv
.python-version
EOF


Step 6 - Setup Ansible Playbook Directory Structure

In your Terminal window you should be at the top level of your project directory. Enter the following commands to create a directory called ansible. The ansible directory will be used for storing your configuration change playbook.



mkdir /home/user21/ciscolive/LTROPS-2711/ansible
touch /home/user21/ciscolive/LTROPS-2711/ansible/.keep

Enter the following commands to create two more directories within the ansible directory; group_vars and roles. These directories align to Ansible best practices for where specific YAML files will be placed.


mkdir /home/user21/ciscolive/LTROPS-2711/ansible/group_vars
touch /home/user21/ciscolive/LTROPS-2711/ansible/.keep
mkdir /home/user21/ciscolive/LTROPS-2711/ansible/roles
touch /home/user21/ciscolive/LTROPS-2711/ansible/roles/.keep



Step 7 - Create Ansible Config File

Enter the following in your Terminal window to create an ansible.cfg file to disable hostkey checking and set your python interpreter for the purposes of this lab.


touch /home/user21/ciscolive/LTROPS-2711/ansible/ansible.cfg
cat <<EOF >> /home/user21/ciscolive/LTROPS-2711/ansible/ansible.cfg
[defaults]
interpreter_python = "~/.pyenv/shims/python"
host_key_checking = False

[persistent_connection]
command_timeout=100
connect_timeout=100
EOF


Step 8 - Create Ansible Global Vars File

Copy the below YAML into the your Terminal window to create the all.yml file and populate contents of the file for the ansible_connection and username/password information. This is a file with key/value pairs. group_vars/all is where you place universal variables that apply for all devices. For all your devices, you will make the configuration changes over the CLI, so you will leverage Ansible's netcommon.network_cli plugin.

Note

For passwords, it is best practice to leverage something like Ansible Vault. For simplicity, clear text is used in this lab.


touch /home/user21/ciscolive/LTROPS-2711/ansible/group_vars/all.yml
cat <<EOF >> /home/user21/ciscolive/LTROPS-2711/ansible/group_vars/all.yml
---

ansible_connection: ansible.netcommon.network_cli
ansible_user: admin
ansible_ssh_pass: cisco.123
EOF


Step 9 - Create Ansible Inventory File

For Ansible to know what devices to connect to, you must define an inventory file, much like the topology file that was created to drive CXTM. In reality, these two files could be dynamically generated from a single-source of truth if preferred. For Ansible to use the specific OS modules, each device requires the ansible_network_os to be defined.

Enter the following into your Terminal window to create the inventory file.


touch /home/user21/ciscolive/LTROPS-2711/ansible/hosts.yml
cat <<EOF >> /home/user21/ciscolive/LTROPS-2711/ansible/hosts.yml
---
# hosts file for Ansible playbook
all:
  children:
    xe:
        hosts:
            csr1kv:
                ansible_network_os: cisco.ios.ios
                ansible_host: 10.15.121.11
            c8kv:
                ansible_network_os: cisco.ios.ios
                ansible_host: 10.15.121.12
    xr:
        hosts:
            xr9kv:
                ansible_network_os: cisco.iosxr.iosxr
                ansible_host: 10.15.121.13
    nx:
        hosts:
            n9kv:
                ansible_network_os: cisco.nxos.nxos
                ansible_host: 10.15.121.14
EOF


Step 10 - Add Files for Committing to Repo

After initializing your Git repo and creating the directories and files, it is time to add these to your repo with git add commands. Enter the following commands into your Terminal window.


git add ansible/
git add .gitignore


Step 11 - Commit Files to Git Repo

With your directories and files added, you can now commit them to your Git repo with git commit. The -m option is for a comment/message for the comment.


git commit -m "Initial commit"


Step 12 - Push Files to Git Repo

Finally, with your project directory initialized as a Git repo, your files added and committed, you can push everything to your Git repo on the GitLab instance.


git push -u origin main

Enter yes to continue connecting when presented with a prompt similar to the following.


The authenticity of host '10.0.205.200 (10.0.205.200)' can't be established.
ED25519 key fingerprint is SHA256:gJcb41tVGExNFq7UBf+z8bYRH7L1Y+WKvFPp6fbmXls.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?


Step 13 - Return to GitLab & Review Repo

Return to GitLab. Your may have to refresh the browser window. Your GitLab repo should now look similar to the screenshot below with the directories and files you just pushed now stored in source control.




Continue to the next section to create your Infrastructure as Code (IaC) configuration change playbook.