From 4cbf048996cc6e84cb182868f32d5d1795d1fe74 Mon Sep 17 00:00:00 2001 From: Sam Doran Date: Mon, 13 Aug 2018 12:54:31 -0400 Subject: [PATCH] Only template values in vars_prompt rather than all vars (#39304) This allows the use of variables in vars_prompt fields but allows variables entered in the prompt to affect play vars rather than throwing an undefined error. Add tests for vars_prompt (cherry picked from commit 6d38167d49eb41c67d0b58061b8555cf7255705f) --- ..._prompt-allow-prompting-for-play-vars.yaml | 2 + lib/ansible/executor/playbook_executor.py | 23 ++-- lib/ansible/playbook/play.py | 2 +- test/integration/targets/vars_prompt/aliases | 1 + test/integration/targets/vars_prompt/runme.sh | 15 +++ .../targets/vars_prompt/test-vars_prompt.py | 115 ++++++++++++++++++ .../targets/vars_prompt/vars_prompt-1.yml | 15 +++ .../targets/vars_prompt/vars_prompt-2.yml | 16 +++ .../targets/vars_prompt/vars_prompt-3.yml | 17 +++ .../targets/vars_prompt/vars_prompt-4.yml | 16 +++ .../targets/vars_prompt/vars_prompt-5.yml | 14 +++ .../targets/vars_prompt/vars_prompt-6.yml | 20 +++ .../targets/vars_prompt/vars_prompt-7.yml | 12 ++ 13 files changed, 254 insertions(+), 14 deletions(-) create mode 100644 changelogs/fragments/vars_prompt-allow-prompting-for-play-vars.yaml create mode 100644 test/integration/targets/vars_prompt/aliases create mode 100755 test/integration/targets/vars_prompt/runme.sh create mode 100644 test/integration/targets/vars_prompt/test-vars_prompt.py create mode 100644 test/integration/targets/vars_prompt/vars_prompt-1.yml create mode 100644 test/integration/targets/vars_prompt/vars_prompt-2.yml create mode 100644 test/integration/targets/vars_prompt/vars_prompt-3.yml create mode 100644 test/integration/targets/vars_prompt/vars_prompt-4.yml create mode 100644 test/integration/targets/vars_prompt/vars_prompt-5.yml create mode 100644 test/integration/targets/vars_prompt/vars_prompt-6.yml create mode 100644 test/integration/targets/vars_prompt/vars_prompt-7.yml diff --git a/changelogs/fragments/vars_prompt-allow-prompting-for-play-vars.yaml b/changelogs/fragments/vars_prompt-allow-prompting-for-play-vars.yaml new file mode 100644 index 0000000000..d95857ae70 --- /dev/null +++ b/changelogs/fragments/vars_prompt-allow-prompting-for-play-vars.yaml @@ -0,0 +1,2 @@ +bugfixes: + - vars_prompt - properly template play level variables in vars_prompt (https://github.com/ansible/ansible/issues/37984) diff --git a/lib/ansible/executor/playbook_executor.py b/lib/ansible/executor/playbook_executor.py index 6c300977e2..25020c4a1c 100644 --- a/lib/ansible/executor/playbook_executor.py +++ b/lib/ansible/executor/playbook_executor.py @@ -102,16 +102,13 @@ class PlaybookExecutor: # clear any filters which may have been applied to the inventory self._inventory.remove_restriction() - # Create a temporary copy of the play here, so we can run post_validate - # on it without the templating changes affecting the original object. - # Doing this before vars_prompt to allow for using variables in prompt. + # Allow variables to be used in vars_prompt fields. all_vars = self._variable_manager.get_vars(play=play) templar = Templar(loader=self._loader, variables=all_vars) - new_play = play.copy() - new_play.post_validate(templar) + setattr(play, 'vars_prompt', templar.template(play.vars_prompt)) if play.vars_prompt: - for var in new_play.vars_prompt: + for var in play.vars_prompt: vname = var['name'] prompt = var.get("prompt", vname) default = var.get("default", None) @@ -128,17 +125,17 @@ class PlaybookExecutor: else: # we are either in --list-