Ansible Style Guide

Follow the existing collection role architecture and variable contracts. Keep package-manager, container and source installations in their designated collections.

Role organization

  • Preserve defaults/main.yml, vars/main.yml, meta/main.yml, meta/argument_specs.yml, tasks/main.yml, templates and handlers.
  • Keep install, uninstall and teardown in separate tasks/state/ files when that lifecycle is used. Keep configuration under tasks/configure/.
  • Use fully qualified collection names and idempotent modules. Use handlers for service restarts and declare actual dependencies.
  • Prefix variables with the role name. Document their type, default and choices in argument specs and the README Role Variables table. Include the role debug toggle.
  • Use double-quoted YAML strings, SPDX headers and a 120-column limit. Keep fallbacks aligned with defaults and preserve explicitly supplied false or empty values.
  • Every task loop needs loop_control, an explicit loop_var and a useful label.
---
- name: Ensure required directories exist
  ansible.builtin.file:
    path: "{{ role_directory }}"
    state: directory
    mode: "0755"
  loop: "{{ example_directories }}"
  loop_control:
    loop_var: role_directory
    label: "{{ role_directory }}"

Run ansible-lint and yamllint, validate playbook syntax and build the collection with the existing Galaxy target. Test repeat runs and removal paths in isolation. Do not publish or deploy during routine validation.