Skip to content

Instantly share code, notes, and snippets.

@liveaverage
Last active October 8, 2020 19:09
Show Gist options
  • Save liveaverage/854fb3eebcc98be8264ad9543dd9e073 to your computer and use it in GitHub Desktop.
Save liveaverage/854fb3eebcc98be8264ad9543dd9e073 to your computer and use it in GitHub Desktop.
GitLab CI/CD Pipeline

GitLab CI/CD Pipeline Configuration

Save the following contents to .gitlab-ci.yml within your new or existing Ansible role project/repository:

image: python:3-slim

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

stages:
  - ansible-lint

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

If you'd like to speed-up execution time, at the expense of needing to periodically update the ansible-lint python package, you could optionally build a static container image with ansible-lint pre-installed.

Note: if you need to check multiple roles, playbook files, etc. from a single repository, you may need to modify the script parameter above: ansible-lint . playbook1.yml playbook2.yml.

If you need to lock-in a specific version of ansible-lint or ansible you can specify the following for the first before_script execution: pip install ansible-lint==4.2.0 ansible==2.9.10

GitLab CI/CD Badges

Once your pipeline has been defined for your Ansible role project, you can advertise a pipeline status badge by adding the following to your repo README.md.

[![pipeline status](https://gitlab.int.shifti.us/liveaverage/ansible-role-infra-vmware/badges/master/pipeline.svg)](https://gitlab.int.shifti.us/liveaverage/ansible-role-infra-vmware/commits/master)

You could optionally add badges for each branch of your project, or if you'd like a single badge relative to the current working branch you could add a supplemental stage to your .gitlab-ci.yml configuration to dynamically update README.md based on CI variable CI_COMMIT_BRANCH.

Customizing ansible-lint

To further customize ansible-lint tests, output, etc. you can define an .ansible-lint configuration file to include/skip tags, increase verbosity, and more:

parseable: true
quiet: false
skip_list:
  - provision
use_default_rules: true
verbosity: 1

Skipping Rules

You can skip entire lint rules (not recommended) by modifying your .ansible-lint configuration file skip_list, or you could optionally specify # noqa <RULE_NUMBER> on tasks that may throw errors when executing ansible-lint:

  - name: Create user
    shell: |
      oc create user {{ requestor }} --full-name="{{ requestor_name | default(requestor) }}" || true
    delegate_to: "{{ oc_master }}" # noqa 301
    ignore_errors: true

GitHub Actions & Ansible Lint

Colin McCarthy has a great write-up if you're looking to implement similar automated linting for Ansible automation on GitHub: https://www.ansible.com/blog/ansible-linting-with-github-actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment