Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Ansible Variable: How can I pass variable to ansible playbook in the command line?

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 }}

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') }}

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"

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]}'

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"

Reference
https://docs.ansible.com/ansible/2.5/user_guide/playbooks_variables.html#passing-variables-on-the-command-line

https://ttl255.com/ansible-pass-extra-variables-to-playbook/

Rajesh Kumar
Follow me
Latest posts by Rajesh Kumar (see all)
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x