Additional Mission 6: Optimize your environment
Step 1: Adjust all your scripts to use Gitlab Environment variables
If you have successfully completed the previous missions, this next challenge will provide minimal guidance, allowing you to apply your knowledge independently. At this stage, you should have a solid understanding of the workflow and be able to navigate the tasks with confidence.
-
Navigate in your Gitlab Repository to Settings > CI/CD > Variables.
-
Search through all your scripts that are used in the pipeline (Ansible, pyATS).
-
Create all of the relevant variables in Gitlab.
-
Replace the hardcoded values in your scripts with a variable lookup, where
VARIABLE_NAME
equals theKey
in Gitlab:- python:
os.getenv("VARIABLE_NAME")
- ansible:
"{{ lookup('env', 'VARIABLE_NAME') }}"
- python:
-
When your done don't forget to commit and push your changes to your git repository.
Step 2: Adjust your custom-script to create sites
-
Open the Netbox custom-script
./netbox/pod3_site-hierarchy.py
and add the following to the end of the script. Replace the token with your trigger token.if commit: self.log_info("Commit is set. Triggering GitLab pipeline...") project = "9" url = f"http://198.18.134.23/api/v4/projects/{project}/ref/master/trigger/pipeline" token = "YOUR TOKEN HERE" # replace with your token params = { "token": token, "variables[SITE_PIPELINE]": "true" } try: response = requests.post(url, params=params) response.raise_for_status() if response.status_code == 201: self.log_success("Pipeline triggered successfully.") else: self.log_warning( f"Unexpected response status: {response.status_code}. Response: {response.text}" ) except requests.exceptions.RequestException as e: self.log_failure(f"Failed to trigger pipeline: {e}") else: self.log_info("Commit is not set. Skipping GitLab pipeline trigger.")
-
Commit and push your changes to your Git repository.
-
Navigate to Operations > Data Sources, click on your data source and then on Sync.
-
Create a new site by navigating to Customization > Scripts and run your custom script Create Site and Locations:
Site creation variables
Key Value Tenant pod3 Region Choose region depending on your site location Site Name Feel free to choose one Address Feel free to choose one (Street, City, Country) Numbers of Floors 1 Lowest Floor 1 Commit changes true -
Check the pipeline status in your Repository, by clicking on the running pipeline.
-
Make sure to check the dry run and if everything seems as expected, proceed by triggering the deploy stage.
-
As soon as the job successfully executed, check in Catalyst Center if the new site was created.
Example Pod-1: