Use this when organizing values used throughout automation.
Variables · Automation Design
Variables hold values used throughout automation, such as host addresses or package versions, defined at the right scope (global, group, host, run) so they can change without editing logic.
1. Variables hold data used by automation.
2. Know precedence (Ansible has many levels).
3. Clear names and scopes.
4. Don't hard-code secrets.
5. Document defaults.
defaults → group/host vars → extra vars → effective value
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
- Same variable defined in several places with different precedence.
- Secrets in plain variable files.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Variables should be defined at the narrowest sensible scope.
- Know the tool's precedence order.
- Secrets belong in a vault, not plain variables.
ansible-inventory --host <host>— effective variables- Variable precedence rules — where values come from
- Naming conventions — clarity
ReferenceWhy it matters1 section · depth on demand
What it is
Variables hold values that change between environments or targets: hostnames, sizes, versions. Keep them separate from logic (vars files, inventory, tfvars), give them types and defaults where safe, and validate them before use.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Variables carry the values that differ between runs — hostnames, IP addresses, versions, feature flags — so the same automation logic works everywhere. Where a variable's value comes from and which source wins when several define it are the questions behind most "why did it use that value?" problems.
Good practice:
- Keep variables in one well-defined place per scope (global, environment, group, host).
- Know the precedence order of your tool; in Ansible, extra vars (
-e) override everything. - Name variables by what they mean (
ntp_servers, notlist1) and prefix role variables with the role name. - Keep secrets out of plain variable files — reference a vault or secret store.
- Validate types and required values at the start of a run.
ansible-inventory --host sw01 --vars # show the merged variables for one host
ansible -m debug -a 'var=ntp_servers' sw01
Example
variable "vm_count" {
type = number
default = 2
validation {
condition = var.vm_count <= 10
error_message = "Max 10 VMs."
}
}
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Automation Design > Variables. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/azure/well-architected/operational-excellence/enable-automation · confidence 94%