
Following are the differet ways that variables can be set in Ansible. This list is in the order of precedence, where the option higher in the list takes precedence over options lower in the list.
- --extra-vars command line option or Extra Variables in Tower
- set_fact
- include_vars
- vars_prompt (this article)
- vars_files
- vars plugin
- roles vars
- group_vars
- /etc/ansible/hosts or your own inventory file
- lookup vars
- register parameter and debug module
vars_prompt can be used to create a prompt that will define one or more variables. In this example, two variables will be defined, "foo" and "bar".
---
- hosts: localhost
vars_prompt:
- name: foo
prompt: Please enter a value for the 'foo' variable
private: no
- name: bar
prompt: Please enter a value for the 'bar' variable
private: yes
tasks:
- debug:
msg: the foo variable contains a value of {{ foo }}
- debug:
msg: the bar variable contains a value of {{ bar }}
...
AVOID TROUBLE
The -e or --extra-vars command line option, Extra Variables in Tower and set_fact module will take precedence over the vars prompt.
Running this playbook should generate prompts. Notice that the bar prompt will not print any output because the bar prompt is private.
Be aware that all though the "bar" variable is private, the value of the "bar" variable will be accessible when running the ansible-playbook command with the -vvv (very verbose) flag.
Please enter a value for the 'foo' variable: Hello
Please enter a value for the 'bar' variable:
Something like this should be returned.
TASK [debug]
ok: [localhost] => {
"msg": "the foo variable contains a value of Hello"
}
TASK [debug]
ok: [localhost] => {
"msg": "the bar variable contains a value of World"
}
Did you find this article helpful?
If so, consider buying me a coffee over at