Skip to content

Commit

Permalink
template connection variables accessed directly before using (ansible…
Browse files Browse the repository at this point in the history
…#70657)

* template variables accessed directly when using them instead of FieldAttributes
  • Loading branch information
s-hertel authored Jul 16, 2020
1 parent 055871c commit 8c213c9
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/ansible/executor/task_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,10 @@ def _get_connection(self, variables, templar):
cvars = variables

# 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'
# (default value, will chose between ssh and paramiko) and 'persistent'
Expand All @@ -819,8 +822,16 @@ def _get_connection(self, variables, templar):
raise AnsibleError("the connection plugin '%s' was not found" % conn_type)

# load become plugin if needed
if boolean(cvars.get('ansible_become', self._task.become)):
become_plugin = self._get_become(cvars.get('ansible_become_method', self._task.become_method))
if cvars.get('ansible_become') is not None:
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:
connection.set_become_plugin(become_plugin)
Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/var_templating/runme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ ansible-playbook undall.yml -i inventory -v "$@"

# test hostvars templating
ansible-playbook task_vars_templating.yml -v "$@"

ansible-playbook test_connection_vars.yml -v "$@" 2>&1 | grep 'sudo'
26 changes: 26 additions & 0 deletions test/integration/targets/var_templating/test_connection_vars.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/integration/targets/var_templating/vars/connection.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ansible_become: "{{ my_var.become }}"
ansible_become_method: "{{ my_var.become_method }}"
ansible_connection: "{{ my_var.connection }}"

0 comments on commit 8c213c9

Please sign in to comment.