Skip to content

Commit 7a828e7

Browse files
authored
Merge pull request #1200 from moreati/issue1079-templated-python-interpreter
ansible_mitogen: Fix timeout in wait_for_connection with templated ansible_python_interpreter
2 parents 288b005 + 53b4881 commit 7a828e7

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

ansible_mitogen/connection.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -933,31 +933,39 @@ def reset(self):
933933
self.reset_compat_msg
934934
)
935935

936-
# Strategy's _execute_meta doesn't have an action obj but we'll need one for
937-
# running interpreter_discovery
938-
# will create a new temporary action obj for this purpose
939-
self._action = ansible_mitogen.mixins.ActionModuleMixin(
940-
task=0,
941-
connection=self,
942-
play_context=self._play_context,
943-
loader=0,
944-
templar=0,
945-
shared_loader_obj=0
946-
)
947-
948-
# Workaround for https://github.com/ansible/ansible/issues/84238
936+
# Handle templated connection variables during `meta: reset_connection`.
937+
# Many bugs/implementation details of Mitogen & Ansible collide here.
938+
# See #1079, #1096, #1132, ansible/ansible#84238, ...
949939
try:
950940
task, templar = self._play_context.vars.pop(
951941
'_mitogen.smuggled.reset_connection',
952942
)
953943
except KeyError:
954-
pass
944+
self._action_monkey_patched_by_mitogen = False
955945
else:
946+
# LOG.info('%r.reset(): remote_addr=%r', self, self._play_context.remote_addr)
947+
# ansible.plugins.strategy.StrategyBase._execute_meta() doesn't
948+
# have an action object, which we need for interpreter_discovery.
949+
# Create a temporary action object for this purpose.
950+
self._action = ansible_mitogen.mixins.ActionModuleMixin(
951+
task=task,
952+
connection=self,
953+
play_context=self._play_context,
954+
loader=templar._loader,
955+
templar=templar,
956+
shared_loader_obj=0,
957+
)
958+
self._action_monkey_patched_by_mitogen = True
959+
960+
# Workaround for https://github.com/ansible/ansible/issues/84238
956961
self.set_options(
957962
task_keys=task.dump_attrs(),
958963
var_options=self._mitogen_var_options(templar),
959964
)
960965

966+
del task
967+
del templar
968+
961969
# Clear out state in case we were ever connected.
962970
self.close()
963971

@@ -977,6 +985,11 @@ def reset(self):
977985
finally:
978986
binding.close()
979987

988+
# Cleanup any monkey patching we did for `meta: reset_connection`
989+
if self._action_monkey_patched_by_mitogen:
990+
del self._action
991+
del self._action_monkey_patched_by_mitogen
992+
980993
# Compatibility with Ansible 2.4 wait_for_connection plug-in.
981994
_reset = reset
982995

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ To avail of fixes in an unreleased version, please download a ZIP file
2121
In progress (unreleased)
2222
------------------------
2323

24+
* :gh:issue:`1079` :mod:`ansible_mitogen`: Fix :ans:mod:`wait_for_connection`
25+
timeout with templated ``ansible_python_interpreter``
2426

2527

2628
v0.3.19 (2024-12-02)

tests/ansible/hosts/default.hosts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ ssh-common-args ansible_host=localhost ansible_user="{{ lookup('pipe', 'whoami')
1919
ansible_ssh_common_args=-o PermitLocalCommand=yes -o LocalCommand="touch {{ ssh_args_canary_file }}"
2020
ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ inventory_hostname }}
2121

22+
[issue1079]
23+
wait-for-connection ansible_host=localhost ansible_user="{{ lookup('pipe', 'whoami') }}"
24+
2225
[tt_targets_bare]
2326
tt-bare
2427

tests/ansible/regression/all.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
- import_playbook: issue_776__load_plugins_called_twice.yml
1717
- import_playbook: issue_952__ask_become_pass.yml
1818
- import_playbook: issue_1066__add_host__host_key_checking.yml
19+
- import_playbook: issue_1079__wait_for_connection_timeout.yml
1920
- import_playbook: issue_1087__template_streamerror.yml
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- name: regression/issue_1079__wait_for_connection_timeout.yml
2+
hosts: issue1079
3+
gather_facts: false
4+
tasks:
5+
- name: Wait for connection at start of play
6+
wait_for_connection:
7+
timeout: 5
8+
tags:
9+
- issue_1079
10+
- wait_for_connection

tests/ansible/templates/test-targets.j2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ '{{' }} inventory_hostname {{ '}}'
4040

4141
{% set tt = containers[0] %}
4242

43+
[issue1079]
44+
wait-for-connection ansible_host={{ tt.hostname }} ansible_port={{ tt.port }} ansible_python_interpreter="{{ '{{' }} '{{ tt.python_path }}' | trim {{ '}}' }}"
45+
46+
[issue1079:vars]
47+
ansible_user=mitogen__has_sudo_nopw
48+
ansible_password=has_sudo_nopw_password
49+
4350
[tt_targets_bare]
4451
tt-bare
4552

0 commit comments

Comments
 (0)