Git Setup
NetDevOps

Return to your VSCode application window. It is now time to setup and initialize your Git repo.


Step 1 - Change Directory Back to Project Top Level

Ensure you are at the top level directory for your project folder:


cd /home/pod14/workspace/nxapilab


Step 2 - Set Git User Global Settings

Set your Git global settings:


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


Step 3 - Initialize Local Directory as Git Repo

Initialize your local project directory as git repo:


git init


Step 4 - Add Remote Pointer to Git Repo

Add the remote pointer to your Git repo:


git remote add origin git@10.15.0.159:Pod14_2025_01/LTRDCN-3903.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. An example of a specific file you do not want added to your Git repo is your .python-version hidden file that specifies your pyenv environment.


touch /home/pod14/workspace/nxapilab/.gitignore
cat <<EOF > /home/pod14/workspace/nxapilab/.gitignore
*.env
scripts/
tests/results/*.zip
secrets.sh

# VScode 
.vscode/*

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

# pyenv
.python-version
EOF


Step 6 - Create Prod Ansible Host Specific Files

As you previously stepped through building out your infrastructure as code with Ansible, there were host specific variables created for your staging spine switch and leaf switches. Again, host specific in this case is switch specific. The Ansible roles you created are agnostic to the fabric they are run against. So, you need to simply create the host specific variable files for the prod fabric switches. Since the staging fabric is a development fabric and thus a mirror image of your prod fabric, you simply need to copy the each host specific file for staging to its prod counterpart.


cp ansible-nxos/host_vars/10.15.14.11.yml ansible-nxos/host_vars/10.15.14.14.yml
cp ansible-nxos/host_vars/10.15.14.12.yml ansible-nxos/host_vars/10.15.14.15.yml
cp ansible-nxos/host_vars/10.15.14.13.yml ansible-nxos/host_vars/10.15.14.16.yml
sed -i '0,/^\([[:space:]]*hostname: *\).*/s//\hostname: prod-spine1/;' ansible-nxos/host_vars/10.15.14.14.yml
sed -i '0,/^\([[:space:]]*hostname: *\).*/s//\hostname: prod-leaf1/;' ansible-nxos/host_vars/10.15.14.15.yml
sed -i '0,/^\([[:space:]]*hostname: *\).*/s//\hostname: prod-leaf2/;' ansible-nxos/host_vars/10.15.14.16.yml


Step 7 - Create Prod Ansible Group

As discussed in the previous step, the Ansible host file needs to be created for prod as you did for staging.


touch /home/pod14/workspace/nxapilab/ansible-nxos/prod.yml
cat <<EOF > /home/pod14/workspace/nxapilab/ansible-nxos/prod.yml
# hosts file for Ansible playbook
---
all:
  children:
    spines:
      hosts:
        10.15.14.14:
    leafs:
      hosts:
        10.15.14.15:
        10.15.14.16:
EOF


Step 8 - Create Prod pyATS Topolgoy File

And finally, the pyATS testbed YAML file needs to be created for your prod fabric so that the tests can run against your prod fabric.

  
touch /home/pod14/workspace/nxapilab/tests/prod-testbed.yaml
cat <<EOF > /home/pod14/workspace/nxapilab/tests/prod-testbed.yaml
---
testbed:
  name: NX-API Prod Lab
  alias: Prod
  credentials:
    default:
      username: admin
      password: cisco.123
devices:
  prod-spine1:
    alias: n9kv-s1
    type: switch
    os: nxos
    platform: n9k
    connections:
      defaults:
        via: rest
      ssh:
        protocol: ssh
        ip: 10.15.14.14
      rest:
        class: rest.connector.Rest
        protocol: https
        ip: 10.15.14.14
  prod-leaf1:
    alias: n9kv-l1
    type: switch
    os: nxos
    platform: n9k
    connections:
      defaults:
        via: rest
      ssh:
        protocol: ssh
        ip: 10.15.14.15
      rest:
        class: rest.connector.Rest
        protocol: https
        ip: 10.15.14.15
  prod-leaf2:
    alias: n9kv-l2
    type: switch
    os: nxos
    platform: n9k
    connections:
      defaults:
        via: rest
      ssh:
        protocol: ssh
        ip: 10.15.14.16
      rest:
        class: rest.connector.Rest
        protocol: https
        ip: 10.15.14.16
EOF


Step 9 - Add Files for Committing to Repo

After initializing your Git repo and creating the files specific to your prod fabric, it is time to add these files with Git in preparation for commiting them to the repo. You perform the action of adding files with git add. You will add your ansible-nxos and tests directories. Again, the Ansible playbooks and pyATS tests are not specific to either fabric and are designed to work on either fabric. Lastly, you'll add your .gitignore file. You may notice the removal of the hidden Travis YAML files. Ansible Galaxy creates these by default for Travis CI which is another option as a CI/CD platform. Since we're using GitLab for this lab, we're simply going to remove them.


rm -rf ansible-nxos/roles/common/.travis.yml
rm -rf ansible-nxos/roles/underlay/.travis.yml
rm -rf ansible-nxos/roles/overlay/.travis.yml
git add ansible-nxos/
git add tests/
git add .gitignore


Step 10 - Check Git Status

After adding what you are wanting to commit to your git repo, check the status with git status.


git status

  On branch main

  No commits yet

  Changes to be committed:
    (use "git rm --cached ..." to unstage)
          new file:   .gitignore
          new file:   ansible-nxos/ansible.cfg
          new file:   ansible-nxos/group_vars/leafs.yml
          new file:   ansible-nxos/group_vars/nxos.yml
          new file:   ansible-nxos/group_vars/spines.yml
          new file:   ansible-nxos/host_vars/10.15.1.11.yml
          new file:   ansible-nxos/host_vars/10.15.1.12.yml
          new file:   ansible-nxos/host_vars/10.15.1.13.yml
          new file:   ansible-nxos/host_vars/10.15.1.14.yml
          new file:   ansible-nxos/host_vars/10.15.1.15.yml
          new file:   ansible-nxos/host_vars/10.15.1.16.yml
          new file:   ansible-nxos/prod.yml
          new file:   ansible-nxos/roles/common/README.md
          new file:   ansible-nxos/roles/common/defaults/main.yml
          new file:   ansible-nxos/roles/common/handlers/main.yml
          new file:   ansible-nxos/roles/common/meta/main.yml
          new file:   ansible-nxos/roles/common/tasks/main.yml
          new file:   ansible-nxos/roles/common/tests/inventory
          new file:   ansible-nxos/roles/common/tests/test.yml
          new file:   ansible-nxos/roles/common/vars/main.yml
          new file:   ansible-nxos/roles/overlay/README.md
          new file:   ansible-nxos/roles/overlay/defaults/main.yml
          new file:   ansible-nxos/roles/overlay/handlers/main.yml
          new file:   ansible-nxos/roles/overlay/meta/main.yml
          new file:   ansible-nxos/roles/overlay/tasks/main.yml
          new file:   ansible-nxos/roles/overlay/tests/inventory
          new file:   ansible-nxos/roles/overlay/tests/test.yml
          new file:   ansible-nxos/roles/overlay/vars/main.yml
          new file:   ansible-nxos/roles/underlay/README.md
          new file:   ansible-nxos/roles/underlay/defaults/main.yml
          new file:   ansible-nxos/roles/underlay/handlers/main.yml
          new file:   ansible-nxos/roles/underlay/meta/main.yml
          new file:   ansible-nxos/roles/underlay/tasks/main.yml
          new file:   ansible-nxos/roles/underlay/tests/inventory
          new file:   ansible-nxos/roles/underlay/tests/test.yml
          new file:   ansible-nxos/roles/underlay/vars/main.yml
          new file:   ansible-nxos/staging.yml
          new file:   ansible-nxos/vxlan.yml
          new file:   tests/prod-testbed.yaml
          new file:   tests/pyats.robot
          new file:   tests/pyats_aetest.py
          new file:   tests/pyats_easypy.py
          new file:   tests/results/.keep
          new file:   tests/staging-testbed.yaml

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"

  [main (root-commit) e7f8557] Initial commit
    44 files changed, 1184 insertions(+)
    create mode 100644 .gitignore
    create mode 100644 ansible-nxos/ansible.cfg
    create mode 100644 ansible-nxos/group_vars/leafs.yml
    create mode 100644 ansible-nxos/group_vars/nxos.yml
    create mode 100644 ansible-nxos/group_vars/spines.yml
    create mode 100644 ansible-nxos/host_vars/10.15.1.11.yml
    create mode 100644 ansible-nxos/host_vars/10.15.1.12.yml
    create mode 100644 ansible-nxos/host_vars/10.15.1.13.yml
    create mode 100644 ansible-nxos/host_vars/10.15.1.14.yml
    create mode 100644 ansible-nxos/host_vars/10.15.1.15.yml
    create mode 100644 ansible-nxos/host_vars/10.15.1.16.yml
    create mode 100644 ansible-nxos/prod.yml
    create mode 100644 ansible-nxos/roles/common/README.md
    create mode 100644 ansible-nxos/roles/common/defaults/main.yml
    create mode 100644 ansible-nxos/roles/common/handlers/main.yml
    create mode 100644 ansible-nxos/roles/common/meta/main.yml
    create mode 100644 ansible-nxos/roles/common/tasks/main.yml
    create mode 100644 ansible-nxos/roles/common/tests/inventory
    create mode 100644 ansible-nxos/roles/common/tests/test.yml
    create mode 100644 ansible-nxos/roles/common/vars/main.yml
    create mode 100644 ansible-nxos/roles/overlay/README.md
    create mode 100644 ansible-nxos/roles/overlay/defaults/main.yml
    create mode 100644 ansible-nxos/roles/overlay/handlers/main.yml
    create mode 100644 ansible-nxos/roles/overlay/meta/main.yml
    create mode 100644 ansible-nxos/roles/overlay/tasks/main.yml
    create mode 100644 ansible-nxos/roles/overlay/tests/inventory
    create mode 100644 ansible-nxos/roles/overlay/tests/test.yml
    create mode 100644 ansible-nxos/roles/overlay/vars/main.yml
    create mode 100644 ansible-nxos/roles/underlay/README.md
    create mode 100644 ansible-nxos/roles/underlay/defaults/main.yml
    create mode 100644 ansible-nxos/roles/underlay/handlers/main.yml
    create mode 100644 ansible-nxos/roles/underlay/meta/main.yml
    create mode 100644 ansible-nxos/roles/underlay/tasks/main.yml
    create mode 100644 ansible-nxos/roles/underlay/tests/inventory
    create mode 100644 ansible-nxos/roles/underlay/tests/test.yml
    create mode 100644 ansible-nxos/roles/underlay/vars/main.yml
    create mode 100644 ansible-nxos/staging.yml
    create mode 100644 ansible-nxos/vxlan.yml
    create mode 100644 tests/prod-testbed.yaml
    create mode 100644 tests/pyats.robot
    create mode 100644 tests/pyats_aetest.py
    create mode 100644 tests/pyats_easypy.py
    create mode 100644 tests/results/.keep
    create mode 100644 tests/staging-testbed.yaml

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


  Enumerating objects: 65, done.
  Counting objects: 100% (65/65), done.
  Delta compression using up to 8 threads
  Compressing objects: 100% (40/40), done.
  Writing objects: 100% (65/65), 11.00 KiB | 751.00 KiB/s, done.
  Total 65 (delta 8), reused 0 (delta 0), pack-reused 0
  To 10.15.0.159:Pod01_2025_01/LTRDCN-3903.git
  * [new branch]      main -> main
  Branch 'main' set up to track remote branch 'main' from 'origin'.

Step 13 - Return to GitLab & Review Repo

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



Continue to the next section to setup your GitLab CI file for the CI/CD pipeline.