Skip to content

Commit 88e7c56

Browse files
authored
Merge pull request #1175 from moreati/issue1083-ssh_executable
ansible_mitogen: Templated ssh executable
2 parents 8924470 + 833e284 commit 88e7c56

File tree

7 files changed

+66
-1
lines changed

7 files changed

+66
-1
lines changed

ansible_mitogen/connection.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,29 @@ def close(self):
890890
self.binding.close()
891891
self.binding = None
892892

893+
def _mitogen_var_options(self, templar):
894+
# Workaround for https://github.com/ansible/ansible/issues/84238
895+
var_names = C.config.get_plugin_vars('connection', self._load_name)
896+
variables = templar.available_variables
897+
var_options = {
898+
var_name: templar.template(variables[var_name])
899+
for var_name in var_names
900+
if var_name in variables
901+
}
902+
903+
if self.allow_extras:
904+
extras_var_prefix = 'ansible_%s_' % self.extras_prefix
905+
var_options['_extras'] = {
906+
var_name: templar.template(variables[var_name])
907+
for var_name in variables
908+
if var_name not in var_options
909+
and var_name.startswith(extras_var_prefix)
910+
}
911+
else:
912+
var_options['_extras'] = {}
913+
914+
return var_options
915+
893916
reset_compat_msg = (
894917
'Mitogen only supports "reset_connection" on Ansible 2.5.6 or later'
895918
)
@@ -922,6 +945,19 @@ def reset(self):
922945
shared_loader_obj=0
923946
)
924947

948+
# Workaround for https://github.com/ansible/ansible/issues/84238
949+
try:
950+
task, templar = self._play_context.vars.pop(
951+
'_mitogen.smuggled.reset_connection',
952+
)
953+
except KeyError:
954+
pass
955+
else:
956+
self.set_options(
957+
task_keys=task.dump_attrs(),
958+
var_options=self._mitogen_var_options(templar),
959+
)
960+
925961
# Clear out state in case we were ever connected.
926962
self.close()
927963

ansible_mitogen/strategy.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import ansible_mitogen.process
4646

4747
import ansible.executor.process.worker
48+
import ansible.template
4849
import ansible.utils.sentinel
4950

5051

@@ -326,3 +327,24 @@ def run(self, iterator, play_context, result=0):
326327
self._worker_model.on_strategy_complete()
327328
finally:
328329
ansible_mitogen.process.set_worker_model(None)
330+
331+
def _smuggle_to_connction_reset(self, task, play_context, iterator, target_host):
332+
# Workaround for https://github.com/ansible/ansible/issues/84238
333+
variables = self._variable_manager.get_vars(
334+
play=iterator._play, host=target_host, task=task,
335+
_hosts=self._hosts_cache, _hosts_all=self._hosts_cache_all,
336+
)
337+
templar = ansible.template.Templar(
338+
loader=self._loader, variables=variables,
339+
)
340+
play_context.vars.update({
341+
'_mitogen.smuggled.reset_connection': (task, templar),
342+
})
343+
344+
def _execute_meta(self, task, play_context, iterator, target_host):
345+
if task.args['_raw_params'] == 'reset_connection':
346+
self._smuggle_to_connction_reset(task, play_context, iterator, target_host)
347+
348+
return super(StrategyMixin, self)._execute_meta(
349+
task, play_context, iterator, target_host,
350+
)

ansible_mitogen/transport_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def private_key_file(self):
511511
return self._play_context.private_key_file
512512

513513
def ssh_executable(self):
514-
return C.config.get_config_value("ssh_executable", plugin_type="connection", plugin_name="ssh", variables=self._task_vars.get("vars", {}))
514+
return self._connection_option('ssh_executable')
515515

516516
def timeout(self):
517517
return self._play_context.timeout

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ In progress (unreleased)
2525
(e.g. ``become_exe``).
2626
* :gh:issue:`1083` :mod:`ansible_mitogen`: Templated become executable
2727
arguments (e.g. ``become_flags``).
28+
* :gh:issue:`1083` :mod:`ansible_mitogen`: Templated ssh executable
29+
(``ansible_ssh_executable``).
30+
* :gh:issue:`1083` :mod:`ansible_mitogen`: Fixed templated connection options
31+
during a ``meta: reset_connection`` task.
2832

2933

3034
v0.3.15 (2024-10-28)

tests/ansible/hosts/default.hosts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ ansible_user="{{ lookup('pipe', 'whoami') }}"
4646
tt-password ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
4747
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw
4848
tt-remote-user ansible_password=has_sudo_nopw_password ansible_user="{{ 'mitogen__has_sudo_nopw' | trim }}"
49+
tt-ssh-executable ansible_password=has_sudo_nopw_password ansible_ssh_executable="{{ 'ssh' | trim }}" ansible_user=mitogen__has_sudo_nopw
4950

5051
[tt_targets_inventory:vars]
5152
ansible_host=localhost

tests/ansible/integration/ssh/templated_by_play_taskvar.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
vars:
55
ansible_password: "{{ 'has_sudo_nopw_password' | trim }}"
66
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
7+
ansible_ssh_executable: "{{ 'ssh' | trim }}"
78
ansible_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
89

910
tasks:

tests/ansible/templates/test-targets.j2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ ansible_user=mitogen__has_sudo_nopw
7474
tt-password ansible_password="{{ '{{' }} 'has_sudo_nopw_password' | trim {{ '}}' }}" ansible_port={{ tt.port }} ansible_user=mitogen__has_sudo_nopw
7575
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ '{{' }} {{ tt.port }} | int {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw
7676
tt-remote-user ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_user="{{ '{{' }} 'mitogen__has_sudo_nopw' | trim {{ '}}' }}"
77+
tt-ssh-executable ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_ssh_executable="{{ '{{' }} 'ssh' | trim {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw
7778

7879
[tt_targets_inventory:vars]
7980
ansible_host={{ tt.hostname }}

0 commit comments

Comments
 (0)