Skip to content

ansible-lint

ansible-lint documentation

ansible-lint - GitHub

Configuration in MegaLinter

Variable Description Default value
ANSIBLE_ANSIBLE_LINT_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
ANSIBLE_ANSIBLE_LINT_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src\|lib)
Include every file
ANSIBLE_ANSIBLE_LINT_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test\|examples)
Exclude no file
ANSIBLE_ANSIBLE_LINT_CLI_LINT_MODE Override default CLI lint mode
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
{linter.cli_lint_mode}
ANSIBLE_ANSIBLE_LINT_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".yml", ".yaml"]
ANSIBLE_ANSIBLE_LINT_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
ANSIBLE_ANSIBLE_LINT_PRE_COMMANDS List of bash commands to run before the linter None
ANSIBLE_ANSIBLE_LINT_POST_COMMANDS List of bash commands to run after the linter None
ANSIBLE_ANSIBLE_LINT_CONFIG_FILE ansible-lint configuration file name
Use LINTER_DEFAULT to let the linter find it
.ansible-lint.yml
ANSIBLE_ANSIBLE_LINT_RULES_PATH Path where to find linter configuration file Workspace folder, then MegaLinter default rules
ANSIBLE_ANSIBLE_LINT_DISABLE_ERRORS Run linter but consider errors as warnings false
ANSIBLE_ANSIBLE_LINT_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0
ANSIBLE_DIRECTORY Directory containing ANSIBLE files ansible

MegaLinter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 95 Docker Image Size (tag) Docker Pulls
dart Optimized for DART based projects 41 Docker Image Size (tag) Docker Pulls
documentation MegaLinter for documentation projects 40 Docker Image Size (tag) Docker Pulls
dotnet Optimized for C, C++, C# or VB based projects 47 Docker Image Size (tag) Docker Pulls
go Optimized for GO based projects 42 Docker Image Size (tag) Docker Pulls
java Optimized for JAVA based projects 42 Docker Image Size (tag) Docker Pulls
javascript Optimized for JAVASCRIPT or TYPESCRIPT based projects 49 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 44 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 49 Docker Image Size (tag) Docker Pulls
ruby Optimized for RUBY based projects 41 Docker Image Size (tag) Docker Pulls
rust Optimized for RUST based projects 41 Docker Image Size (tag) Docker Pulls
salesforce Optimized for Salesforce based projects 43 Docker Image Size (tag) Docker Pulls
scala Optimized for SCALA based projects 41 Docker Image Size (tag) Docker Pulls
swift Optimized for SWIFT based projects 41 Docker Image Size (tag) Docker Pulls
terraform Optimized for TERRAFORM based projects 46 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • Activated only if sub-directory ansible is found. (directory name can be overridden with ANSIBLE_DIRECTORY)
  • File extensions: .yml, .yaml
  • File name do not ends with: vault.yml, vault.yaml, galaxy.yml, galaxy.yaml

How the linting is performed

  • ansible-lint is called one time by identified file

Example calls

ansible-lint -v myfile.yml
ansible-lint -v -c .ansible-lint.yml myfile.yml

Help content

usage: ansible-lint [-h] [-L] [-f {rich,plain,rst}] [-q] [-p]
                    [--parseable-severity] [--progressive] [-r RULESDIR] [-R]
                    [--show-relpath] [-t TAGS] [-T] [-v] [-x SKIP_LIST]
                    [-w WARN_LIST] [--nocolor] [--force-color]
                    [--exclude EXCLUDE_PATHS] [-c CONFIG_FILE] [--version]
                    [playbook [playbook ...]]

positional arguments:
  playbook              One or more files or paths. When missing it will
                        enable auto-detection mode.

optional arguments:
  -h, --help            show this help message and exit
  -L                    list all the rules
  -f {rich,plain,rst}   Format used rules output, (default: rich)
  -q                    quieter, although not silent output
  -p                    parseable output in the format of pep8
  --parseable-severity  parseable output including severity of rule
  --progressive         Return success if it detects a reduction in number of
                        violations compared with previous git commit. This
                        feature works only in git repositories.
  -r RULESDIR           Specify custom rule directories. Add -R to keep using
                        embedded rules from /usr/lib/python3.8/site-
                        packages/ansiblelint/rules
  -R                    Keep default rules when using -r
  --show-relpath        Display path relative to CWD
  -t TAGS               only check rules whose id/tags match these values
  -T                    list all the tags
  -v                    Increase verbosity level
  -x SKIP_LIST          only check rules whose id/tags do not match these
                        values
  -w WARN_LIST          only warn about these rules, unless overridden in
                        config file defaults to 'experimental'
  --nocolor             disable colored output
  --force-color         Try force colored output (relying on ansible's code)
  --exclude EXCLUDE_PATHS
                        path to directories or files to skip. This option is
                        repeatable.
  -c CONFIG_FILE        Specify configuration file to use. Defaults to
                        ".ansible-lint"
  --version             show program's version number and exit

Installation on mega-linter Docker image

Example success log

Results of ansible-lint linter (version 4.2.0)
See documentation on https://megalinter.github.io/descriptors/ansible_ansible_lint/
-----------------------------------------------

[SUCCESS] .automation/test/ansible/ansible/ansible_good_1.yml
    Examining .automation/test/ansible/ansible/ansible_good_1.yml of type playbook
    Examining .automation/test/ansible/ansible/ghe-initialize/tasks/ghe-api-config-apply.yml of type tasks
    Examining .automation/test/ansible/ansible/ghe-initialize/tasks/ghe-config-apply.yml of type tasks
    Examining .automation/test/ansible/ansible/ghe-initialize/tasks/collectd-settings.yml of type tasks
    Examining .automation/test/ansible/ansible/ghe-initialize/tasks/ghe-ldap-configuration.yml of type tasks
    Examining .automation/test/ansible/ansible/ghe-initialize/tasks/splunk-settings.yml of type tasks
    Examining .automation/test/ansible/ansible/ghe-initialize/tasks/main.yml of type tasks
    Examining .automation/test/ansible/ansible/ghe-initialize/tasks/ghe-initial-configuration.yml of type tasks
    Examining .automation/test/ansible/ansible/ghe-initialize/handlers/main.yml of type handlers

Example error log

Results of ansible-lint linter (version 4.2.0)
See documentation on https://megalinter.github.io/descriptors/ansible_ansible_lint/
-----------------------------------------------

[ERROR] .automation/test/ansible/ansible/ansible_bad_1.yml
    Traceback (most recent call last):
      File "/usr/bin/ansible-lint", line 11, in <module>
        load_entry_point('ansible-lint==4.2.0', 'console_scripts', 'ansible-lint')()
      File "/usr/lib/python3.8/site-packages/ansiblelint/__main__.py", line 187, in main
        matches.extend(runner.run())
      File "/usr/lib/python3.8/site-packages/ansiblelint/__init__.py", line 267, in run
        for child in ansiblelint.utils.find_children(arg, self.playbook_dir):
      File "/usr/lib/python3.8/site-packages/ansiblelint/utils.py", line 163, in find_children
        for child in play_children(basedir, item, playbook[1], playbook_dir):
      File "/usr/lib/python3.8/site-packages/ansiblelint/utils.py", line 215, in play_children
        return delegate_map[k](basedir, k, v, parent_type)
      File "/usr/lib/python3.8/site-packages/ansiblelint/utils.py", line 246, in _taskshandlers_children
        results.extend(_roles_children(basedir, k, [th['action'].get('name')],
      File "/usr/lib/python3.8/site-packages/ansiblelint/utils.py", line 285, in _roles_children
        results.extend(_look_for_role_files(basedir, role, main=main))
      File "/usr/lib/python3.8/site-packages/ansiblelint/utils.py", line 330, in _look_for_role_files
        role_path = _rolepath(basedir, role)
      File "/usr/lib/python3.8/site-packages/ansiblelint/utils.py", line 299, in _rolepath
        path_dwim(basedir, os.path.join('roles', role)),
      File "/usr/lib/python3.8/posixpath.py", line 90, in join
        genericpath._check_arg_types('join', a, *p)
      File "/usr/lib/python3.8/genericpath.py", line 152, in _check_arg_types
        raise TypeError(f'{funcname}() argument must be str, bytes, or '
    TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'