From 7ae100611c2b62dfb3c6a0993c8130be83d100f1 Mon Sep 17 00:00:00 2001 From: Rodney Quillo Date: Fri, 18 Jan 2013 19:00:57 +0800 Subject: [PATCH 1/2] Adding default on vars_prompt --- examples/playbooks/prompts.yml | 4 +++- lib/ansible/callback_plugins/noop.py | 2 +- lib/ansible/callbacks.py | 11 +++++++++-- lib/ansible/playbook/play.py | 9 +++++++-- plugins/callbacks/log_plays.py | 2 +- plugins/callbacks/osx_say.py | 2 +- 6 files changed, 22 insertions(+), 8 deletions(-) diff --git a/examples/playbooks/prompts.yml b/examples/playbooks/prompts.yml index 6b02007249..564061ae12 100644 --- a/examples/playbooks/prompts.yml +++ b/examples/playbooks/prompts.yml @@ -29,8 +29,10 @@ - name: "some_password" prompt: "Enter password" private: yes + - name: "release_version" prompt: "Product release version" + default: "my_default_version" private: no - name: "my_password2" @@ -48,7 +50,7 @@ tasks: - 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 action: shell echo my password is $my_password2 diff --git a/lib/ansible/callback_plugins/noop.py b/lib/ansible/callback_plugins/noop.py index 4b9bcd2381..a41ed74115 100644 --- a/lib/ansible/callback_plugins/noop.py +++ b/lib/ansible/callback_plugins/noop.py @@ -72,7 +72,7 @@ class CallbackModule(object): def playbook_on_task_start(self, name, is_conditional): 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, default=None, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None): pass def playbook_on_setup(self): diff --git a/lib/ansible/callbacks.py b/lib/ansible/callbacks.py index 1fd47b35c5..2b04a23760 100644 --- a/lib/ansible/callbacks.py +++ b/lib/ansible/callbacks.py @@ -435,7 +435,7 @@ class PlaybookCallbacks(object): print banner(msg) 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, default=None, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None): if prompt: msg = "%s: " % prompt @@ -458,10 +458,17 @@ class PlaybookCallbacks(object): else: result = prompt(msg, private) + # if result is false and default is not None + if not result and default: + result = default + + if encrypt: 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, default=default, private=private, + prompt=prompt, encrypt=encrypt, confirm=confirm, salt_size=salt_size, salt=None + ) return result diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index 06cf184c1e..dcf9f5ffea 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -189,6 +189,7 @@ class Play(object): vname = var['name'] prompt = var.get("prompt", vname) + default = var.get("default", None) private = var.get("private", True) confirm = var.get("confirm", False) @@ -197,13 +198,17 @@ class Play(object): salt = var.get("salt", None) 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, default, private, prompt, encrypt, confirm, salt_size, salt + ) elif type(self.vars_prompt) == dict: for (vname, prompt) in self.vars_prompt.iteritems(): prompt_msg = "%s: " % prompt 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, default=None, private=False, prompt=prompt_msg + ) else: raise errors.AnsibleError("'vars_prompt' section is malformed, see docs") diff --git a/plugins/callbacks/log_plays.py b/plugins/callbacks/log_plays.py index d9af3049de..440ff78df5 100644 --- a/plugins/callbacks/log_plays.py +++ b/plugins/callbacks/log_plays.py @@ -86,7 +86,7 @@ class CallbackModule(object): def playbook_on_task_start(self, name, is_conditional): 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, default=None, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None): pass def playbook_on_setup(self): diff --git a/plugins/callbacks/osx_say.py b/plugins/callbacks/osx_say.py index 9dba871a4f..b506d2dbe3 100644 --- a/plugins/callbacks/osx_say.py +++ b/plugins/callbacks/osx_say.py @@ -73,7 +73,7 @@ class CallbackModule(object): else: 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, default=None, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None): pass def playbook_on_setup(self): From 395093e0bf8b6c23f234df3a7168961be3934510 Mon Sep 17 00:00:00 2001 From: Rodney Quillo Date: Sat, 19 Jan 2013 08:34:47 +0800 Subject: [PATCH 2/2] Move default arguments to the end of the function call. --- lib/ansible/callback_plugins/noop.py | 2 +- lib/ansible/callbacks.py | 6 +++--- lib/ansible/playbook/play.py | 4 ++-- plugins/callbacks/log_plays.py | 2 +- plugins/callbacks/osx_say.py | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/ansible/callback_plugins/noop.py b/lib/ansible/callback_plugins/noop.py index a41ed74115..b0f48ef47f 100644 --- a/lib/ansible/callback_plugins/noop.py +++ b/lib/ansible/callback_plugins/noop.py @@ -72,7 +72,7 @@ class CallbackModule(object): def playbook_on_task_start(self, name, is_conditional): pass - def playbook_on_vars_prompt(self, varname, default=None, 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 def playbook_on_setup(self): diff --git a/lib/ansible/callbacks.py b/lib/ansible/callbacks.py index 2b04a23760..619613fb33 100644 --- a/lib/ansible/callbacks.py +++ b/lib/ansible/callbacks.py @@ -435,7 +435,7 @@ class PlaybookCallbacks(object): print banner(msg) call_callback_module('playbook_on_task_start', name, is_conditional) - def on_vars_prompt(self, varname, default=None, 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: msg = "%s: " % prompt @@ -466,8 +466,8 @@ class PlaybookCallbacks(object): if encrypt: result = utils.do_encrypt(result,encrypt,salt_size,salt) - call_callback_module( 'playbook_on_vars_prompt', varname, default=default, 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 diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index dcf9f5ffea..f678a15bb7 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -199,7 +199,7 @@ class Play(object): if vname not in self.playbook.extra_vars: vars[vname] = self.playbook.callbacks.on_vars_prompt ( - vname, default, private, prompt, encrypt, confirm, salt_size, salt + vname, private, prompt, encrypt, confirm, salt_size, salt, default ) elif type(self.vars_prompt) == dict: @@ -207,7 +207,7 @@ class Play(object): prompt_msg = "%s: " % prompt if vname not in self.playbook.extra_vars: vars[vname] = self.playbook.callbacks.on_vars_prompt( - varname=vname, default=None, private=False, prompt=prompt_msg + varname=vname, private=False, prompt=prompt_msg, default=None ) else: diff --git a/plugins/callbacks/log_plays.py b/plugins/callbacks/log_plays.py index 440ff78df5..3931b3411b 100644 --- a/plugins/callbacks/log_plays.py +++ b/plugins/callbacks/log_plays.py @@ -86,7 +86,7 @@ class CallbackModule(object): def playbook_on_task_start(self, name, is_conditional): pass - def playbook_on_vars_prompt(self, varname, default=None, 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 def playbook_on_setup(self): diff --git a/plugins/callbacks/osx_say.py b/plugins/callbacks/osx_say.py index b506d2dbe3..89c82ca530 100644 --- a/plugins/callbacks/osx_say.py +++ b/plugins/callbacks/osx_say.py @@ -73,7 +73,7 @@ class CallbackModule(object): else: say("Notifying task: %s" % name, REGULAR_VOICE) - def playbook_on_vars_prompt(self, varname, default=None, 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 def playbook_on_setup(self):