Ansible extra vars (-e): Pass Variables from Command Line (Complete Guide)
By Luca Berton · Published 2024-01-01 · Category: database-automation
How to use Ansible extra vars (-e / --extra-vars) to pass variables from the command line. Override variables, pass JSON, use variable files. Highest precedence variables explained.
Ansible extra vars (-e): Pass Variables from Command Line (Complete Guide)
Extra vars (--extra-vars or -e) are the highest precedence variables in Ansible, passed directly from the command line. They override every other variable source — inventory, group_vars, role defaults, and playbook vars.
Basic Usage
In your playbook:
Variable Formats
Key=Value (Simple)
JSON String
YAML String
Variable File (@filename)
vars/production.yml:
Multiple -e Flags
Variable Precedence
Extra vars have the highest precedence (position 22 of 22). They override everything:
This makes -e perfect for one-time overrides without modifying files.
Common Patterns
Environment Promotion
Version Pinning
Target Host Override
Boolean Flags
Pass Lists and Dictionaries
Extra Vars with Ad-Hoc Commands
Extra Vars with ansible-pull
Secrets and Vault with Extra Vars
Dynamic Extra Vars
From Shell Commands
From Script Output
Default Values for Extra Vars
Always provide defaults in playbooks for optional extra vars:
FAQ
What are extra vars in Ansible?
Extra vars (-e or --extra-vars) are variables passed from the command line when running ansible-playbook. They have the highest precedence in Ansible's variable hierarchy, overriding all other variable sources including inventory, group_vars, and playbook vars.
How do I pass a variable from the command line in Ansible?
Use -e flag: ansible-playbook playbook.yml -e "variable=value". For multiple variables: -e "var1=value1 var2=value2". For complex data, use JSON: -e '{"key": "value"}'. For files: -e @vars.yml.
Can I pass a list or dictionary as an extra var?
Yes, use JSON format: -e '{"my_list": ["a", "b", "c"]}' or -e '{"my_dict": {"key": "value"}}'. Key=value format only works for simple strings.
Do extra vars override all other variables?
Yes. Extra vars have the absolute highest precedence in Ansible (position 22 of 22). They override role defaults, inventory vars, group_vars, host_vars, play vars, role vars, and everything else.
How do I pass a variable file with extra vars?
Use the @ prefix: -e @vars/production.yml. The file can be YAML or JSON. You can combine multiple files and inline values: -e @defaults.yml -e @prod.yml -e "version=2.0".
Conclusion
Extra vars (-e) are the most powerful way to parameterize Ansible playbooks at runtime. Use them for environment promotion, version pinning, and one-time overrides. Always provide default() values for optional extra vars to make playbooks usable with or without command-line arguments.
Related Articles • Ansible Variables: Complete Guide • Ansible Variable Precedence: 22 Levels Explained • Ansible Vault: Encrypt Sensitive Data • Ansible Magic Variables: Complete Reference
Category: database-automation