Bobcares

How to use Ansible Lint to scan playbooks

by | Mar 18, 2020

Are you looking at how to use Ansible Lint?

Ansible is an IT automation tool.

Ansible Lint is a command-line tool that analyzes playbook code for potential errors.

At Bobcares, we get many queries regarding Ansible, as a part of our Server Management Services.

Today, our Support Engineers will give an idea of how the linting process helps.

 

What is ansible-lint?

Before getting to ansible-lint, let’s have a look at Ansible.

Ansible is an open-source IT automation tool. The purpose of this tool is to configure systems, deploy software and many more.

Usually, Ansible connects to other nodes via SSH and pushes Ansible modules into them. Ansible holds its configurations in .yml files.

An important YAML file is Ansible playbook. It defines how the Ansible should work in the hosts. So errors in the YAML files can affect the working of Ansible.

This is where we make use of a command-line tool, ansible-lint. We use this command for linting playbooks or .yml files in Ansible.

A linting process runs a program that analyses code and finds potential errors in Ansible code. That is it detects the behaviors and practices that need improvement. It can detect syntax errors, undefined variables, etc.

 

Installing the linting tool in Ansible

Ansible-lint is a command-line tool. It does not come with the Ansible package, rather we need to install it. We can install it in various ways.

To install it using the built-in package manager in Ubuntu we use the command,

apt install ansible-lint

Since ansible-lint is a Python package we can install it using the command,

pip install ansible-lint

Now let’s see how our Support Engineers use the linting command in Ansible.

 

How to use ansible-lint?

To obtain complete information on using the linting command, we use,

ansible-lint --help

And the output appears as,

How to use ansible lint

There many default rules used while linting a playbook. To list them, we use the command,

ansible-lint -L

There are a total of 18 default rules. A few rules are,

ANSIBLE0002: Trailing whitespace
There should not be any trailing whitespace
ANSIBLE0004: Git checkouts must contain explicit version
All version control checkouts must point to an explicit commit or tag, not just "latest"
ANSIBLE0005: Mercurial checkouts must contain explicit revision
All version control checkouts must point to an explicit commit or tag, not just "latest"
ANSIBLE0006: Using command rather than module
Executing a command when there is an Ansible module is generally a bad idea
ANSIBLE0007: Using command rather than an argument to e.g. file
Executing a command when there is are arguments to modules is generally a bad idea
ANSIBLE0008: Deprecated sudo
Instead of sudo/sudo_user, use become/become_user.
. . .

For instance, if a playbook has trailing whitespace, the linting output shows up as,

How to use ansible lint

Here the output lists the violated rule along with the exact line number where it occurred.

Likewise, the syntax errors in playbook appear as,

Syntax Error while loading YAML.
did not find expected '-' indicator

The error appears to be in '/etc/ansible/playbook.yml': line 14, column 6, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

name: Disable SELinux
^ here

Here it displays the offending line along with exact error.

Most importantly, a user should run the linting command in the same location as that of the .yml file.

To scan all the .yml file in the location we can use the command,

ansible-lint *.yml

 

How to custom configure it?

Next, let’s see the possibility of custom configuring the linting tool.

We can custom configure ansible-lint for our requirement. Apart from the default rules, we can specify additional rules in a file.

And the command-line flag -c <file path> will lint the playbook based on the custom configuration.

 

[Need assistance in using Ansible lint? – We can help you.]

 

Conclusion

So far, we saw how to use the ansible-lint command-line tool. Today, our Support Engineers gave a complete guide on its installation, usage, and configuration.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF