The -e or --extra-vars command line option can be used to create variables on the command line, like this.
ansible-playbook playbook.yml --extra-vars "foo=Hello bar=World"
Or like using, using the JSON format.
ansible-playbook playbook.yml --extra-vars '{"foo": "Hello", "bar": "World"}'
In a playbook, the debug module can be used to print each variable.
---
- hosts: all
tasks:
- debug:
msg: "{{ foo }}"
- debug:
msg: "{{ bar }}"
Something like this should be returned.
TASK [debug]
ok: [server1.example.com] => {
"msg": "Hello"
}
TASK [debug]
ok: [server1.example.com] => {
"msg": "World"
}
Array
Here is how you would pass in an array, using the JSON format.
ansible-playbook playbook.yml --extra-vars '{"foo": [Hello, World] }'
Booleans
Here is how you would create boolean variables.
ansible-playbook playbook.yml --extra-vars "{foo: true, bar: false}"
Precedence
The -e or --extra-vars command line option will take precedence over every other way that a variable can be set. For example, let's say you attempt to update the foo variable to contain a value of "Goodbye" using the vars plugin.
---
- hosts: all
vars:
foo: Goodbye
tasks:
- debug:
msg: "{{ foo }}"
The foo variable will retain it's value of "Hello" because the -e or --extra-vars command line options take precedence.
TASK [debug]
ok: [server1.example.com] => {
"msg": "Hello"
}