Skip to content
This repository has been archived by the owner on Nov 12, 2024. It is now read-only.

Commit

Permalink
Fix delegate_facts with interpreter not being set (ansible#70293)
Browse files Browse the repository at this point in the history
Fixes ansible#70168

ci_complete

Co-authored-by: Brian Coca <[email protected]>
Co-authored-by: Matt Clay <[email protected]>
  • Loading branch information
3 people authored and ciis0 committed May 17, 2021
1 parent ae331ea commit e076bde
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "Fix ``delegate_facts: true`` when ``ansible_python_interpreter`` is not set. (https://github.com/ansible/ansible/issues/70168)"
14 changes: 4 additions & 10 deletions lib/ansible/plugins/action/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,11 @@ def _remote_file_exists(self, path):
return True
return False

def _configure_module(self, module_name, module_args, task_vars=None):
def _configure_module(self, module_name, module_args, task_vars):
'''
Handles the loading and templating of the module code through the
modify_module() function.
'''

if task_vars is None:
use_vars = dict()

if self._task.delegate_to:
use_vars = task_vars.get('ansible_delegated_vars')[self._task.delegate_to]
else:
Expand Down Expand Up @@ -219,20 +215,18 @@ def _configure_module(self, module_name, module_args, task_vars=None):
# we'll propagate back to the controller in the task result
discovered_key = 'discovered_interpreter_%s' % idre.interpreter_name

# update the local vars copy for the retry
use_vars['ansible_facts'][discovered_key] = self._discovered_interpreter

# TODO: this condition prevents 'wrong host' from being updated
# but in future we would want to be able to update 'delegated host facts'
# irrespective of task settings
if not self._task.delegate_to or self._task.delegate_facts:
# store in local task_vars facts collection for the retry and any other usages in this worker
if use_vars.get('ansible_facts') is None:
task_vars['ansible_facts'] = {}
task_vars['ansible_facts'][discovered_key] = self._discovered_interpreter
# preserve this so _execute_module can propagate back to controller as a fact
self._discovered_interpreter_key = discovered_key
else:
task_vars['ansible_delegated_vars'][self._task.delegate_to]
if task_vars['ansible_delegated_vars'][self._task.delegate_to].get('ansible_facts') is None:
task_vars['ansible_delegated_vars'][self._task.delegate_to]['ansible_facts'] = {}
task_vars['ansible_delegated_vars'][self._task.delegate_to]['ansible_facts'][discovered_key] = self._discovered_interpreter

return (module_style, module_shebang, module_data, module_path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
shippable/posix/group1
non_local # this test requires interpreter discovery, which means code coverage must be disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- hosts: localhost
gather_facts: no
tasks:
- name: Test python interpreter discovery with delegate_to without delegate_facts
ping:
delegate_to: testhost
- name: Test python interpreter discovery with delegate_to with delegate_facts
ping:
delegate_to: testhost
delegate_facts: yes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[local]
testhost ansible_connection=local ansible_python_interpreter=auto # interpreter discovery required
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

set -eux

ansible-playbook delegate_facts.yml -i inventory "$@"
6 changes: 3 additions & 3 deletions test/units/plugins/action/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,19 @@ def mock_find_plugin(name, options, collection_list=None):
self.assertEqual(shebang, u"#!/usr/bin/python")

# test module not found
self.assertRaises(AnsibleError, action_base._configure_module, 'badmodule', mock_task.args)
self.assertRaises(AnsibleError, action_base._configure_module, 'badmodule', mock_task.args, {})

# test powershell module formatting
with patch.object(builtins, 'open', mock_open(read_data=to_bytes(powershell_module_replacers.strip(), encoding='utf-8'))):
mock_task.action = 'win_copy'
mock_task.args = dict(b=2)
mock_connection.module_implementation_preferences = ('.ps1',)
(style, shebang, data, path) = action_base._configure_module('stat', mock_task.args)
(style, shebang, data, path) = action_base._configure_module('stat', mock_task.args, {})
self.assertEqual(style, "new")
self.assertEqual(shebang, u'#!powershell')

# test module not found
self.assertRaises(AnsibleError, action_base._configure_module, 'badmodule', mock_task.args)
self.assertRaises(AnsibleError, action_base._configure_module, 'badmodule', mock_task.args, {})

def test_action_base__compute_environment_string(self):
fake_loader = DictDataLoader({
Expand Down

0 comments on commit e076bde

Please sign in to comment.