Mastering Ansible lint

Writing easy to maintain Ansible roles and playbooks is more of secret art that will take years to master. Product documentation is useful but is far from complete and lacks lots of hints.

Avoiding false positives

You can use noqa comments to disable specific rules. Be careful not to put them on multi-line YAML blocks as they will not be loaded as comments.

    - name: some ansible task
      # noqa 305 <-- correct
      shell: |
        env  # 305 <-- not correct

If the false positives are too many or if you are just in the process of adopting the linter, narrow down its scope:

# .ansible-lint
    - .travis.yml  # be careful not to exclude too much
    - 305  # Disable rule, include link to bug that forced you to disable it

Accessing Ansible modules from other repos

In the unlikely case where you would end up with ansible-lint errors caused by the fact that it fails to find some Ansible modules which may not even be located inside your own repository, I provide this hack for you which assures is able to find zuul_return module which is part of zuul Python package.

  - repo:
    rev: v4.1.1a0
      - id: ansible-lint
        files: \.(yaml|yml)$
        # Helps it find zuul_return module on both zuul and on dev environments,
        # Based on
        entry: >
          bash -c 'env ANSIBLE_LIBRARY=`python -c "import os, zuul;
          ansible-lint --force-color -v "$@"'
        exclude: playbooks/legacy
          - zuul