Extra Vars on the Command Line
Override values at runtime with -e.
Extra Vars on the Command Line is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.
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. ✨
Learn DevOps Bootcamp with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 142
- Lessons
- 568
Frequently Asked Questions
Is the “Extra Vars on the Command Line” lesson free?
Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Extra Vars on the Command Line”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.
What will I learn in “Extra Vars on the Command Line”?
Override values at runtime with -e. You practise DevOps Bootcamp 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 DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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 DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp 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