FortiGate Terraform with GitLab CI/CD Pipeline

In a previous post, I have created a sample of FortiGate config with Terraform. This was all performed locally from a Windows machine. While this is good in a lab environment, the major problem with it is that the management doesn’t scale.

If there is more than a single network engineer, they need to share the same Terraform state file. If this is stored locally on a single machine, then it’s difficult to manage, and we do not want it to go out of sync. So this is where a tool such as GitLab and the use of managed Terraform States. I have previously talked about them in my AWS Terraform post.

In this post it is a little different as the GitLab runner needs to communicate with local resources and not internet ones. I have written about this in my ACI Automation post. But I will go over again, as that was performed on a Mac.

Lab

The lab is the same as the lab that has been used in my previous FortiGate Terraform post. A single FortiGate running version 7.2. There are two clients connected, but they serve no purpose in this lab.

Setup

Fortigate Repo in GitLab
Create new Runner in GitLab
Create new Docker Network
Install GitLab Runner (Ubuntu)
Create new Managed Terraform State
Test

Create new Runner in GitLab

The runner is created in the project under: Settings >> CI/CD >> Runners
From here, create a new runner

The new runner options will look similar to the screenshot below. I will be using Ubuntu, so Linux for me.

Once the runner has been created, you will be taken to a new page. That’s all for now. Keep this page open as the details are required. It will also confirm the runner has registered successfully once the message will appear below the Go to runners page button.

Create New Docker Network

As this will be running on a local network, the best way I found using Docker was to create a new Docker bridge network for GitLab. This will provide access to the local network.

Install and Configure GitLab Runner (Ubuntu)

Install the GitLab runner on Ubuntu.

To run and configure the runner, use the following command. Not that there are extra options.
Name for the runner:
Executor: Docker
Default Docker Image: alpine

The runner registration command creates the file /etc/gitlab-runner/config.toml. Inside this file are the settings.

This will now show that the runner has successfully registered in the GitLab page.

Create New Managed Terraform State

The managed Terraform state can now be created. Terraform knows where to look based off the bankend.tf file.

Inside the FortiGate repo is a file named .gitlab-ci.yml. This file is what will perform all the Terraform commands for the CI/CD pipeline. This is the default provided by GitLab, the template Terraform/Base.gitlab-ci.yml references the GitLab managed terraform state and will create this file automatically.

Inside my .gitlab-ci.yml file, I have included variables for the FortiGate token. These tokens can be found in GitLab under: Settings >> CICD >> Variables. For more details, I have created a post that walks through the process.

The Terraform state is created when the pipeline first runs. The pipeline will run when something is changed inside the repo by default.

To locate the Terraform state, navigate to: Operate >> Terraform states.
Currently, there is nothing in here. That will be populated in the next stage.

Test

The test step will be running the pipeline and seeing the Terraform state file created, and configuration apply to the FortiGate.

Make any changes as required. Push the changes back to GitLab. The pipeline will automatically run. If there are no changes to be made, the pipeline can be run manually.

To run the pipeline manually, navigate to: Build >> Pipelines >> Run Pipeline

The pipeline has run with the changes I made. It is pending manual intervention to deploy the configuration.

Going back to the Terraform state (Operate >> Terraform states), the state file will be present, named default.

Leave a Comment

Your email address will not be published. Required fields are marked *