Oddity with vars_prompt defaults

357 views
Skip to first unread message

Jim Kleckner

unread,
Apr 18, 2014, 2:41:24 PM4/18/14
to ansible...@googlegroups.com
See the input and output below for some oddity with vars_prompt.

First, note that yes is being coerced to True but no results in a missing default.
Quoting it works around this at the expense of no longer being boolean.

Second, would it make sense to have a boolean option to have vars_prompt do the smart coercion of yes/no/true/false to boolean?
I find that the result being a string requires pushing bool down through the code.

Thanks, Jim


---
- name: Default values of prompts
  hosts
: localhost
  gather_facts
: no
  vars_prompt
:
   
- name: yes_val_from_prompt
      prompt
: Test for boolean value - enter yes or no
     
default: yes
     
private: no
   
# Bug?  Why does the default value not get printed?
   
- name: no_val_from_prompt
      prompt
: Test for boolean value - enter yes or no
     
default: no
     
private: no
   
- name: yes_string_val_from_prompt
      prompt
: Test for boolean value - enter yes or no
     
default: "yes"
     
private: no
   
# Bug?  Why does the default value not get printed?
   
- name: no_string_val_from_prompt
      prompt
: Test for boolean value - enter yes or no
     
default: "no"
     
private: no
  tasks
:
   
- debug: var=yes_val_from_prompt
   
- debug: var=no_val_from_prompt
   
- debug: var=yes_string_val_from_prompt
   
- debug: var=no_string_val_from_prompt


Results in:


ansible-playbook -i inv test_prompt_no_default.yml

Test for boolean value - enter yes or no [True]:  
Test for boolean value - enter yes or no:      <======= Missing default
Test for boolean value - enter yes or no [yes]:  
Test for boolean value - enter yes or no [no]:  

PLAY
[Default values of prompts] **********************************************  

TASK
: [debug var=yes_val_from_prompt] *****************************************  
ok
: [127.0.0.1] => {
   
"yes_val_from_prompt": "True" <========== coerced to True
}

TASK
: [debug var=no_val_from_prompt] ******************************************  
ok
: [127.0.0.1] => {
   
"no_val_from_prompt": "" <======== Missing default
}

TASK
: [debug var=yes_string_val_from_prompt] **********************************  
ok
: [127.0.0.1] => {
   
"yes_string_val_from_prompt": "yes" <======= Not coerced to True (as expected)
}

TASK
: [debug var=no_string_val_from_prompt] ***********************************  
ok
: [127.0.0.1] => {
   
"no_string_val_from_prompt": "no"
}

PLAY RECAP
********************************************************************  

127.0.0.1                  : ok=4    changed=0    unreachable=0    failed=0  


Jim Kleckner

unread,
Apr 18, 2014, 2:56:35 PM4/18/14
to ansible...@googlegroups.com
I realize now I should have sent this to the devel list.
Will send an issue and proposed patch.

diff --git a/lib/ansible/callbacks.py b/lib/ansible/callbacks.py
index 1abfe68..cbfd315 100644
--- a/lib/ansible/callbacks.py
+++ b/lib/ansible/callbacks.py
@@ -643,7 +643,7 @@ class PlaybookCallbacks(object):
 
     def on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
 
-        if prompt and default:
+        if prompt and default is not None:
             msg = "%s [%s]: " % (prompt, default)
         elif prompt:
             msg = "%s: " % prompt

Michael DeHaan

unread,
Apr 23, 2014, 8:58:09 AM4/23/14
to ansible...@googlegroups.com
When typing these up, please include an English description of what you expect and what you get, and a short summary, otherwise it can be very difficult to interpret the playbooks.


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/462b1836-ed1c-4173-8bae-800106ab8f7f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Jim Kleckner

unread,
Apr 23, 2014, 7:45:49 PM4/23/14
to ansible...@googlegroups.com
Yes, had done that in the issue:

Do you prefer it in the group in addition?

Jim

Michael DeHaan

unread,
Apr 24, 2014, 10:39:49 AM4/24/14
to ansible...@googlegroups.com
Just the ticket should be fine, thanks!


Reply all
Reply to author
Forward
0 new messages