Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      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
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java 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

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • 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

    • GuideLLM: Evaluate LLM deployments for real-world inference

    • Unleashing multimodal magic with RamaLama

    • Integrate Red Hat AI Inference Server & LangChain in agentic workflows

    • Streamline multi-cloud operations with Ansible and ServiceNow

    • Automate dynamic application security testing with RapiDAST

    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

    Red Hat legal and privacy links

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

    Report a website issue