🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started 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/

Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x