Run Ansible Lint Inside Docker Container on Linux

Run Ansible Lint Inside Docker Container on Linux

Ansible Lint is a command-line utility used to review Ansible playbooks for best practices, syntax problems, and potential misconfigurations. It helps maintain consistency and improves playbook quality by enforcing recommended standards and catching common mistakes early. Using Ansible Lint inside a Docker container provides a predictable environment with preinstalled dependencies. This approach avoids local setup differences, keeps the tool version consistent, and simplifies execution across different machines.

Create directory for testing and navigate to it:

mkdir ansible && cd ansible

Create a simple playbook file named playbook.yaml:

- name: Install nginx
  hosts: web
  become: true

  tasks:
    - name: Install package
      apt:
        name: nginx
        state: present

To validate the playbooks in the current directory using Ansible Lint from a Docker container, execute the following command:

docker run -it --rm -v ./:/code pipelinecomponents/ansible-lint ansible-lint

Explanation of the command:

  • docker run - starts a new container instance.
  • -it - enables interactive mode with a terminal session.
  • --rm - removes the container automatically after execution completes.
  • -v ./:/code - mounts the current working directory into /code inside the container so files become accessible.
  • pipelinecomponents/ansible-lint - Docker image containing Ansible Lint.
  • ansible-lint - command executed inside the container to analyze playbooks.

After execution, Ansible Lint reports findings similar to the following output:

WARNING  Listing 1 violation(s) that are fatal
fqcn[action-core]: Use FQCN for builtin module actions (apt).
playbook.yaml:7:7 Use `ansible.builtin.apt` or `ansible.legacy.apt` instead.

Read documentation for instructions on how to ignore specific rule violations.

# Rule Violation Summary

  1 fqcn profile:production tags:formatting

Failed: 1 failure(s), 0 warning(s) on 1 files. Last profile that met the validation criteria was 'shared'. Rating: 4/5 star

The report indicates that the apt module usage does not follow Fully Qualified Collection Name (FQCN) conventions. Updating the task to use ansible.builtin.apt aligns the playbook with modern Ansible standards and resolves the violation.

Leave a Comment

Cancel reply

Your email address will not be published.