In lab 5 we're going to build ontop of our lab 4. Lab 4 only dealt with source code checks. We're now going to start building and testing our CICD pipeline that can actually execute some network automation!
The first step in this lab is to checkout the Lab 5 branch from our forked repository.
- Ensure you're in the correct GitLab forked repository directory. (./ac2_cicd_workshop)
cd ac2-cicd-workshop/ac2_cicd_workshop/
- Switch into the Lab 5 Branch
git switch Lab_5_Generate_Configs
In Lab 4 we started our Containerlab topology. Quickly make sure that is still up and the Mgmt IPs havent changed.
@jeffkala ➜ /workspaces/autocon2-cicd-workshop-dev/clab (jkala-work) $ sudo containerlab inspect
INFO[0000] Parsing & checking topology file: ceos-lab.clab.yml
+---+---------+--------------+--------------+------+---------+---------------+--------------+
| # | Name | Container ID | Image | Kind | State | IPv4 Address | IPv6 Address |
+---+---------+--------------+--------------+------+---------+---------------+--------------+
| 1 | ceos-01 | 2591c2120d60 | ceos:4.32.0F | ceos | running | 172.17.0.6/16 | N/A |
| 2 | ceos-02 | 43f7a0c03e49 | ceos:4.32.0F | ceos | running | 172.17.0.3/16 | N/A |
| 3 | ceos-03 | 97460114f25c | ceos:4.32.0F | ceos | running | 172.17.0.5/16 | N/A |
| 4 | ceos-04 | 9e4f783304fc | ceos:4.32.0F | ceos | running | 172.17.0.4/16 | N/A |
+---+---------+--------------+--------------+------+---------+---------------+--------------+
Lab 5 introduces a few elements. We will start using the source code in the repository to generate configuration utilizing the IaC concepts in the repository.
The high-level steps are listed below:
- A click app is packaged in the
cli.py
file. It has multiple command line triggers, but we will focus on thegenerate-config
function.
[!INFO] This is not a python course; therefore, the code in
cli.py
won't be directly explained.
-
The generate-config function uses Nornir and two tasks exposed by public libraries.
template_file
from nornir_jinja2.write_file
from nornir_utils
-
Upon the template_file tasks being executed it find the entrypoint template called
eos.j2
and renders the Jinja2 files that build the configuration.- Data needed to render the templates is stored in the native Nornir inventory files, mainly in
hosts.yml
.
- Data needed to render the templates is stored in the native Nornir inventory files, mainly in
[!INFO] The configuration change we're introducing in this lab (few steps from now) will simply require you to uncomment out some of the data attributes in the
host.yml
file. This will allow us to save some time in the workshop delivery.
- When you open the
.gitlab-ci.yml
file you will notice a new stage, and a new includes file has been added.
First, we will look at the stages:
section which has two added stages.
stages: # List of stages for jobs, and their order of execution
- "lab-4-lint-and-format"
- "lab-4-pytest"
- "lab-5-generate"
- "lab-5-diff"
Second, we see the includes:
now has a new GitLab ci file to include.
include:
- local: ".gitlab/ci/lab-4-includes.gitlab-ci.yml"
- local: ".gitlab/ci/lab-5-includes.gitlab-ci.yml"
- Lab 5 includes file has two jobs.
---
generate-config-job:
stage: "lab-5-generate"
script:
- "echo 'Generating configuration files..'"
- "poetry run python ac2_cicd_workshop/cli.py generate-config --inventory-dir ac2_cicd_workshop/inventory"
diff-config-job:
stage: "lab-5-diff"
allow_failure: true
script:
- "echo 'Diff configuration files..'"
- "diff -y -r ac2_cicd_workshop/output/configs/ topologies/network-lab/startup-configs/"
You can see here, we're simply reusing our click app that we packaged with the application. We're running the click app and passing in our inventory source. This will run the Nornir tasks to generate and save the new configuration files, by default saving them in the GitLab repo directory output
, and naming the files <hostname>.conf
.
Secondly, we run an additional job that simply does a diff of the new configs we generated compared to startup configs. Notice we allow_failures
on this job since its simply a documentation stage.
Our configuration change to demonstate our CICD pipeline will be a simple OSPF change. The containerlab topology remains the same, we're simply adding Lo100 as an OSPF area and then validating our changes.
[!INFO] As this is not a Network Engineering workshop, we're keeping it simple.
The diagram below explains more about the topology and the goals.
Now that Lab 5 has been explained, lets quickly update our Nornir inventory hosts file and uncomment out a few new attributes that will render new configurations to accomplish the goals explained in the diagram!
- We don't want to have to update the
host.yaml
file for Nornir again because we switched branches. Lets just checkout the single file from Lab_4 that is already accurate.
git checkout Lab_4_Source_Code_Checks ac2_cicd_workshop/inventory/hosts.yml
- For each host uncomment out the two sections.
First uncomment out the area
definition under Lo100
.
Loopback100:
description: "Mimic OSPF Area 0.0.0.4 loopback"
ip: "192.168.104.100/24"
# ospf:
# area: "0.0.0.4"
Loopback100:
description: "Mimic OSPF Area 0.0.0.4 loopback"
ip: "192.168.104.100/24"
ospf:
area: "0.0.0.4"
Secondly, uncomment out the router ospf stub area configuration.
data:
ospf:
process_id: 1
router_id: "10.0.0.4"
# stub_areas:
# - "0.0.0.4"
data:
ospf:
process_id: 1
router_id: "10.0.0.4"
stub_areas:
- "0.0.0.4"
[!INFO] Double check this was done for ALL four host.
- Commit and Push your code up!
git add -A;git commit -m "lab5 updates";git push -u origin Lab_5_Generate_Configs
- Go into your GitLab UI and navigate to the forked project.
- Navigate to Builds from the side menu and click on Pipelines.
- Watch your Pipeline run!!