# Test, lint and validate Ansible roles in CI

Source: https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci
Language: en
Published: 2026-10-13
Updated: 2026-11-25

How syntax checks, ansible-lint, check mode and simple CI stages prevent role issues from appearing first on production systems.

## Why role quality needs its own step

Ansible is quick to use, which is also a risk. A playbook can look syntactically valid while still being hard to maintain, non-idempotent or unsuitable for CI. A generic Ansible series therefore needs a dedicated quality step.

This article deliberately avoids platform-specific topics. It describes checks that apply equally to Linux baselines, update playbooks, web server roles and later specialist roles.

## Minimal validation path

- Keep YAML and repository structure consistent.
- Install collections reproducibly.
- Validate playbooks with --syntax-check.
- Use ansible-lint as a quality gate.
- Run check mode and diff mode against lab inventories.
- Build CI so failures are visible before merge.

## Project files for CI

```text
ansible-platform/
  requirements.yaml
  .ansible-lint
  inventories/lab/hosts.yaml
  playbooks/linux-baseline.yaml
  roles/linux_baseline/
```

## Install collections reproducibly

```yaml
# requirements.yaml
collections:
  - name: ansible.posix
  - name: community.general
```

```bash
ansible-galaxy collection install -r requirements.yaml
```

## Configure ansible-lint

```yaml
# .ansible-lint
profile: production
exclude_paths:
  - .cache/
  - tmp/
warn_list:
  - experimental
```

## Local validation commands

```bash
ansible-playbook -i inventories/lab/hosts.yaml playbooks/linux-baseline.yaml --syntax-check
ansible-lint playbooks/linux-baseline.yaml roles/linux_baseline
ansible-playbook -i inventories/lab/hosts.yaml playbooks/linux-baseline.yaml --check --diff
```

## Simple CI pipeline

```yaml
stages:
  - validate

ansible_validate:
  stage: validate
  image: python:3.13
  before_script:
    - python -m pip install --upgrade pip
    - python -m pip install ansible-core ansible-lint
    - ansible-galaxy collection install -r requirements.yaml
  script:
    - ansible-playbook -i inventories/lab/hosts.yaml playbooks/linux-baseline.yaml --syntax-check
    - ansible-lint playbooks/linux-baseline.yaml roles/linux_baseline
```

## What CI does not prove

Linting and syntax checks do not prove that a playbook is operationally correct. They catch common issues early. Real acceptance still needs a lab, check mode, real runs and an idempotence check.

## Acceptance table

```markdown
| Area | Check | Result |
| --- | --- | --- |
| Dependencies | requirements.yaml installs successfully | PASS |
| Syntax | ansible-playbook --syntax-check passes | PASS |
| Linting | ansible-lint reports no blockers | PASS |
| Simulation | check/diff run is understandable | PASS |
| Idempotence | second run shows no unexpected changes | PARTIAL |
| CI | pipeline fails on syntax or lint errors | PASS |
```

## What comes next

The next part can show how Ansible repositories are versioned, released and changed traceably. That is the bridge between individual playbooks and durable operations.

## Sources and further documentation

The examples follow the official Ansible Lint documentation and the Ansible playbook CLI documentation.

## Hero image

[![Ansible logo.](https://forgeone.solutions/media/Ansible_Logo.webp)](https://forgeone.solutions/media/Ansible_Logo.webp)

## Author

Mathias Rumbold

## Category

- [Ansible & Automation](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci?category=ansible-automation)
- [Enterprise Linux](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci?category=enterprise-linux)

## Tags

- [Ansible](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci?tags=ansible)
- [Ansible Roles](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci?tags=ansible-roles)
- [Automation](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci?tags=automation)
- [Linux](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci?tags=linux)
- [Linux Management](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci?tags=linux-management)
- [Enterprise Linux](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci?tags=enterprise-linux)

## Ansible automation step by step

This series deliberately stays generic: Ansible fundamentals, repository structure, roles, Vault, Linux baselines, updates, quality checks, CI and AWX. Domain topics such as IdM, FreeIPA, Proxmox or SUSE Multi-Linux Manager are covered in their own specialist series.

1. [Install Ansible and create your first playbooks](https://forgeone.solutions/en/tech/install-ansible-and-create-your-first-playbooks)
2. [Ansible inventories, roles and Vault in practice](https://forgeone.solutions/en/tech/ansible-inventories-roles-vault-manage-linux-systems-reproducibly)
3. [Roll out a Linux baseline with Ansible](https://forgeone.solutions/en/tech/ansible-linux-baseline-packages-services-ssh-acceptance)
4. [Orchestrate updates, reboots and maintenance windows with Ansible](https://forgeone.solutions/en/tech/ansible-orchestrate-updates-reboots-maintenance-windows)
5. [Test, lint and validate Ansible roles in CI (current part)](https://forgeone.solutions/en/tech/ansible-test-lint-validate-roles-ci)
6. [Version, release and change Ansible repositories traceably](https://forgeone.solutions/en/tech/ansible-version-release-change-repositories-traceably)
7. [Position AWX for repeatable execution, credentials and job templates](https://forgeone.solutions/en/tech/awx-ansible-jobs-credentials-job-templates)

## Further content

- [Ansible](https://forgeone.solutions/en/products/ansible)
