1---2name: ansible3description: Ansible Conventions and Best Practices4---5# Ansible Conventions and Best Practices67## General Instructions89- Use Ansible to configure and manage infrastructure.10- Use version control for your Ansible configurations.11- Keep things simple; only use advanced features when necessary12- Give every play, block, and task a concise but descriptive `name`13 - Start names with an action verb that indicates the operation being performed, such as "Install," "Configure," or "Copy"14 - Capitalize the first letter of the task name15 - Omit periods from the end of task names for brevity16 - Omit the role name from role tasks; Ansible will automatically display the role name when running a role17 - When including tasks from a separate file, you may include the filename in each task name to make tasks easier to locate (e.g., `<TASK_FILENAME> : <TASK_NAME>`)18- Use comments to provide additional context about **what**, **how**, and/or **why** something is being done19 - Don't include redundant comments20- Use dynamic inventory for cloud resources21 - Use tags to dynamically create groups based on environment, function, location, etc.22 - Use `group_vars` to set variables based on these attributes23- Use idempotent Ansible modules whenever possible; avoid `shell`, `command`, and `raw`, as they break idempotency24 - If you have to use `shell` or `command`, use the `creates:` or `removes:` parameter, where feasible, to prevent unnecessary execution25- Use [fully qualified collection names (FQCN)](https://docs.ansible.com/ansible/latest/reference_appendices/glossary.html#term-Fully-Qualified-Collection-Name-FQCN) to ensure the correct module or plugin is selected26 - Use the `ansible.builtin` collection for [builtin modules and plugins](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#plugin-index)27- Group related tasks together to improve readability and modularity28- For modules where `state` is optional, explicitly set `state: present` or `state: absent` to improve clarity and consistency29- Use the lowest privileges necessary to perform a task30 - Only set `become: true` at the play level or on an `include:` statement, if all included tasks require super user privileges; otherwise, specify `become: true` at the task level31 - Only set `become: true` on a task if it requires super user privileges3233## Secret Management3435- When using Ansible alone, store secrets using Ansible Vault36 - Use the following process to make it easy to find where vaulted variables are defined37 1. Create a `group_vars/` subdirectory named after the group38 2. Inside this subdirectory, create two files named `vars` and `vault`39 3. In the `vars` file, define all of the variables needed, including any sensitive ones40 4. Copy all of the sensitive variables over to the `vault` file and prefix these variables with `vault_`41 5. Adjust the variables in the `vars` file to point to the matching `vault_` variables using Jinja2 syntax: `db_password: "{{ vault_db_password }}"`42 6. Encrypt the `vault` file to protect its contents43 7. Use the variable name from the `vars` file in your playbooks44- When using other tools with Ansible (e.g., Terraform), store secrets in a third-party secrets management tool (e.g., Hashicorp Vault, AWS Secrets Manager, etc.)45 - This allows all tools to reference a single source of truth for secrets and prevents configurations from getting out of sync4647## Style4849- Use 2-space indentation and always indent lists50- Separate each of the following with a single blank line:51 - Two host blocks52 - Two task blocks53 - Host and include blocks54- Use `snake_case` for variable names55- Sort variables alphabetically when defining them in `vars:` maps or variable files56- Always use multi-line map syntax, regardless of how many pairs exist in the map57 - It improves readability and reduces changeset collisions for version control58- Prefer single quotes over double quotes59 - The only time you should use double quotes is when they are nested within single quotes (e.g. Jinja map reference), or when your string requires escaping characters (e.g., using "\n" to represent a newline)60 - If you must write a long string, use folded block scalar syntax (i.e., `>`) to replace newlines with spaces or literal block scalar syntax (i.e., `|`) to preserve newlines; omit all special quoting61- The `host` section of a play should follow this general order:62 - `hosts` declaration63 - Host options in alphabetical order (e.g., `become`, `remote_user`, `vars`)64 - `pre_tasks`65 - `roles`66 - `tasks`67- Each task should follow this general order:68 - `name`69 - Task declaration (e.g., `service:`, `package:`)70 - Task parameters (using multi-line map syntax)71 - Loop operators (e.g., `loop`)72 - Task options in alphabetical order (e.g. `become`, `ignore_errors`, `register`)73 - `tags`74- For `include` statements, quote filenames and only use blank lines between `include` statements if they are multi-line (e.g., they have tags)7576## Linting7778- Use `ansible-lint` and `yamllint` to check syntax and enforce project standards79- Use `ansible-playbook --syntax-check` to check for syntax errors80- Use `ansible-playbook --check --diff` to perform a dry-run of playbook execution8182<!-- 83These guidelines were based on, or copied from, the following sources:8485- [Ansible Documentation - Tips and Tricks](https://docs.ansible.com/ansible/latest/tips_tricks/index.html)86- [Whitecloud Ansible Styleguide](https://github.com/whitecloud/ansible-styleguide)87-->