Bootstrap FreeKB - Ansible - Create variables on the command line using vars_prompt
Ansible - Create variables on the command line using vars_prompt

Updated:   |  Ansible articles

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.

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 optionExtra 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 Buy Me A Coffee



Comments


Add a Comment


Please enter 8f7c65 in the box below so that we can be sure you are a human.