online posts 16
ansible 101 for networking

task 8.3 · chapter 8 of 8: run it like production · 20 min

A CI job

objective

Add the pipeline jobs that lint and syntax-check every push.

you edit, in the lab.gitlab-ci.yml

open a file in VS Code: Ctrl+P (Cmd+P on a Mac), type its name, Enter

given
lintyamllint . and ansible-lint --offline
syntaxansible-playbook playbooks/site.yml --syntax-check
1 explain

What is a CI pipeline for an Ansible repo?

CI (continuous integration) runs checks automatically on every push to Git, so nobody has to remember to run them. For an Ansible repo, that means everything you can check without the network: YAML style, ansible-lint, and that every playbook parses.

GitLab reads its pipeline from .gitlab-ci.yml at the root of the repo. Its runners can’t reach the switches in your codespace, and that’s fine: CI’s job is to catch mistakes before anyone runs anything.

2 show

What are the parts of a .gitlab-ci.yml?

default:                       # settings every job shares
  image: python:3.13-slim      #   the container each job runs in
  before_script:               #   commands that run before each job's script
    - pip install -q -r requirements.txt

lint:                          # a JOB: any top-level name that has a script
  script:                      #   the commands; any one failing fails the job
    - yamllint .

Jobs run in parallel, each in a fresh container. A red job means the push broke something, and the merge request says which.

Two things in the file are already done for you. placeholder is a job that never runs: GitLab rejects a pipeline file with no jobs at all, so it keeps the file valid until you add yours. Leave it. And before_script writes .vault-pass from a CI/CD variable called VAULT_PASSWORD: chapter 7’s ansible.cfg expects that file, and it’s never committed. In your own GitLab project, set VAULT_PASSWORD as a masked variable (Settings → CI/CD → Variables). Lint and syntax checks never decrypt anything, so the lab’s pipeline works without it.

3 follow along

The lint job

  1. Add the job at the bottom of the file, at the left margin:

    edit.gitlab-ci.ymlCtrl+P → .gitlab-ci.yml
    lint:
      script:
        - yamllint .
        - ansible-lint --offline
    

    --offline stops ansible-lint from downloading anything: the collections are already installed by before_script.

  2. Run the same steps here: make ci. If it passes in the codespace, it passes in GitLab.

4 challenge

The syntax job

Add a second job, syntax, that checks playbooks/site.yml parses, without running it. Then make ci again. If you have a GitLab fork of the lab, push and watch both jobs run.

hint

ansible-playbook has a flag that parses a playbook, resolves its roles and stops: --syntax-check.

show the solution.gitlab-ci.yml

or pull it into your lab: make solve T=8.3, then git diff --staged shows what changed

syntax:
  script:
    - ansible-playbook playbooks/site.yml --syntax-check

5 check: run it, then prove it

$ make ci
$ make check T=8.3

when it's right, you'll see

╭─ task 8.3 · a CI job
│  ✓ .gitlab-ci.yml runs ansible-lint
│  ✓ and a syntax check of playbooks/site.yml
│  ✓ and yamllint
╰─ 3/3 checks passed  verified