Ansible extra vars allow you to override or add variables at runtime without modifying playbooks or inventory files, making your automation more flexible and reusable. This is especially useful for deploying to different environments with unique settings, like database passwords or server roles, without hardcoding them. You need Ansible installed and a basic playbook ready to use this feature. No special permissions beyond what’s needed to run Ansible are required.
What You'll Need
- Ansible installed on your control node (Linux, macOS, or Windows WSL)
- A working Ansible playbook (.yml file)
- Access to a target host via SSH or another supported connection method
- Basic knowledge of YAML syntax for variable definitions
Step-by-Step
- Open your terminal or command prompt on the machine where Ansible is installed.
- Navigate to the directory containing your Ansible playbook using the
cdcommand. For example:cd ~/ansible/playbooks. - Identify the variable you want to pass. For instance, if your playbook uses a variable called
app_portto define the port for a web server, you’ll override it using extra vars. - Run your playbook with the
--extra-varsflag followed by the variable assignment. Use single quotes to avoid shell interpretation. Example:ansible-playbook deploy-web.yml --extra-vars 'app_port=8080'. - To pass multiple variables, separate them with spaces inside the same quotes:
ansible-playbook deploy-web.yml --extra-vars 'app_port=8080 db_host=192.168.1.10 app_env=production'. - If your variable value contains spaces or special characters, wrap the entire assignment in single quotes and escape inner quotes if needed. For example:
ansible-playbook deploy-web.yml --extra-vars 'message="Hello, world!"'. - To pass a variable as a JSON object, use the
json:prefix:ansible-playbook deploy-web.yml --extra-vars '{"users": ["alice", "bob"], "role": "admin"}'. - To use a JSON file as input, reference it with the
@symbol:ansible-playbook deploy-web.yml --extra-vars '@/path/to/vars.json'. The file must contain valid JSON. - Verify the variables are applied by checking the playbook output. Ansible will display the values of overridden variables during execution under the "TASK" section or in debug output if you added a debug task.
- If using inventory files, remember that extra vars take precedence over inventory variables and playbook defaults, ensuring your runtime values override any static definitions.
Common Mistakes
- Forgetting quotes around variable assignments containing spaces or special characters—this causes shell errors or unintended variable parsing.
- Using double quotes instead of single quotes on Unix-like systems, which may cause the shell to expand variables like $HOME before Ansible sees them.
- Typing variable names incorrectly—Ansible won’t warn you if a variable doesn’t exist in the playbook; it simply won’t apply the override.
- Using the
--extra-varsflag after other flags like-ior-uwithout proper spacing, causing Ansible to misinterpret the command. - Assuming extra vars work with
ansible(ad-hoc commands) the same way as withansible-playbook. They do, but ad-hoc commands don’t use playbooks, so variables only affect modules that accept them. - Passing variables as key=value pairs without quotes when the value is a number or boolean—this usually works but can break if the value includes symbols like commas or colons.
Tips
- Use
--extra-varsto inject secrets from environment variables:ansible-playbook deploy.yml --extra-vars "password=$DB_PASSWORD"—ensure the environment variable is set in your shell before running the command. - Combine extra vars with inventory files:
ansible-playbook -i production.ini deploy.yml --extra-vars "env=prod"lets you reuse the same playbook across environments. - For complex configurations, store extra vars in a YAML file and reference it with
@filename.yml. Ansible supports YAML files this way too, not just JSON. - Add a debug task in your playbook like
- debug: var=app_portto confirm values are received during execution. - Use
ansible-docto check if a module accepts variables you plan to pass—some modules ignore extra vars if they’re not explicitly declared in their parameters. - When using CI/CD pipelines, pass extra vars via environment variables or secrets managers (like HashiCorp Vault or AWS Secrets Manager) using shell expansion for security.
Troubleshooting
- If Ansible says "invalid option" for
--extra-vars, your version is too old (pre-1.8). Upgrade Ansible using pip:pip install --upgrade ansible. - If variables aren’t taking effect, check for typos in the variable name. Ansible is case-sensitive:
appPort≠app_port. - If you see "ERROR! syntax error" when using JSON, validate the JSON structure with a tool like jsonlint.com. Extra commas or unquoted keys break parsing.
- If using a file with
@, ensure the path is absolute or relative to your current working directory. Usepwdto confirm location. - If the playbook runs but ignores your variable, check if the variable is being overwritten later in the playbook by another task or role. Extra vars are high priority but can be overridden by host/group vars in inventory if explicitly set.
- For Windows users: use double quotes consistently and avoid PowerShell’s variable expansion by wrapping everything in single quotes:
'key=value'. - Check the Ansible verbosity level: run with
-vor-vvvto see which variables are being loaded and from where.
Frequently Asked Questions
Can I use extra vars with ad-hoc Ansible commands?
Yes, you can use--extra-vars with ansible for one-off tasks. For example: ansible webservers -m ping --extra-vars '{"timeout": 30}'. The variable is passed to the module if it supports it.
Do extra vars override inventory variables?
Yes, extra vars have higher precedence than inventory variables, group_vars, and host_vars. They’re among the highest in Ansible’s variable precedence hierarchy, right after command-line flags like-e and before defaults.
Can I pass a list or array using extra vars?
Yes, use JSON format:--extra-vars '{"servers": ["server1", "server2"]}'. YAML files referenced with @ also support lists and nested structures.
Is it safe to pass passwords via extra vars on the command line?
Not recommended. Command-line arguments can be visible in process lists or shell history. Use encrypted files with ansible-vault or inject secrets via environment variables instead.Why does my variable show as null even with extra vars?
It likely means the variable name in your playbook doesn’t match the one you’re passing. Double-check spelling and case. Also, ensure the variable isn’t being set to null later in the playbook’s execution flow.Can I use extra vars with Ansible Galaxy roles?
Yes. Extra vars are passed to all roles in a playbook. If a role expects a variable likenginx_port, you can override it via --extra-vars 'nginx_port=8080' even if the role is imported from Galaxy.