Linting

“the process of running a program that will analyze code for potential errors”

yamllint <filename>.yml

In our example files:

error.yml
- hosts: 1.1.1.1
   gather_facts: no

  vars_files:
       ext_vars.yml


  tasks:

   - name: Some task here
     module: 
       module_commands:
noerror.yml
---
- hosts: 1.1.1.1
  gather_facts: false


  tasks:

  - name: task here
yamllint
yamllint error.yml
yamllint noerror.yml
../_images/lint.png

Fig 4

Configurations of Yamllint

Depending on your OS

Container
  • ls /usr/lib/python3.7/site-packages/yamllint/conf

pip3 on Ubuntu
  • ls /usr/lib/python3/dist-packages/yamllint/conf

pip3 on Mac (homebrew)
  • ls /usr/local/lib/python3.7/site-packages/yamllint/conf

You will see two files, default and relaxed. These come included during installation but you can always make your own, Happy Googling

default.yaml
 1 ---
 2 rules:
 3   braces: enable
 4   brackets: enable
 5   colons: enable
 6   commas: enable
 7   comments:
 8     level: warning
 9   comments-indentation:
10     level: warning
11   document-end: disable
12   document-start:
13     level: warning
14   empty-lines: enable
15   empty-values: enable
16   hyphens: enable
17   indentation: enable
18   key-duplicates: enable
19   key-ordering: disable
20   line-length: enable
21   new-line-at-end-of-file: enable
22   new-lines: enable
23   octal-values: enable
24   quoted-strings: disable
25   trailing-spaces: enable
26   truthy:
27     level: warning

One more “linting” item to cover before we look into plays/playbooks. We can use Ansible to find other issues that are more Ansible related

ansible-playbook error.yml --syntax-check

Or you can install ansible-lint See Github project page for more here

pip3 install ansible-lint --user
Example ansible-lint
ansible-lint somefile.yml