Ansible for Proxmox VE · Module 04
10-node-prep.yml into a role without changing its behaviourdefaults/, logic in tasks/, files in templates/Your 10-node-prep.yml is 60 lines. It will not stay that way.
A role is a directory layout with conventions, nothing more.
roles/pve_node/
├── defaults/main.yml # values, lowest precedence - meant to be overridden
├── vars/main.yml # values that are NOT meant to be overridden
├── tasks/main.yml # the tasks (no "hosts:", no "- name: play")
├── handlers/main.yml # the handlers
├── templates/ # Jinja2 templates (.j2)
├── files/ # files copied verbatim
└── meta/main.yml # dependencies, metadataAnsible loads main.yml from each of these automatically. You do not wire anything up yourself.
defaults/ is the role’s interfaceroles/pve_node/defaults/main.yml
Anyone using your role can override any of these. That is the contract.
vars/main.yml is the opposite: values the role needs and callers should not touch. Use it sparingly, because it outranks almost everything.
From weakest to strongest, the parts you will actually meet:
roles/x/defaults/main.ymlinventory/group_vars/inventory/host_vars/roles/x/vars/main.ymlvars: in the play--extra-vars on the command lineThe working rule: put values in defaults/, override them in group_vars/. That covers almost every case.
A template renders a file from inventory data and facts. The classic first one is a message of the day:
templates/motd.j2
The logic leaves the YAML. Diffs become readable, and the template can be inspected on its own.
template owns the whole fileThat is the difference to lineinfile or blockinfile, which own a marked region.
blockinfile |
template |
|
|---|---|---|
| Owns | a marked region | the whole file |
| Foreign changes | survive | are overwritten |
| Use when | the file has other owners | the file is yours alone |
/etc/motd is yours. /etc/hosts is not — the provisioning writes it, and Proxmox VE resolves its own node name through it. Pick the file before you pick the module.
A tagged partial run can leave a host in a state that no full run would produce. Tags are for fast iteration while developing, not a substitute for structure.
ansible-lintIt catches the things reviewers would otherwise catch for you: missing name:, shell where command suffices, deprecated syntax, unsafe file permissions.
Convert 10-node-prep.yml into the role pve_node and prove that nothing changed: the first run after the conversion must report changed=0.
→ Exercise sheet: From playbook to role
credativ · Ansible for Proxmox VE