|
| 1 | +--- |
| 2 | +alwaysApply: true |
| 3 | +--- |
| 4 | + |
| 5 | +# Ansible Role Conventions |
| 6 | + |
| 7 | +- Prefer readability over complexity. |
| 8 | +- Split logic into Ansible Roles to allow composing a combination of roles. |
| 9 | +- Ansible Roles do not solve business logic; they offer system logic. |
| 10 | +- Playbooks can combine Ansible Roles into business logic. |
| 11 | +- Ansible Roles need to be compatible with the distributions listed in `meta/main.yml`. |
| 12 | +- `ansible-lint` should succeed without errors. |
| 13 | + |
| 14 | +## Development Focus Ordering |
| 15 | + |
| 16 | +1. Focus on the intent of the role in `meta/main.yml`. |
| 17 | +2. Focus on user variables, defined in `defaults/main.yml`. |
| 18 | +3. Focus on role variables, defined in `vars/main.yml`. |
| 19 | +4. Focus on tasks, templates, and files. |
| 20 | + |
| 21 | +## Meta (`meta/main.yml`) |
| 22 | + |
| 23 | +- The `galaxy_info.description` captures the intent of the role. |
| 24 | +- The file `meta/main.yml` is used to generate many other files using [Ansible Generator](https://github.com/robertdebock/ansible-generator). |
| 25 | +- **Don't** use `dependencies`: This may surprise a user with extra roles being downloaded and executed. |
| 26 | +- Distributions mentioned are tested using Molecule. |
| 27 | + |
| 28 | +## User Variables (`defaults/main.yml`) |
| 29 | + |
| 30 | +- The user will need to adjust these settings, so making life easier in this file is helpful. |
| 31 | +- Variables must start with the name of the role. For example: the role `ansible-role-xyz` should have `xyz_` prepended to its variables. The intent is to limit variable bleeding and help a user understand where a variable is applicable. |
| 32 | +- Carefully consider if a variable is a string, integer, boolean, list, or map. Changing a variable type later can be difficult for users. |
| 33 | +- Above the variable, in comment style, describe the behaviour of the variable. |
| 34 | + |
| 35 | +## Role Variables (`vars/main.yml`) |
| 36 | + |
| 37 | +- The file `vars/main.yml` is used to determine or set "internal variables". The user should not be interested in these variables, as they are difficult to overwrite. |
| 38 | +- Determining a variable for a value can be done using lookup maps: |
| 39 | + |
| 40 | + ```yaml |
| 41 | + _xyz_packages: |
| 42 | + default: |
| 43 | + - foo |
| 44 | + - bar |
| 45 | + Suse: |
| 46 | + - foobar |
| 47 | + |
| 48 | + xyz_packages: "{{ _xyz_packages[ansible_os_family] | default(_xyz_packages['default']) }}" |
| 49 | + ``` |
| 50 | + |
| 51 | + This pattern can be used for all kinds of things, such as: |
| 52 | + |
| 53 | + 1. Packages |
| 54 | + 2. Service names |
| 55 | + 3. Users and groups |
| 56 | + 4. Logic (tasks/main.yml) |
| 57 | + |
| 58 | +- In the name: line of a task, describe the intent of the task. That may differ slightly from what the task is doing. For example: |
| 59 | + |
| 60 | + ```yaml |
| 61 | + # Describes the intent of a task (strong) |
| 62 | + - name: Configure xyz |
| 63 | + |
| 64 | + # Describes what the task does (weak) |
| 65 | + - name: Place xyz.conf |
| 66 | + ``` |
| 67 | + |
| 68 | +- Spread lines vertically as much as possible to increase readability. For example: |
| 69 | + |
| 70 | + ```yaml |
| 71 | + # Easy to read (vertical) |
| 72 | + - name: Do something |
| 73 | + ansible.builtin.file: |
| 74 | + src: foo |
| 75 | + dest: /foo |
| 76 | + when: |
| 77 | + - xyz |
| 78 | + failed_when: |
| 79 | + - xyz == "xyz" |
| 80 | + notify: |
| 81 | + - XYZ Handler |
| 82 | + |
| 83 | + # Difficult to read (horizontal) |
| 84 | + - name: Do something |
| 85 | + ansible.builtin.file: |
| 86 | + src: foo |
| 87 | + dest: /foo |
| 88 | + when: xyz |
| 89 | + failed_when: xyz == "xyz" |
| 90 | + notify: XYZ Handler |
| 91 | + ``` |
| 92 | + |
| 93 | + An added benefit is that it's very obvious that when, failed_when, changed_when, and handlers are actually (AND) lists. |
| 94 | + |
| 95 | +- Never use equal signs (=) where colons (:) are possible. |
| 96 | +- Never use Jinja in tasks or handlers. Instead, use a mapping in vars/main.yml to determine a value. |
| 97 | +- Try to resolve values in vars/main.yml to prevent unnecessary when conditions. |
| 98 | +- Prefer modules over `ansible.builtin.shell` and `ansible.builtin.command`. |
| 99 | +- Packages should not use `latest` as a version, unless the role is intended to update the package. |
| 100 | +- The `ansible.builtin.file` should mention an `owner`, `group`, and `mode`. |
| 101 | +- Use FQCNs for modules. |
| 102 | +- Used collections should be mentioned in the role `requirements.txt`. |
| 103 | +- Test all variables mentioned in `defaults/main.yml` using `ansible.builtin.assert` in `tasks/assert.yml`. |
| 104 | +- Fail as fast as possible, using `meta/argument_specs.yml` and `tasks/assert.yml`. |
| 105 | +- Avoid using `ansible.builtin.set_fact`. |
| 106 | +- For error-handling, prefer `block` and `rescue`. |
| 107 | +- `failed_when: true` is never preferred, just as `ignore_errors: true`. |
| 108 | +- Only use `gather_facts: true` when the facts are required. |
| 109 | + |
| 110 | +## Handlers |
| 111 | + |
| 112 | +- Don't use the listen directive. |
| 113 | + |
| 114 | +## Templates |
| 115 | + |
| 116 | +- Use `{{ ansible_managed | comment }}` (with the correct comment style)at the top of templates. |
| 117 | +- Although difficult, try to prevent using Jinja. There are exceptions to this rule. |
| 118 | + |
| 119 | +## Files |
| 120 | + |
| 121 | +- It’s nearly always beneficial to use templates. Exceptions are: |
| 122 | + |
| 123 | + 1. Binary files (keep in mind: Ansible is not a packaging solution). |
| 124 | + |
| 125 | +## Molecule |
| 126 | + |
| 127 | +- The `molecule/default/verify.yml` should test the intent of the role. This can mean: to test if a port is open, to test if an installed softwareproduct can show a version number, etc. |
0 commit comments