It is possible to set variables at the command line using the –extra-vars (or -e) argument. Variables can be defined using a single quoted string (containing one or more variables) using one of the formats below.
key=value
Method 1
$ ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo"
$ ansible-playbook release.yml -e "version=1.23.45 other_variable=foo"
In your yml file you assign these to scoped ansible variables by doing something like:
vars:
my_version: "{{ version }}"
my_other_variable: {{ other_variable }}
Code language: JavaScript (javascript)
Method 2
An alternative to using command line args is to utilise environmental variables that are already defined within your session, you can reference these within your ansible yml files like this:
# To set a environmental variables
$ export version=1.23.45
$ export other_variable=foo"
vars:
my_version: "{{ lookup('env', 'version') }}"
my_other_variable: {{ lookup('env', 'other_variable') }}
Code language: PHP (php)
Method 3
If you are passing variables from a file, you need a second –extra-vars flag to pass variables.
$ ansible-playbook release.yml --extra-vars "@some_file.json"
$ ansible-playbook release.yml -e "@some_file.json"
$ ansible-playbook release.yml --extra-vars "version=1.23.45 other_variable=foo" --extra-vars "@some_file.json"
$ ansible-playbook release.yml -e "version=1.23.45 other_variable=foo" --extra-vars "@some_file.json"Code language: JavaScript (javascript)
Method 4
How can I pass yaml array to –extra-vars in Ansible playbook?
To answer your first question “How can I pass yaml array to –extra-vars in Ansible playbook.” you can pass in a json formatted string to extra-vars.
Here is an example play:
- hosts: all
gather_facts: no
tasks:
- debug: var=test_list
And how to pass in test_list to ansible-playbook:
$ ansible-playbook -c local -i localhost, test.yml --extra-vars='{"test_list": [1,2,3]}'Code language: JavaScript (javascript)
Method 5 – PASSING MULTIPLE VARIABLES.
$ ansible-playbook extra_var_multiple.yml --extra-var "my_var1=CoolVar1 my_var2=WarmVar2"
PASSING VARIABLES CONTAINING SPACES.
$ ansible-playbook extra_var_single.yml -e "my_var='Very Hot Var with spaces'"
#PASSING INTEGER, BOOL, LIST, OR ANY OTHER NON-STRING VALUES.
ansible-playbook extra_var_json.yml -e '{"my_float": 1.618, "my_list": ["HGTTG", 42], "my_string": "Time is an illusion."}'
#PASSING JSON FORMATTED VARIABLES SAVED IN A FILE
[przemek@quasar extra_var]$ cat extra_vars.json
{"my_float": 1.618, "my_list": ["HGTTG", 42], "my_string": "Time is an illusion."}
ansible-playbook extra_var_json.yml -e "@extra_vars.json"
Code language: PHP (php)
https://ttl255.com/ansible-pass-extra-variables-to-playbook/
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals