Using GitLab CI to deploy Ansible playbooks reliably

Automating Ansible playbook deployment with GitLab CI creates a repeatable path from a committed change to a controlled infrastructure update. Instead of running commands from a laptop or a jump host, the pipeline can validate code, select the correct inventory, retrieve protected credentials, and record the result for later review.

This model suits teams managing VMware clusters, Linux services, network appliances, and cloud resources. It also works well for a home lab that has grown beyond a few manual commands. A Git repository becomes the source of truth, while GitLab runners provide the execution environment.

For Australian organisations, auditability is often important because infrastructure teams support distributed offices, government contracts, healthcare providers, mining operations, and managed service customers. A pipeline can provide useful evidence when systems are hosted between Sydney, Melbourne, Perth, and regional sites.

The goal is not to make every change fully automatic on day one. A sensible implementation starts with linting and dry runs, then adds approvals and production deployment after the workflow has proved reliable. This keeps the process practical for a small Brisbane IT team as well as a larger enterprise operation.

Build a repository that pipelines can understand

A clear repository layout reduces ambiguity for both engineers and GitLab runners. Keep playbooks, roles, inventories, variables, and pipeline configuration in predictable locations. For example, site.yml might orchestrate roles, while inventories/dev and inventories/prod contain environment-specific host definitions.

Sensitive values should never be committed to Git. Store Vault passwords, SSH keys, API tokens, and VMware credentials in GitLab CI/CD variables or an external secrets manager. Mark variables as masked and protected, and restrict production deployment jobs to protected branches or tags.

Pinning versions is equally important. Use a known Ansible version, define collections in requirements.yml, and install them during the job. A runner using Ansible 2.15 today and a different release next month can produce confusing results, particularly when modules or collection behaviour changes.

Design stages for safe change delivery

A useful pipeline normally separates syntax checks, quality checks, planning, and deployment. ansible-lint catches risky patterns, while ansible-playbook --syntax-check confirms that the playbook can be parsed. A check-mode job can reveal many unintended changes before a real run.

The deployment stage should use an explicit inventory and extra variables rather than relying on defaults hidden inside the runner. For production, configure a manual GitLab job with an approval rule. Development environments can deploy automatically after a successful merge, while production remains deliberate.

A simplified .gitlab-ci.yml might look like this:

stages:
  - validate
  - plan
  - deploy

image: python:3.12-slim

before_script:
  - pip install ansible ansible-lint
  - ansible-galaxy collection install -r requirements.yml

validate:
  stage: validate
  script:
    - ansible-lint
    - ansible-playbook site.yml -i inventories/dev --syntax-check

plan:
  stage: plan
  script:
    - ansible-playbook site.yml -i inventories/prod --check --diff
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

deploy:
  stage: deploy
  script:
    - ansible-playbook site.yml -i inventories/prod
  when: manual
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'

The exact commands depend on the environment. A VMware-focused team may need community.vmware, while a Linux fleet might require collections for firewall, database, or cloud modules. Documentation from the VMware series can help connect infrastructure concepts with practical automation patterns.

Choose runners and credentials carefully

GitLab runners can be shared, group-specific, or dedicated to a project. A shell runner is easy to use but exposes jobs to the host operating system. Docker or Kubernetes runners provide stronger isolation, although they require more operational planning. A dedicated runner inside the management network may be appropriate when vCenter or internal servers are not publicly reachable.

Network placement matters for Australian businesses with private WANs and regional facilities. A runner in Sydney may have acceptable access to a Melbourne vCenter but poor connectivity to a Perth site. If the deployment targets several locations, consider local runners, controlled routing, and sensible Ansible timeout values rather than assuming a single path will always be stable.

SSH host key checking should remain enabled in production. Maintain trusted fingerprints through a managed known_hosts file or a secure bootstrap process. For VMware automation, use service accounts with the minimum required privileges and avoid embedding passwords in command lines, job logs, or inventory files.

Requirement Practical GitLab CI approach Operational benefit
Code validation ansible-lint and syntax checks Finds errors before deployment
Environment separation Distinct inventories and protected variables Reduces accidental production changes
Secret handling Masked GitLab variables or a vault Keeps credentials out of Git and logs
Production control Manual job with approvals Adds a deliberate change gate
Repeatable execution Pinned image and collection versions Limits runner drift
Audit trail Merge requests and job artefacts Shows who changed and deployed code

Add testing, logging, and rollback thinking

Check mode is valuable, though it cannot predict every side effect. Some modules do not support complete check-mode behaviour, and a service restart or API operation may need a dedicated test. Where possible, use disposable virtual machines or a lab vCenter to test role changes before they reach production.

Capture useful artefacts from each job, such as rendered reports, lint output, and deployment summaries. Avoid publishing files that contain secrets or sensitive host information. GitLab job logs should make it clear which commit, inventory, and playbook were used.

Rollback is usually a playbook design concern rather than a GitLab feature. Version configuration files, keep previous application packages available, and write reversal tasks where practical. For an infrastructure change, reverting the Git commit may restore the desired definition, but it does not automatically reverse every change already made to a host.

Australian teams often work across AEST, ACST, and AWST. Schedule disruptive jobs with the actual site time zone in mind, especially for retail, education, mining, and 24-hour operations. A deployment labelled “after hours” in Sydney may still occur during a busy shift in Perth.

Improve the workflow without losing control

Once the basic pipeline works, add merge request checks, reusable CI templates, environment approvals, and notifications to Microsoft Teams or email. Use tags for release points and retain job artefacts long enough to support incident investigations and change-management requirements.

For a home lab, the same pattern can manage ESXi hosts, virtual machines, DNS, monitoring, and GitLab itself. Free infrastructure resources and automation utilities can complement this approach; the free tools collection is a useful place to find supporting resources for a practical lab workflow.

Start with one non-production playbook, a dedicated runner, and a protected variable set. Commit the pipeline with the automation code, require a review, and let the first deployment produce evidence rather than aiming for maximum speed. Once validation and approvals are dependable, expand the pattern across your Ansible estate and make every infrastructure change easier to trust.