# ansible / foundations / variables
Dynamic Playbooks with Variables
In Ansible, a variable is a placeholder used to store a value that can be reused across playbooks, roles, inventories, and tasks. Stop hard-coding data and make your automation scalable and flexible.
Why Use Variables?
Writing a unique playbook for every single server is impossible to maintain. Variables solve this by allowing you to define the structure once, and feed the specifics in later.
Avoid Hard-Coding
By extracting values like IPs, usernames, and ports, you can reuse the exact same playbook across different environments (dev, test, prod) simply by swapping the variable file.
Reduce Human Errors
If you need to change a package version or a directory path, you update it in one central variable location instead of hunting through hundreds of tasks.
Types of Ansible Variables
| Variable Type | Example Implementation | Purpose & Context |
|---|---|---|
| Play vars | vars: | Defined and used directly inside a specific playbook. |
| Inventory vars | host var | Values attached to a specific host in the inventory. |
| Group vars | group_vars/web.yml | Applied to all hosts within a specific inventory group. |
| Host vars | host_vars/server.yml | Applied exclusively to one specific host. |
| Role defaults | defaults/main.yml | Fallback variables for roles (Lowest priority). |
| Role vars | vars/main.yml | Strict variables for roles (High priority). |
| Facts | ansible_os_family | System information automatically collected from the target. |
| Extra vars | -e "a=1" | Passed via CLI at runtime (Highest priority, overrides all). |
| Register | register: result | Captures and stores the output of a specific task. |
| Set facts | set_fact: | Variables dynamically created or calculated during runtime. |
| vars_prompt | User input | Pauses execution to ask the user for a value at runtime. |
| vars_files | vars_files: | Loads external YAML files containing variable definitions. |
| Environment vars | environment: | Sets OS-level environment variables for the shell. |
| Loop vars | item | The magic variable representing the current iteration of a loop. |
| Magic vars | hostvars | Built-in Ansible engine variables holding state data. |
Variables in Action
--- - hosts: web vars: myname: "shan" myage: 26 vars_files: - ak.yaml tasks: - name: Display my name and age debug: msg: "My name is {{ myname }} and my age is {{ myage }}" - name: Display OS distribution debug: msg: "The OS distribution is {{ ansible_distribution }}" - name: Display values from external file debug: msg: "Son of {{ fname }} & {{ mname }}" - name: Display host-specific variable debug: msg: "The server is located in {{ location }}"
Step-by-Step Breakdown
- 1. Hosts
- hosts: weblimits the playbook to run only on the web host group defined in your inventory. - 2. Playbook Vars
vars:defines variables (myname, myage) that are available exclusively inside this play. - 3. External File
vars_files:imports external variables from ak.yaml (which containsfname: Kumarandmname: Lakshmi). - 4. System Facts
{{ ansible_distribution }}is an automatic "Fact" variable collected by Ansible representing the remote OS (e.g., Ubuntu). - 5. Host Vars
{{ location }}is pulled directly from a file like host_vars/node1.yaml, applying only to that specific host.
Critical Rules & Precedence
Always put quotes around a variable if it starts a value! For example, name: {{ myname }} will trigger a YAML syntax error. It must be written as name: "{{ myname }}".
If the same variable is defined in multiple places, Ansible follows a strict precedence list. Extra vars (passed via the CLI with -e) have the highest priority and will overwrite any variable in files or roles.