Slide 1
Most trusted JOB oriented professional program
DevOps Certified Professional (DCP)

Take your first step into the world of DevOps with this course, which will help you to learn about the methodologies and tools used to develop, deploy, and operate high-quality software.

Slide 2
DevOps to DevSecOps – Learn the evolution
DevSecOps Certified Professional (DSOCP)

Learn to automate security into a fast-paced DevOps environment using various open-source tools and scripts.

Slide 2
Get certified in the new tech skill to rule the industry
Site Reliability Engineering (SRE) Certified Professional

A method of measuring and achieving reliability through engineering and operations work – developed by Google to manage services.

Slide 2
Master the art of DevOps
Master in DevOps Engineering (MDE)

Get enrolled for the most advanced and only course in the WORLD which can make you an expert and proficient Architect in DevOps, DevSecOps and Site Reliability Engineering (SRE) principles together.

Slide 2
Gain expertise and certified yourself
Azure DevOps Solutions Expert

Learn about the DevOps services available on Azure and how you can use them to make your workflow more efficient.

Slide 3
Learn and get certified
AWS Certified DevOps Professional

Learn about the DevOps services offered by AWS and how you can use them to make your workflow more efficient.

previous arrow
next arrow

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
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x