Sample CI configuration files to run ansible-lint against an Ansible role or playbook (GitLab CI, Travis CI, CircleCI, GitHub Action)

Recently, I have been publishing some Ansible roles on GitHub, so I have been thinking about what kind of CI to set up.

The most famous tool for this task is Molecule (that’s what we use at work). It can run entire test playbooks to make sure they work exactly as expected.

I really don’t have the time to do that, and my roles are not critical for production, so I settled for a simple linting pipeline. I am using the also well-know ansible-lint which will validate the YAML files and enforce Ansible best practices.

Since it’s as simple as running ansible-lint . in your Ansible role directory, I will share below some simple pipeline configurations for the most famous CI/CD tools.

It requires not configuration, you’ll just need to add the repository to the CI service as usual, and then commit the correct configuration file!

I am currently using CircleCI for this specific pipeline. The tree services work identically, but CircleCI is the fastest to trigger and run the pipeline.

Note

Update: I switched to GitHub actions now because it’s easier to not rely on a third party service. Also all my Circle builds started to fail mysteriously…

GitLab CI

.gitlab-ci.yml:

image: python:3-slim

before_script:
  - pip install ansible ansible-lint
  - ansible-lint --version

stages:
  - ansible-lint

ansible-lint:
  stage: ansible-lint
  script:
    - ansible-lint .

CircleCI

.circle-ci/config.yml:

version: 2
jobs:
  build:
    docker:
      - image: python:3-slim
    steps:
      - checkout
      - run: pip install ansible ansible-lint
      - run: ansible-lint --version
      - run: ansible-lint .

Travis CI

.travis.yml:

language: python
install:
  - pip install ansible ansible-lint
  - ansible-lint --version
script:
  - ansible-lint .

GitHub Action

.github/workflows/push.yml:

name: ansible-lint

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    name: ansible-lint
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-python@v2
      - run: pip install ansible ansible-lint
      - run: ansible-lint --version
      - run: ansible-lint .