订阅内容

Computers thrive on consistency. The reason programming and markup languages (like YAML) have a syntax is because computers cannot derive context from data. In natural languages, we use conventions like commas, quotation marks, and parentheses to help each other parse what we write. However, even without these assistive characters, most of us can still comprehend the communication. Computers don't have that ability, so they rely on rules that dictate what kind of data you provide as input.

Because computers rely on syntax, a whole category of applications has arisen with the sole task of validating input before input reaches the intended application.

A linter is designed to catch errors in data before that data gets processed. This saves you (or your automated workflow) from errors during a critical stage of operation.

The yamllint command is designed, as its name implies, to lint YAML. For Ansible playbooks, there's ansible-lint, and you might think that it would render yamllint useless on your system. However, it's yamllint that enables ansible-lint to report generic YAML violations, and it's a good backup linter when you're on a system that doesn't support or doesn't currently have ansible-lint installed on it.

Install

The easiest way to install yamllint is with pip:

$ python3 -m pip install --user yamllint

Confirm the installation:

$ yamllint --version
yamllint X.Y.Z

Using yamllint

Create this simple playbook. It creates a set of directories on a host. I've included intentional errors so that yamllint will return useful output:

- hosts: localhost
  tasks:
    - name: Create directories
      ansible.builtin.file:
        path: "{{ item }}"
        state: directory
      with_items:
      - '~/Foo'   # this is wrong
      - '~/Bar'   # this is wrong
      - '~/Baz'   # this is wrong

With this playbook saved as standard_dirs.yaml in the directory structure ~/Ansible/playbooks, run the yamllint command:

$ cd ~/Ansible
$ yamllint playbooks/standard_dirs.yaml
playbooks/standard_dirs.yaml
 1:1   warning  missing document start "---" (document-start)
 8:7   error    wrong indentation: expected 8 but found 6 (indentation)
 10:16 error    no new line character at the end of file (new-line-at-end-of-file)

It returns one warning and two errors. Each exception listed starts with a line number and character position. Conveniently, the fix is often explicitly spelled out in the error message.

[ Learn more about Ansible by reading an excerpt from Jesse Keating's eBook Mastering Ansible. ]

Fixing errors

  1. The first error occurs on line 1, character 1. Implement the fix by adding --- at the top of the YAML file.
  2. The second exception occurs on line 8 at the seventh character. The error text states this is an indentation error, and you fix it by adding an additional two spaces before each dash (-) character. If you don't know why those are errors, read my YAML for Ansible article.
  3. The final error is on line 10 at character 16. Fix this by adding a newline character (but not a blank line!) at the end of the file. I find the easiest way to do this is:
    $ sed -i -e '$a\' playbooks/standard_dirs.yaml

Check your work

Run yamllint again to confirm that your fixes satisfy YAML syntax requirements:

$ yamllint playbooks/standard_dirs.yaml
$

No output means no errors, so the changes to the file fixed the errors and warnings.

Lint everything that's lintable

If you're writing code or markup in a language that has a linter available for it, then run the linter. The yamllint command is an invaluable tool when you write YAML. It misses some Ansible-specific warnings, but it's a good fallback command for playbooks, a good command to combine with ansible-lint, and it's essential for any YAML you write.


关于作者

Seth Kenlon is a Linux geek, open source enthusiast, free culture advocate, and tabletop gamer. Between gigs in the film industry and the tech industry (not necessarily exclusive of one another), he likes to design games and hack on code (also not necessarily exclusive of one another).

Read full bio
UI_Icon-Red_Hat-Close-A-Black-RGB

按频道浏览

automation icon

自动化

有关技术、团队和环境 IT 自动化的最新信息

AI icon

人工智能

平台更新使客户可以在任何地方运行人工智能工作负载

open hybrid cloud icon

开放混合云

了解我们如何利用混合云构建更灵活的未来

security icon

安全防护

有关我们如何跨环境和技术减少风险的最新信息

edge icon

边缘计算

简化边缘运维的平台更新

Infrastructure icon

基础架构

全球领先企业 Linux 平台的最新动态

application development icon

应用领域

我们针对最严峻的应用挑战的解决方案

Virtualization icon

虚拟化

适用于您的本地或跨云工作负载的企业虚拟化的未来