1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #1875 from cocoy/vars_prompt_default

Adding default on vars_prompt
This commit is contained in:
Michael DeHaan 2013-01-21 06:57:50 -08:00
commit ef38e9f0e6
6 changed files with 22 additions and 8 deletions

View file

@ -29,8 +29,10 @@
- name: "some_password" - name: "some_password"
prompt: "Enter password" prompt: "Enter password"
private: yes private: yes
- name: "release_version" - name: "release_version"
prompt: "Product release version" prompt: "Product release version"
default: "my_default_version"
private: no private: no
- name: "my_password2" - name: "my_password2"
@ -48,7 +50,7 @@
tasks: tasks:
- name: imagine this did something interesting with $release_version - name: imagine this did something interesting with $release_version
action: shell echo foo >> /tmp/$release_version-$alpha action: shell echo foo >> /tmp/$release_version-alpha
- name: look we crypted a password - name: look we crypted a password
action: shell echo my password is $my_password2 action: shell echo my password is $my_password2

View file

@ -72,7 +72,7 @@ class CallbackModule(object):
def playbook_on_task_start(self, name, is_conditional): def playbook_on_task_start(self, name, is_conditional):
pass pass
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None): def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
pass pass
def playbook_on_setup(self): def playbook_on_setup(self):

View file

@ -435,7 +435,7 @@ class PlaybookCallbacks(object):
print banner(msg) print banner(msg)
call_callback_module('playbook_on_task_start', name, is_conditional) call_callback_module('playbook_on_task_start', name, is_conditional)
def on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None): def on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
if prompt: if prompt:
msg = "%s: " % prompt msg = "%s: " % prompt
@ -458,10 +458,17 @@ class PlaybookCallbacks(object):
else: else:
result = prompt(msg, private) result = prompt(msg, private)
# if result is false and default is not None
if not result and default:
result = default
if encrypt: if encrypt:
result = utils.do_encrypt(result,encrypt,salt_size,salt) result = utils.do_encrypt(result,encrypt,salt_size,salt)
call_callback_module('playbook_on_vars_prompt', varname, private=private, prompt=prompt, encrypt=encrypt, confirm=confirm, salt_size=salt_size, salt=None) call_callback_module( 'playbook_on_vars_prompt', varname, private=private, prompt=prompt,
encrypt=encrypt, confirm=confirm, salt_size=salt_size, salt=None, default=default
)
return result return result

View file

@ -189,6 +189,7 @@ class Play(object):
vname = var['name'] vname = var['name']
prompt = var.get("prompt", vname) prompt = var.get("prompt", vname)
default = var.get("default", None)
private = var.get("private", True) private = var.get("private", True)
confirm = var.get("confirm", False) confirm = var.get("confirm", False)
@ -197,13 +198,17 @@ class Play(object):
salt = var.get("salt", None) salt = var.get("salt", None)
if vname not in self.playbook.extra_vars: if vname not in self.playbook.extra_vars:
vars[vname] = self.playbook.callbacks.on_vars_prompt(vname, private, prompt,encrypt, confirm, salt_size, salt) vars[vname] = self.playbook.callbacks.on_vars_prompt (
vname, private, prompt, encrypt, confirm, salt_size, salt, default
)
elif type(self.vars_prompt) == dict: elif type(self.vars_prompt) == dict:
for (vname, prompt) in self.vars_prompt.iteritems(): for (vname, prompt) in self.vars_prompt.iteritems():
prompt_msg = "%s: " % prompt prompt_msg = "%s: " % prompt
if vname not in self.playbook.extra_vars: if vname not in self.playbook.extra_vars:
vars[vname] = self.playbook.callbacks.on_vars_prompt(varname=vname, private=False, prompt=prompt_msg) vars[vname] = self.playbook.callbacks.on_vars_prompt(
varname=vname, private=False, prompt=prompt_msg, default=None
)
else: else:
raise errors.AnsibleError("'vars_prompt' section is malformed, see docs") raise errors.AnsibleError("'vars_prompt' section is malformed, see docs")

View file

@ -86,7 +86,7 @@ class CallbackModule(object):
def playbook_on_task_start(self, name, is_conditional): def playbook_on_task_start(self, name, is_conditional):
pass pass
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None): def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
pass pass
def playbook_on_setup(self): def playbook_on_setup(self):

View file

@ -73,7 +73,7 @@ class CallbackModule(object):
else: else:
say("Notifying task: %s" % name, REGULAR_VOICE) say("Notifying task: %s" % name, REGULAR_VOICE)
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None): def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
pass pass
def playbook_on_setup(self): def playbook_on_setup(self):