ansible_mitogen: Support templated SSH port

fixes #978
pull/1145/head
Alex Willmer 1 month ago
parent fb76f2eeea
commit 77a01ff8d6

@ -464,7 +464,7 @@ class PlayContextSpec(Spec):
return optional_secret(self._connection_option('password')) return optional_secret(self._connection_option('password'))
def port(self): def port(self):
return self._play_context.port return self._connection_option('port')
def python_path(self, rediscover_python=False): def python_path(self, rediscover_python=False):
s = self._connection.get_task_var('ansible_python_interpreter') s = self._connection.get_task_var('ansible_python_interpreter')

@ -25,6 +25,7 @@ In Progress (unreleased)
GitHub Actions GitHub Actions
* :gh:issue:`1116` :mod:`ansible_mitogen`: Support for templated variable * :gh:issue:`1116` :mod:`ansible_mitogen`: Support for templated variable
`ansible_ssh_user`. `ansible_ssh_user`.
* :gh:issue:`978` :mod:`ansible_mitogen`: Support templated Ansible SSH port.
v0.3.11 (2024-10-07) v0.3.11 (2024-10-07)

@ -28,6 +28,7 @@ ansible_host=localhost
[tt_targets_inventory] [tt_targets_inventory]
tt-password ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw tt-password ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw
tt-remote-user ansible_password=has_sudo_nopw_password ansible_user="{{ 'mitogen__has_sudo_nopw' | trim }}" tt-remote-user ansible_password=has_sudo_nopw_password ansible_user="{{ 'mitogen__has_sudo_nopw' | trim }}"
[tt_targets_inventory:vars] [tt_targets_inventory:vars]

@ -0,0 +1,18 @@
# Ansible removed its default SSH port in May 2021, defering to the SSH
# implementation.
# https://github.com/ansible/ansible/commit/45618a6f3856f7332df8afe4adc40d85649a70da
# Careful templating is needed to preseve the type(s) of expected_ssh_port,
# particularly in combination with the assert_equal action plugin.
# Do: {{ expected_ssh_port }}
# Don't: {{ expected_ssh_port | any_filter }}
# Don't: {% if ...%}{{ expected_ssh_port }}{% else %}...{% endif %}
# https://stackoverflow.com/questions/66102524/ansible-set-fact-type-cast/66104814#66104814
- set_fact:
expected_ssh_port: null
when: ansible_version.full is version('2.11.1', '>=', strict=True)
- set_fact:
expected_ssh_port: 22
when: ansible_version.full is version('2.11.1', '<', strict=True)

@ -15,6 +15,7 @@
gather_facts: no gather_facts: no
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- meta: end_play - meta: end_play
when: when:
@ -66,7 +67,7 @@
'keepalive_interval': 30, 'keepalive_interval': 30,
'keepalive_count': 10, 'keepalive_count': 10,
'password': null, 'password': null,
'port': null, 'port': '{{ expected_ssh_port }}',
'python_path': ['python3000'], 'python_path': ['python3000'],
'remote_name': null, 'remote_name': null,
'ssh_args': [ 'ssh_args': [

@ -55,6 +55,7 @@
- hosts: cd-normal - hosts: cd-normal
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- mitogen_get_stack: - mitogen_get_stack:
delegate_to: cd-alias delegate_to: cd-alias
register: out register: out
@ -72,7 +73,7 @@
'keepalive_interval': 30, 'keepalive_interval': 30,
'keepalive_count': 10, 'keepalive_count': 10,
'password': null, 'password': null,
'port': null, 'port': '{{ expected_ssh_port }}',
"python_path": ["python3000"], "python_path": ["python3000"],
'remote_name': null, 'remote_name': null,
'ssh_args': [ 'ssh_args': [
@ -98,6 +99,7 @@
- hosts: cd-alias - hosts: cd-alias
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- mitogen_get_stack: - mitogen_get_stack:
register: out register: out
- assert_equal: - assert_equal:
@ -114,7 +116,7 @@
'keepalive_interval': 30, 'keepalive_interval': 30,
'keepalive_count': 10, 'keepalive_count': 10,
'password': null, 'password': null,
'port': null, 'port': '{{ expected_ssh_port }}',
"python_path": ["python3000"], "python_path": ["python3000"],
'remote_name': null, 'remote_name': null,
'ssh_args': [ 'ssh_args': [
@ -140,6 +142,7 @@
- hosts: cd-normal-normal - hosts: cd-normal-normal
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- mitogen_get_stack: - mitogen_get_stack:
register: out register: out
- assert_equal: - assert_equal:
@ -167,7 +170,7 @@
'keepalive_interval': 30, 'keepalive_interval': 30,
'keepalive_count': 10, 'keepalive_count': 10,
'password': null, 'password': null,
'port': null, 'port': '{{ expected_ssh_port }}',
"python_path": ["python3000"], "python_path": ["python3000"],
'remote_name': null, 'remote_name': null,
'ssh_args': [ 'ssh_args': [
@ -193,6 +196,7 @@
- hosts: cd-normal-alias - hosts: cd-normal-alias
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- mitogen_get_stack: - mitogen_get_stack:
register: out register: out
- assert_equal: - assert_equal:
@ -237,7 +241,7 @@
'keepalive_interval': 30, 'keepalive_interval': 30,
'keepalive_count': 10, 'keepalive_count': 10,
'password': null, 'password': null,
'port': null, 'port': '{{ expected_ssh_port }}',
"python_path": ["python3000"], "python_path": ["python3000"],
'remote_name': null, 'remote_name': null,
'ssh_args': [ 'ssh_args': [
@ -262,6 +266,7 @@
- hosts: cd-newuser-normal-normal - hosts: cd-newuser-normal-normal
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- mitogen_get_stack: - mitogen_get_stack:
register: out register: out
- assert_equal: - assert_equal:
@ -289,7 +294,7 @@
'keepalive_interval': 30, 'keepalive_interval': 30,
'keepalive_count': 10, 'keepalive_count': 10,
'password': null, 'password': null,
'port': null, 'port': '{{ expected_ssh_port }}',
"python_path": ["python3000"], "python_path": ["python3000"],
'remote_name': null, 'remote_name': null,
'ssh_args': [ 'ssh_args': [
@ -315,6 +320,7 @@
- hosts: cd-newuser-normal-normal - hosts: cd-newuser-normal-normal
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- mitogen_get_stack: - mitogen_get_stack:
delegate_to: cd-alias delegate_to: cd-alias
register: out register: out
@ -332,7 +338,7 @@
'keepalive_interval': 30, 'keepalive_interval': 30,
'keepalive_count': 10, 'keepalive_count': 10,
'password': null, 'password': null,
'port': null, 'port': '{{ expected_ssh_port }}',
"python_path": ["python3000"], "python_path": ["python3000"],
'remote_name': null, 'remote_name': null,
'ssh_args': [ 'ssh_args': [

@ -3,6 +3,7 @@
gather_facts: false gather_facts: false
vars: vars:
ansible_password: "{{ 'has_sudo_nopw_password' | trim }}" ansible_password: "{{ 'has_sudo_nopw_password' | trim }}"
ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}"
ansible_user: "{{ 'mitogen__has_sudo_nopw' | trim }}" ansible_user: "{{ 'mitogen__has_sudo_nopw' | trim }}"
tasks: tasks:

@ -6,13 +6,15 @@
hosts: tc-port-unset hosts: tc-port-unset
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- {mitogen_get_stack: {}, register: out} - {mitogen_get_stack: {}, register: out}
- assert: - assert:
that: that:
- out.result|length == 1 - out.result|length == 1
- out.result[0].method == "ssh" - out.result[0].method == "ssh"
- out.result[0].kwargs.port == None - out.result[0].kwargs.port == expected_ssh_port
fail_msg: | fail_msg: |
expected_ssh_port={{ expected_ssh_port }}
out={{ out }} out={{ out }}
tags: tags:
- mitogen_only - mitogen_only
@ -54,6 +56,7 @@
vars: {mitogen_via: tc-port-explicit-ssh} vars: {mitogen_via: tc-port-explicit-ssh}
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- {mitogen_get_stack: {}, register: out} - {mitogen_get_stack: {}, register: out}
- assert: - assert:
that: that:
@ -61,8 +64,9 @@
- out.result[0].method == "ssh" - out.result[0].method == "ssh"
- out.result[0].kwargs.port == 4321 - out.result[0].kwargs.port == 4321
- out.result[1].method == "ssh" - out.result[1].method == "ssh"
- out.result[1].kwargs.port == None - out.result[1].kwargs.port == expected_ssh_port
fail_msg: | fail_msg: |
expected_ssh_port={{ expected_ssh_port }}
out={{ out }} out={{ out }}
tags: tags:
- mitogen_only - mitogen_only
@ -86,6 +90,7 @@
vars: {mitogen_via: tc-port-explicit-port} vars: {mitogen_via: tc-port-explicit-port}
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- {mitogen_get_stack: {}, register: out} - {mitogen_get_stack: {}, register: out}
- assert: - assert:
that: that:
@ -93,8 +98,9 @@
- out.result[0].method == "ssh" - out.result[0].method == "ssh"
- out.result[0].kwargs.port == 1234 - out.result[0].kwargs.port == 1234
- out.result[1].method == "ssh" - out.result[1].method == "ssh"
- out.result[1].kwargs.port == None - out.result[1].kwargs.port == expected_ssh_port
fail_msg: | fail_msg: |
expected_ssh_port={{ expected_ssh_port }}
out={{ out }} out={{ out }}
tags: tags:
- mitogen_only - mitogen_only
@ -119,6 +125,7 @@
vars: {mitogen_via: tc-port-both} vars: {mitogen_via: tc-port-both}
tasks: tasks:
- include_tasks: ../_mitogen_only.yml - include_tasks: ../_mitogen_only.yml
- include_tasks: ../_expected_ssh_port.yml
- {mitogen_get_stack: {}, register: out} - {mitogen_get_stack: {}, register: out}
- assert: - assert:
that: that:
@ -126,8 +133,9 @@
- out.result[0].method == "ssh" - out.result[0].method == "ssh"
- out.result[0].kwargs.port == 1532 - out.result[0].kwargs.port == 1532
- out.result[1].method == "ssh" - out.result[1].method == "ssh"
- out.result[1].kwargs.port == None - out.result[1].kwargs.port == expected_ssh_port
fail_msg: | fail_msg: |
expected_ssh_port={{ expected_ssh_port }}
out={{ out }} out={{ out }}
tags: tags:
- mitogen_only - mitogen_only

@ -50,6 +50,7 @@ ansible_python_interpreter={{ tt.python_path }}
[tt_targets_inventory] [tt_targets_inventory]
tt-password ansible_password="{{ '{{' }} 'has_sudo_nopw_password' | trim {{ '}}' }}" ansible_port={{ tt.port }} ansible_user=mitogen__has_sudo_nopw tt-password ansible_password="{{ '{{' }} 'has_sudo_nopw_password' | trim {{ '}}' }}" ansible_port={{ tt.port }} ansible_user=mitogen__has_sudo_nopw
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ '{{' }} {{ tt.port }} | int {{ '}}' }}" ansible_user=mitogen__has_sudo_nopw
tt-remote-user ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_user="{{ '{{' }} 'mitogen__has_sudo_nopw' | trim {{ '}}' }}" tt-remote-user ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_user="{{ '{{' }} 'mitogen__has_sudo_nopw' | trim {{ '}}' }}"
[tt_targets_inventory:vars] [tt_targets_inventory:vars]

Loading…
Cancel
Save