# Ask for variable value if variable not defined.

**URL:** <https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442>\
**Category:** Ansible Project\
**Created:** [January 28, 2014, 2:25pm UTC](https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442 "2014-01-28T14:25:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Praczko](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@Marcin\_Praczko](https://forum.ansible.com/u/Marcin_Praczko)\
**Post date:** [January 28, 2014, 2:25pm UTC](https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442/1 "2014-01-28T14:25:50Z")

</div>

Hi,

Have question whether is possible (ansible 1.4.2 \>) setup prompt for volue for variable in variable is not defined.

For example:  
First run of playbook:

- variable is not defined
- ask for value
- task write value of variable to some ansible vars file

… Do other tasks…

Next run of playbook:

- variable is read from ansible vars file
- no prompt for value

… Do other tasks …

Tried find this in ansible help, but so far no luck.

Best regards,  
Marcin Praczko

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 28, 2014, 2:42pm UTC](https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442/2 "2014-01-28T14:42:14Z")

</div>

There’s no mechanism for this.

vars\_prompt always prompts for the variable.

---

<div class="post-metadata">

**Author:** ![adam2](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@adam2](https://forum.ansible.com/u/adam2)\
**Post date:** [January 28, 2014, 5:25pm UTC](https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442/3 "2014-01-28T17:25:12Z")

</div>

The following is pseudo code, parts of it may not be entirely correct.  
But the real trick is to use when.

---

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [September 10, 2014, 8:59am UTC](https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442/4 "2014-09-10T08:59:32Z")

</div>

Hi Micheal,  
Do you have any advice for it? It doesn’t accept when condition.  
Best.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 10, 2014, 1:21pm UTC](https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442/5 "2014-09-10T13:21:36Z")

</div>

As politely as possible… Please don’t share made-up syntax on this list, it confuses everyone 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [September 10, 2014, 1:22pm UTC](https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442/6 "2014-09-10T13:22:56Z")

</div>

Nope, it doesn’t do this. Simple as that 🙂

I’d suggest setting a role default instead, or putting a good default value in group\_vars/all, and you can override that on the CLI with “-e”.

vars\_prompt in Ansible is not _usually_ a good idea as it limits the ability of someone to automate a workflow in Ansible, since it’s interactive.

---

<div class="post-metadata">

**Author:** ![Serkan\_C](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Serkan\_C](https://forum.ansible.com/u/Serkan_C)\
**Post date:** [September 12, 2014, 10:45am UTC](https://forum.ansible.com/t/ask-for-variable-value-if-variable-not-defined/14442/7 "2014-09-12T10:45:03Z")

</div>

Hi,  
You’re right, i was using default variables and extra parameters to override but i thought that maybe i can also use it for basic system playbooks.  
Thanks.
