[2.10] template connection variables accessed directly before using (#70657) (#70688)

* template connection variables accessed directly before using (#70657)

* template variables accessed directly when using them instead of FieldAttributes

(cherry picked from commit 8c213c9334)

* changelog
This commit is contained in:
Sloane Hertel 2020-07-21 14:19:52 -04:00 committed by GitHub
parent ed07821a59
commit d329985d4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 3 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Template connection variables before using them (https://github.com/ansible/ansible/issues/70598).

View file

@ -889,7 +889,10 @@ class TaskExecutor:
cvars = variables cvars = variables
# use magic var if it exists, if not, let task inheritance do it's thing. # use magic var if it exists, if not, let task inheritance do it's thing.
self._play_context.connection = cvars.get('ansible_connection', self._task.connection) if cvars.get('ansible_connection') is not None:
self._play_context.connection = templar.template(cvars['ansible_connection'])
else:
self._play_context.connection = self._task.connection
# TODO: play context has logic to update the conneciton for 'smart' # TODO: play context has logic to update the conneciton for 'smart'
# (default value, will chose between ssh and paramiko) and 'persistent' # (default value, will chose between ssh and paramiko) and 'persistent'
@ -910,8 +913,16 @@ class TaskExecutor:
raise AnsibleError("the connection plugin '%s' was not found" % conn_type) raise AnsibleError("the connection plugin '%s' was not found" % conn_type)
# load become plugin if needed # load become plugin if needed
if boolean(cvars.get('ansible_become', self._task.become)): if cvars.get('ansible_become') is not None:
become_plugin = self._get_become(cvars.get('ansible_become_method', self._task.become_method)) become = boolean(templar.template(cvars['ansible_become']))
else:
become = self._task.become
if become:
if cvars.get('ansible_become_method'):
become_plugin = self._get_become(templar.template(cvars['ansible_become_method']))
else:
become_plugin = self._get_become(self._task.become_method)
try: try:
connection.set_become_plugin(become_plugin) connection.set_become_plugin(become_plugin)

View file

@ -13,3 +13,5 @@ ansible-playbook undall.yml -i inventory -v "$@"
# test hostvars templating # test hostvars templating
ansible-playbook task_vars_templating.yml -v "$@" ansible-playbook task_vars_templating.yml -v "$@"
ansible-playbook test_connection_vars.yml -v "$@" 2>&1 | grep 'sudo'

View file

@ -0,0 +1,26 @@
---
- hosts: localhost
gather_facts: no
vars:
my_var:
become_method: sudo
connection: local
become: 1
tasks:
- include_vars: "./vars/connection.yml"
- command: whoami
ignore_errors: yes
register: result
failed_when: result is not success and (result.module_stderr is defined or result.module_stderr is defined)
- assert:
that:
- "'sudo' in result.module_stderr"
when: result is not success and result.module_stderr is defined
- assert:
that:
- "'Invalid become method specified' not in result.msg"
when: result is not success and result.msg is defined

View file

@ -0,0 +1,3 @@
ansible_become: "{{ my_var.become }}"
ansible_become_method: "{{ my_var.become_method }}"
ansible_connection: "{{ my_var.connection }}"