AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

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 ArticlesAnsible Variables: Complete GuideAnsible Variable Precedence: 22 Levels ExplainedAnsible Vault: Encrypt Sensitive DataAnsible Magic Variables: Complete Reference

Category: database-automation

Browse all Ansible tutorials · AnsiblePilot Home