Extra Vars on the Command Line
Override values at runtime with -e.
Extra Vars on the Command Line is a free Ansible Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ansible Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Values at Runtime
Sometimes you want to pick a value when you launch a play, not bake it into a file. That is what extra vars are for. 🌟
The -e Flag
Pass extra vars with the -e (or --extra-vars) flag on ansible-playbook. Each one becomes a variable for the whole run.
ansible-playbook site.yml -e "env=prod"Multiple key=value Pairs
Separate several values with spaces inside one quoted string. Ansible parses each key=value pair into a variable.
ansible-playbook site.yml -e "env=prod port=443"Repeating -e
You can also pass -e more than once. Each appearance adds its own variable, which keeps long commands readable.
ansible-playbook site.yml -e env=prod -e port=443JSON for Rich Types
key=value makes everything a string. To pass numbers, lists or dictionaries, use JSON inside the extra vars instead.
ansible-playbook site.yml -e '{"port": 443}'Load Extra Vars from a File
Prefix the value with an @ to read variables from a YAML or JSON file. Great for a long set of overrides.
ansible-playbook site.yml -e @overrides.ymlExtra Vars Win
An extra var beats almost every other source. If env is set in vars and on the CLI, the command line value is used.
A Safe Override Default
Pair extra vars with the default filter so the play still works when no override is given.
name: "{{ env | default('dev') }}"Good for Environments
The same playbook can target dev, staging or prod just by changing one extra var at launch. One file, many targets.
ansible-playbook deploy.yml -e env=stagingToggle Behavior with Flags
Extra vars work nicely as on/off switches. Combine one with a when condition to enable or skip a task.
when: run_migrations | default(false)Quote Spaces Carefully
If a value contains spaces, wrap the whole pair in quotes so your shell does not split it. Mind shell quoting.
ansible-playbook s.yml -e "motd=Hello there"Quick Check
You set port in vars and also pass -e port=443. Which value wins?
Recap
You can now override values at launch with -e, pass JSON or @files, and rely on extra vars beating other sources. ✨
Frequently asked questions
Is the “Extra Vars on the Command Line” lesson free?
Yes — the full text of “Extra Vars on the Command Line” is free to read here on the web, and the Ansible Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ansible Academy course, upgrade to CoddyKit PRO.
What will I learn in “Extra Vars on the Command Line”?
Override values at runtime with -e. You practise Ansible Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ansible Academy?
No prior experience is required. Ansible Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Extra Vars on the Command Line” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ansible Academy lesson?
Yes. Every Ansible Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining & Using vars in a Play
- The {{ }} Substitution Syntax
- Extra Vars on the Command Line
- Where Variables Come From & Precedence