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
oransible
you can specify the following for the first before_script execution:pip install ansible-lint==4.2.0 ansible==2.9.10
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
.
[](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
.
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
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
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