Skip to content

Commit

Permalink
Merge pull request #1200 from moreati/issue1079-templated-python-inte…
Browse files Browse the repository at this point in the history
…rpreter

ansible_mitogen: Fix timeout in wait_for_connection with templated ansible_python_interpreter
  • Loading branch information
moreati authored Dec 9, 2024
2 parents 288b005 + 53b4881 commit 7a828e7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
41 changes: 27 additions & 14 deletions ansible_mitogen/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,31 +933,39 @@ def reset(self):
self.reset_compat_msg
)

# Strategy's _execute_meta doesn't have an action obj but we'll need one for
# running interpreter_discovery
# will create a new temporary action obj for this purpose
self._action = ansible_mitogen.mixins.ActionModuleMixin(
task=0,
connection=self,
play_context=self._play_context,
loader=0,
templar=0,
shared_loader_obj=0
)

# Workaround for https://github.com/ansible/ansible/issues/84238
# Handle templated connection variables during `meta: reset_connection`.
# Many bugs/implementation details of Mitogen & Ansible collide here.
# See #1079, #1096, #1132, ansible/ansible#84238, ...
try:
task, templar = self._play_context.vars.pop(
'_mitogen.smuggled.reset_connection',
)
except KeyError:
pass
self._action_monkey_patched_by_mitogen = False
else:
# LOG.info('%r.reset(): remote_addr=%r', self, self._play_context.remote_addr)
# ansible.plugins.strategy.StrategyBase._execute_meta() doesn't
# have an action object, which we need for interpreter_discovery.
# Create a temporary action object for this purpose.
self._action = ansible_mitogen.mixins.ActionModuleMixin(
task=task,
connection=self,
play_context=self._play_context,
loader=templar._loader,
templar=templar,
shared_loader_obj=0,
)
self._action_monkey_patched_by_mitogen = True

# Workaround for https://github.com/ansible/ansible/issues/84238
self.set_options(
task_keys=task.dump_attrs(),
var_options=self._mitogen_var_options(templar),
)

del task
del templar

# Clear out state in case we were ever connected.
self.close()

Expand All @@ -977,6 +985,11 @@ def reset(self):
finally:
binding.close()

# Cleanup any monkey patching we did for `meta: reset_connection`
if self._action_monkey_patched_by_mitogen:
del self._action
del self._action_monkey_patched_by_mitogen

# Compatibility with Ansible 2.4 wait_for_connection plug-in.
_reset = reset

Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ To avail of fixes in an unreleased version, please download a ZIP file
In progress (unreleased)
------------------------

* :gh:issue:`1079` :mod:`ansible_mitogen`: Fix :ans:mod:`wait_for_connection`
timeout with templated ``ansible_python_interpreter``


v0.3.19 (2024-12-02)
Expand Down
3 changes: 3 additions & 0 deletions tests/ansible/hosts/default.hosts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ ssh-common-args ansible_host=localhost ansible_user="{{ lookup('pipe', 'whoami')
ansible_ssh_common_args=-o PermitLocalCommand=yes -o LocalCommand="touch {{ ssh_args_canary_file }}"
ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ inventory_hostname }}

[issue1079]
wait-for-connection ansible_host=localhost ansible_user="{{ lookup('pipe', 'whoami') }}"

[tt_targets_bare]
tt-bare

Expand Down
1 change: 1 addition & 0 deletions tests/ansible/regression/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
- import_playbook: issue_776__load_plugins_called_twice.yml
- import_playbook: issue_952__ask_become_pass.yml
- import_playbook: issue_1066__add_host__host_key_checking.yml
- import_playbook: issue_1079__wait_for_connection_timeout.yml
- import_playbook: issue_1087__template_streamerror.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- name: regression/issue_1079__wait_for_connection_timeout.yml
hosts: issue1079
gather_facts: false
tasks:
- name: Wait for connection at start of play
wait_for_connection:
timeout: 5
tags:
- issue_1079
- wait_for_connection
7 changes: 7 additions & 0 deletions tests/ansible/templates/test-targets.j2
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ '{{' }} inventory_hostname {{ '}}'

{% set tt = containers[0] %}

[issue1079]
wait-for-connection ansible_host={{ tt.hostname }} ansible_port={{ tt.port }} ansible_python_interpreter="{{ '{{' }} '{{ tt.python_path }}' | trim {{ '}}' }}"

[issue1079:vars]
ansible_user=mitogen__has_sudo_nopw
ansible_password=has_sudo_nopw_password

[tt_targets_bare]
tt-bare

Expand Down

0 comments on commit 7a828e7

Please sign in to comment.