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.


Method 1

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:

Method 3
If you are passing variables from a file, you need a second –extra-vars flag to pass variables.

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:



Rajesh Kumar
Total Page Visits: 864 - Today Page Visits: 1