AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible-Lint Error key-order: Fix YAML Key Ordering in Playbooks

By Luca Berton · Published 2024-01-01 · Category: installation

Fix ansible-lint key-order error. Understand recommended YAML key ordering for tasks, plays, and roles. Configure or skip the rule with examples.

Ansible Playbook key-order: Keeping Your Playbooks Neat and Error-Free

In the world of Ansible, maintaining well-structured and readable playbooks is essential. The key-order rule is your secret weapon for keeping your playbooks clean and less prone to errors. This rule offers key reordering recommendations to enhance your Ansible coding skills and streamline your playbook development.

The Anatomy of key-order

The key-order rule comes with some essential guidelines: "name" Comes First: For plays, tasks, and handlers, the "name" key should always be the first one. This naming convention helps you quickly grasp the purpose of a specific block of code. "block," "rescue," and "always" Are Last: In tasks, the "block," "rescue," and "always" keys should be positioned at the end. This arrangement reduces the likelihood of accidental misindentation errors, especially when dealing with complex playbooks.

Spotting the Problem

Let's take a look at a problematic code snippet:

Here, we encounter a playbook where the "name" key isn't at the beginning, and the "when" key appears after the "block" key, violating the key-order rule.

Ansible Lint Output

The Correct Order

Let's rectify the previous example and adhere to the key-order rule:

Now, we have reordered the keys according to the key-order rule recommendations.

Why Does key-order Matter?

The key-order rule is not about arbitrary rules but rather a best practice. Here's why it's essential:

1. Code Maintenance:

Properly ordered keys make your playbooks more accessible to read and understand, especially for large and complex scripts. When your playbook grows in complexity, you'll appreciate the consistency in key ordering.

2. Error Prevention:

Correct key ordering reduces the chances of misindentation errors. It keeps the essential keys in predictable locations, minimizing the risk of misplaced keys and accidental mistakes.

3. Rule Evolution:

The key-order rule is not set in stone. It's designed to evolve as the community gathers more evidence on optimal key ordering. It starts with simple guidelines like "name first" and expands as better practices emerge.

4. Playbook Resilience:

By adhering to key ordering, you ensure that your playbooks are robust and adaptable. key-order maintains the playbook's resilience when tasks are moved or modified within the script.

A Note on Automation

The key-order rule doesn't just recommend; it can automatically fix the key ordering using the ansible-lint --fix option. This means that you can effortlessly adhere to this rule without manual reordering, fostering consistency and adherence to best practices in your Ansible code.

Conclusion

In conclusion, the key-order rule in Ansible is your ally in creating well-structured, error-free playbooks. By following this rule, you enhance the readability of your code, reduce the likelihood of errors, and ensure that your playbooks remain resilient as they evolve. It's not just a set of arbitrary guidelines; it's a best practice that grows with the Ansible community's collective wisdom.

Adhering to the key-order rule not only benefits you but also anyone who collaborates with your Ansible projects. It streamlines the development process and promotes code consistency, making it easier for your team to maintain and extend playbooks.

With automation capabilities to automatically enforce this rule, Ansible ensures that your playbooks are not only functional but also structured in a way that reflects best practices. So, the next time you dive into Ansible playbook development, keep the key-order rule in mind to maximize your efficiency and produce high-quality, maintainable code. Happy Ansible coding!

The Error

Recommended Task Key Order

Common Violations

Block Key Order

Full Key Order Reference

Suppress the Rule

Auto-Fix

FAQ

Is key order just cosmetic?

Yes — YAML key order doesn't affect execution. But consistent ordering improves readability and makes code reviews easier.

What's the full recommended order?

name → module → args → when → become → register → changed_when → failed_when → ignore_errors → no_log → loop → tags → notify → delegate_to

Can I customize the expected order?

Not directly in ansible-lint. You can skip the rule or accept the default ordering convention.

Recommended Key Order for Tasks

Recommended Key Order for Plays

Common Violations

Skip the Rule

FAQ

Why does key order matter?

Consistent ordering improves readability. When every task follows the same pattern, scanning playbooks becomes faster. It's a style rule, not a functional one.

Does wrong order break anything?

No — YAML doesn't care about key order. This is purely a linting/style rule for team consistency.

What's the full recommended order for tasks?

name → module → module args → become/become_user → when → register → changed_when/failed_when → notify → loop → tags → no_log.

Related ArticlesAnsible Galaxy GuideAnsible when Conditional GuideAnsible Loops GuideAnsible Handlers Guide

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home