Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

How Ansible lint improves playbook debugging

August 28, 2023
Tathagata Paul Himanshu Yadav
Related topics:
Automation and managementLinuxSecure Coding
Related products:
Red Hat Ansible Automation Platform

Share:

    Ansible lint is a command-line tool that checks Ansible playbooks for errors and suggests improvements for the code written in the playbooks. This helps the users adhere to certain standards to follow while writing the playbooks to maintain the integrity of their code.

    This article demonstrates how to install Ansible lint. We will also explore use cases and how to prevent errors during execution of playbooks and save debugging time.

    How to install Ansible lint

    The easiest way to install Ansible lint is by using pip as follows:

    python3 -m pip install –user ansible-lint

    On Red Hat Enterprise Linux (RHEL) systems with a Red Hat Ansible Automation Platform subscription, we can also use dnf to install Ansible lint:

    dnf install ansible-lint

    We can also install Ansible lint from the source repository on GitHub, but it requires pip>=22.3.1.

    pip3 install git+https://github.com/ansible/ansible-lint
    

    Next, we will demonstrate two examples. One use case shows how Ansible lint throws a warning, and the other use case demonstrates a syntax error detected by Ansible lint.

    Example 1: Using a built-in module

    We will use the following code sample for an Ansible playbook (playbook.yml):

    
    ---
    - name: Update apt cache
      hosts: all
      tasks:
        - name: Run apt-get update
          ansible.builtin.command: apt-get update

    To run ansible-lint, run the following command:

    ansible-lint playbook.yml

    We get the following output:

    
    WARNING  Listing 2 violation(s) that are fatal
    
    command-instead-of-module: apt-get used in place of apt-get module
    
    playbook.yml:5 Task/Handler: Run apt-get update
    
    no-changed-when: Commands should not change things if nothing needs doing
    
    playbook.yml:5 Task/Handler: Run apt-get update

    Notice that to run an apt-update on our target host, we use the builtin.command instead of the apt module which better serves the purpose. Ansible lint will throw warnings in this case.

    Fixed code:

    ---
    - name: Update apt cache
      hosts: all
      tasks:
        - name: Run apt-get update
          ansible.builtin.apt:
            update_cache: true

    This effectively removes the errors.

    Example 2: Playbook syntax error

    In this example, we will look at a playbook syntax error instead of a warning. Consider a playbook (playbook.yml) where we set an environment variable:

    ---
    
    - name: Example playbook
      hosts: all
      tasks:
        - name: Set environment variable
          ansible.builtin.shell: echo $MY_ENV_VAR
          environment:
          MY_ENV_VAR: my_value

    To run ansible-lint, run the following command:

    ansible-lint playbook.yml

    We get the following output:

    The error appears to be in '/home/tpaul/Repos/lint/playbook2.yml': line 5, column 7, but may
    
    be elsewhere in the file depending on the exact syntax problem.
    
    
    The offending line appears to be:
    
      tasks:
    
        - name: Set environment variable
    
    
          ^ here

    This playbook throws the following error and points us to the file where the error might have occurred after running Ansible lint.

    Upon further inspection, we notice there is an indentation error in the environment line. The fixed code is as follows:

    ---
    - name: Example playbook
      hosts: all
      tasks:
        - name: Set environment variable
          ansible.builtin.shell: echo $MY_ENV_VAR
          environment:
            MY_ENV_VAR: my_value 

    Apart from a couple other warnings, the above code throws no other fatal errors.

    Ansible lint configuration file

    We can also customize how Ansible lint runs against playbooks according to our specific needs by using the Ansible lint configuration file.

    Create a .ansible-lint file in your working directory.

    We can take an example of the exclude_paths parameter which makes Ansible lint exclude the given paths in the configuration file.

    Populate the configuration file with the following code:

    profile: null
    exclude_paths:
      - test/playbook.yml

    Then, create a new playbook.yml file inside a test directory using the following command:

    mkdir test && cd test
    
    touch playbook.yml

    Populate the contents of the playbook with incorrect code. We will take it from the code in example 2:

    ---
    - name: Example playbook
      hosts: all
      tasks:
        - name: Set environment variable
          ansible.builtin.shell: echo $MY_ENV_VAR
          environment:
          MY_ENV_VAR: my_value 

    Run ansible-lint in the working directory to see that no errors are thrown:

    ansible-lint

    You can view the full set of configuration parameters.

    Continue your automation journey with Ansible

    You can download the latest version of the Ansible Automation Platform at no cost. Get started with the Ansible Automation Platform by exploring interactive labs.

    Related Posts

    • Customizing an OpenShift Ansible Playbook Bundle

    • Using Delve to debug Go programs on Red Hat Enterprise Linux

    • Debuginfo is not just for debugging programs

    • How to debug code in CodeReady Workspaces

    • Write your own Red Hat Ansible Tower inventory plugin

    Recent Posts

    • Why some agentic AI developers are moving code from Python to Rust

    • Confidential VMs: The core of confidential containers

    • Benchmarking with GuideLLM in air-gapped OpenShift clusters

    • Run Qwen3-Next on vLLM with Red Hat AI: A step-by-step guide

    • How to implement observability with Python and Llama Stack

    What’s up next?

    This cheat sheet gets you started using Ansible to automate tasks on Cisco Meraki, an enterprise-cloud managed networking solution for managing access points, security appliances, L2 and L3 switches, and more.

    Get the cheat sheet
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue