Skip to content

Commit 257d602

Browse files
authored
Merge pull request #1167 from moreati/issue905
ansible_mitogen: Template `ssh_args`, `ssh_common_args`, `ssh_extra_args`
2 parents a1d079a + cdfaf31 commit 257d602

File tree

7 files changed

+62
-11
lines changed

7 files changed

+62
-11
lines changed

ansible_mitogen/transport_config.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,12 @@ def ansible_ssh_timeout(self):
506506
)
507507

508508
def ssh_args(self):
509-
local_vars = self._task_vars.get("hostvars", {}).get(self._inventory_name, {})
510509
return [
511510
mitogen.core.to_text(term)
512511
for s in (
513-
C.config.get_config_value("ssh_args", plugin_type="connection", plugin_name="ssh", variables=local_vars),
514-
C.config.get_config_value("ssh_common_args", plugin_type="connection", plugin_name="ssh", variables=local_vars),
515-
C.config.get_config_value("ssh_extra_args", plugin_type="connection", plugin_name="ssh", variables=local_vars)
512+
self._connection_option('ssh_args'),
513+
self._connection_option('ssh_common_args'),
514+
self._connection_option('ssh_extra_args'),
516515
)
517516
for term in ansible.utils.shlex.shlex_split(s or '')
518517
]

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:`905` :mod:`ansible_mitogen`: Support templated SSH command
25+
arguments (e.g. ``ansible_ssh_args``, ``ansible_ssh_extra_args``).
2426

2527

2628
v0.3.14 (2024-10-16)

tests/ansible/hosts/default.hosts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ssh-common-args ansible_host=localhost ansible_user="{{ lookup('pipe', 'whoami')
1717

1818
[issue905:vars]
1919
ansible_ssh_common_args=-o PermitLocalCommand=yes -o LocalCommand="touch {{ ssh_args_canary_file }}"
20-
ssh_args_canary_file=/tmp/ssh_args_{{ inventory_hostname }}
20+
ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ inventory_hostname }}
2121

2222
[tt_targets_bare]
2323
tt-bare

tests/ansible/integration/ssh/all.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
- import_playbook: args.yml
1+
- import_playbook: args_by_inv.yml
2+
- import_playbook: args_by_play_taskvar.yml
23
- import_playbook: config.yml
34
- import_playbook: password.yml
45
- import_playbook: timeouts.yml

tests/ansible/integration/ssh/args.yml renamed to tests/ansible/integration/ssh/args_by_inv.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
- name: integration/ssh/args.yml
1+
- name: integration/ssh/args_by_inv.yml
22
hosts: issue905
33
gather_facts: false
44
tasks:
55
# Test that ansible_ssh_common_args are templated; ansible_ssh_args &
66
# ansible_ssh_extra_args aren't directly tested, we assume they're similar.
7-
# FIXME This test currently relies on variables set in the host group.
8-
# Ideally they'd be set here, and the host group eliminated, but
9-
# Mitogen currently fails to template when defined in the play.
107
# TODO Replace LocalCommand canary with SetEnv canary, to simplify test.
118
# Requires modification of sshd_config files to add AcceptEnv ...
129
- name: Test templating of ansible_ssh_common_args et al
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
- name: integration/ssh/args_by_play_taskvar.yml
2+
hosts: tt_targets_bare
3+
gather_facts: false
4+
vars:
5+
ansible_password: "{{ 'has_sudo_nopw_password' | trim }}"
6+
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
7+
ansible_ssh_common_args: >-
8+
-o PermitLocalCommand=yes
9+
-o LocalCommand="touch {{ ssh_args_canary_file }}"
10+
ansible_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
11+
ssh_args_canary_file: "/tmp/ssh_args_by_play_taskvar_{{ inventory_hostname }}"
12+
tasks:
13+
# Test that ansible_ssh_common_args are templated; ansible_ssh_args &
14+
# ansible_ssh_extra_args aren't directly tested, we assume they're similar.
15+
# TODO Replace LocalCommand canary with SetEnv canary, to simplify test.
16+
# Requires modification of sshd_config files to add AcceptEnv ...
17+
- name: Test templating of ansible_ssh_common_args et al, by play taskvars
18+
block:
19+
- name: Ensure no lingering canary files
20+
file:
21+
path: "{{ ssh_args_canary_file }}"
22+
state: absent
23+
delegate_to: localhost
24+
25+
- name: Reset connections to force new ssh execution
26+
meta: reset_connection
27+
28+
- name: Perform SSH connection, to trigger side effect
29+
ping:
30+
31+
- name: Stat for canary file created by side effect
32+
stat:
33+
path: "{{ ssh_args_canary_file }}"
34+
delegate_to: localhost
35+
register: ssh_args_by_play_taskvar_canary_stat
36+
37+
- assert:
38+
that:
39+
- ssh_args_by_play_taskvar_canary_stat.stat.exists == true
40+
quiet: true
41+
success_msg: "Canary found: {{ ssh_args_canary_file }}"
42+
fail_msg: |
43+
ssh_args_canary_file={{ ssh_args_canary_file }}
44+
ssh_args_by_play_taskvar_canary_stat={{ ssh_args_by_play_taskvar_canary_stat }}
45+
always:
46+
- name: Cleanup canary files
47+
file:
48+
path: "{{ ssh_args_canary_file }}"
49+
state: absent
50+
delegate_to: localhost
51+
tags:
52+
- issue_905

tests/ansible/templates/test-targets.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ssh-common-args ansible_host={{ c.hostname }} ansible_port={{ c.port }} ansible_
3636
ansible_user=mitogen__has_sudo_nopw
3737
ansible_password=has_sudo_nopw_password
3838
ansible_ssh_common_args=-o PermitLocalCommand=yes -o LocalCommand="touch {{ '{{' }} ssh_args_canary_file {{ '}}' }}"
39-
ssh_args_canary_file=/tmp/ssh_args_{{ '{{' }} inventory_hostname {{ '}}' }}
39+
ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ '{{' }} inventory_hostname {{ '}}' }}
4040

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

0 commit comments

Comments
 (0)