Infrastructure as code

Declare OpenShift projects with OpenTofu or Terraform, configure hosts with Ansible, and apply both from a pipeline.

Updated CompTech Team

Clusters drift when every project, quota and host package is created by hand. Infrastructure as code puts that work in Git. This lab uses OpenTofu for the objects in the cluster and Ansible for the hosts underneath it. Terraform reads the same files if you already standardise on it. The commands below use tofu.

The pipeline plans on a merge request and applies on main. That is the same review you already use for application manifests in OpenShift GitOps. GitOps remains the better tool for Deployments and Routes. OpenTofu is the better tool for projects, quotas and cloud or hypervisor resources that exist before a namespace does.

Prerequisites

  • tofu 1.8 or later, and ansible-core, on a bastion or in a CI image
  • A kubeconfig with permission to create projects and resource quotas
  • An S3-compatible bucket for state. OpenShift Data Foundation is what we use in the lab
  • SSH to the hosts Ansible will manage

Projects and quotas with OpenTofu

main.tf declares the dev project and a quota. State goes to object storage so two people cannot apply at once.

terraform {
  required_version = ">= 1.8.0"
  required_providers {
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.35"
    }
  }
  backend "s3" {
    bucket = "tofu-state"
    key    = "lab/payments.tfstate"
    region = "us-east-1"
    endpoints = { s3 = "https://s3.openshift-storage.svc" }
    skip_credentials_validation = true
    skip_requesting_account_id = true
    skip_metadata_api_check     = true
    use_path_style              = true
  }
}

provider "kubernetes" {
  config_path = "~/.kube/config"
}

resource "kubernetes_namespace" "payments_dev" {
  metadata {
    name = "payments-dev"
    labels = { "app.kubernetes.io/part-of" = "payments" }
  }
}

resource "kubernetes_resource_quota" "payments_dev" {
  metadata {
    name      = "compute"
    namespace = kubernetes_namespace.payments_dev.metadata[0].name
  }
  spec {
    hard = {
      "requests.cpu"    = "4"
      "requests.memory" = "8Gi"
      "pods"            = "20"
    }
  }
}
tofu init
tofu plan -out=plan.bin
tofu apply plan.bin
oc get resourcequota compute -n payments-dev

Review tofu plan in the merge request. Apply only the saved plan, so what you apply is what was reviewed. Encrypt the state bucket. State contains resource addresses and any data the provider stored, and it is not the place for a kubeconfig pasted into a variable default.

A module per environment (dev, prod) with the same variables and different numbers keeps production from inheriting a lab quota. Remote state locking needs a lock table the backend supports. With the S3 backend, turn locking on so a second apply waits.

Hosts with Ansible

Ansible configures machines: packages, users, sysctls, the container tools a GitLab runner host needs. It is a poor way to express a Deployment, and a good way to express "this RHEL host has podman and Buildah".

---
- name: Prepare runner hosts
  hosts: runners
  become: true
  tasks:
    - name: Install container tools
      ansible.builtin.dnf:
        name: [podman, buildah, skopeo]
        state: present
    - name: Create the runner user
      ansible.builtin.user:
        name: gitlab-runner
        groups: wheel
        append: true
        state: present
    - name: Allow the runner user to use sudo without a password
      ansible.builtin.copy:
        dest: /etc/sudoers.d/gitlab-runner
        mode: "0440"
        content: "gitlab-runner ALL=(ALL) NOPASSWD: ALL\n"
ansible-playbook -i inventory/lab.yaml playbooks/runners.yaml --check
ansible-playbook -i inventory/lab.yaml playbooks/runners.yaml

--check is the plan. Inventory lives in Git too, without passwords. Use SSH keys or an Ansible Vault password that the pipeline holds as a secret.

How the two tools split the work

Question Tool
A project, a quota, a cloud network, a VM OpenTofu or Terraform
Packages, users and files on a host you already have Ansible
Deployments, Services, Routes, application config OpenShift GitOps
Build and test the application GitLab CI or Pipelines

Put tofu plan on the merge request and tofu apply plan.bin on main, in the same runner you built in the GitLab lab. The apply job should be the only identity with permission to change those objects.

What's next

  1. The runner that executes tofu apply is the one from GitLab CI.
  2. Application manifests stay with OpenShift GitOps, not in this Terraform state.
  3. A portal on top of these templates is Developer Hub.