Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

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

    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

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    • Fun in the RUN instruction: Why container builds with distroless images can surprise you

    • Trusted software factory: Building trust in the agentic AI era

    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

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    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
    © 2026 Red Hat

    Red Hat legal and privacy links

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

    Chat Support

    Please log in with your Red Hat account to access chat support.