diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py index 650355ff..e4de34f1 100644 --- a/ansible_mitogen/transport_config.py +++ b/ansible_mitogen/transport_config.py @@ -442,18 +442,22 @@ class PlayContextSpec(Spec): raise LOG.info( - 'Used PlayContext fallback for plugin=%r, option=%r', - self._connection, name, + 'Used fallback=PlayContext.%s for plugin=%r, option=%r', + name, self._connection, name, ) return getattr(self._play_context, name) - - def _connection_option(self, name): + def _connection_option(self, name, fallback_attr=None): try: return self._connection.get_option(name, hostvars=self._task_vars) except KeyError: - LOG.debug('Used PlayContext fallback for option=%r', name) - return getattr(self._play_context, name) + if fallback_attr is None: + fallback_attr = name + LOG.info( + 'Used fallback=PlayContext.%s for plugin=%r, option=%r', + fallback_attr, self._connection, name, + ) + return getattr(self._play_context, fallback_attr) def transport(self): return self._transport @@ -462,7 +466,7 @@ class PlayContextSpec(Spec): return self._inventory_name def remote_addr(self): - return self._play_context.remote_addr + return self._connection_option('host', fallback_attr='remote_addr') def remote_user(self): return self._connection_option('remote_user') diff --git a/tests/ansible/hosts/default.hosts b/tests/ansible/hosts/default.hosts index f16a4427..6bb21fc6 100644 --- a/tests/ansible/hosts/default.hosts +++ b/tests/ansible/hosts/default.hosts @@ -22,9 +22,6 @@ ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ inventory_hostname }} [tt_targets_bare] tt-bare -[tt_targets_bare:vars] -ansible_host=localhost - [tt_become_bare] tt-become-bare @@ -43,6 +40,7 @@ ansible_host=localhost ansible_user="{{ lookup('pipe', 'whoami') }}" [tt_targets_inventory] +tt-host ansible_host="{{ 'localhost' | trim }}" ansible_password=has_sudo_nopw_password ansible_user=mitogen__has_sudo_nopw tt-host-key-checking ansible_host=localhost ansible_host_key_checking="{{ 'false' | trim }}" ansible_password=has_sudo_nopw_password ansible_user=mitogen__has_sudo_nopw tt-password ansible_host=localhost ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw tt-port ansible_host=localhost ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw diff --git a/tests/ansible/integration/ssh/args_by_play_taskvar.yml b/tests/ansible/integration/ssh/args_by_play_taskvar.yml index 5ae83f2a..073265b2 100644 --- a/tests/ansible/integration/ssh/args_by_play_taskvar.yml +++ b/tests/ansible/integration/ssh/args_by_play_taskvar.yml @@ -2,6 +2,7 @@ hosts: tt_targets_bare gather_facts: false vars: + ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}" ansible_password: "{{ 'has_sudo_nopw_password' | trim }}" ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}" ansible_ssh_common_args: >- diff --git a/tests/ansible/integration/ssh/templated_by_play_keyword.yml b/tests/ansible/integration/ssh/templated_by_play_keyword.yml index e66cc5f3..c6ff1674 100644 --- a/tests/ansible/integration/ssh/templated_by_play_keyword.yml +++ b/tests/ansible/integration/ssh/templated_by_play_keyword.yml @@ -3,6 +3,7 @@ gather_facts: false remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}" vars: + ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}" ansible_password: has_sudo_nopw_password ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}" tasks: diff --git a/tests/ansible/integration/ssh/templated_by_play_taskvar.yml b/tests/ansible/integration/ssh/templated_by_play_taskvar.yml index d3ae6117..151aa614 100644 --- a/tests/ansible/integration/ssh/templated_by_play_taskvar.yml +++ b/tests/ansible/integration/ssh/templated_by_play_taskvar.yml @@ -2,6 +2,7 @@ hosts: tt_targets_bare gather_facts: false vars: + ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}" ansible_password: "{{ 'has_sudo_nopw_password' | trim }}" ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}" ansible_ssh_executable: "{{ 'ssh' | trim }}" @@ -16,6 +17,7 @@ hosts: tt_targets_bare gather_facts: false vars: + ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}" ansible_private_key_file: "{{ git_basedir }}/tests/data/docker/mitogen__has_sudo_pubkey.key" ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}" ansible_ssh_executable: "{{ 'ssh' | trim }}" diff --git a/tests/ansible/integration/ssh/templated_by_task_keyword.yml b/tests/ansible/integration/ssh/templated_by_task_keyword.yml index df956af5..dc16205a 100644 --- a/tests/ansible/integration/ssh/templated_by_task_keyword.yml +++ b/tests/ansible/integration/ssh/templated_by_task_keyword.yml @@ -5,6 +5,7 @@ # https://github.com/mitogen-hq/mitogen/issues/1132 remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}" vars: + ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}" ansible_password: has_sudo_nopw_password ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}" tasks: @@ -19,6 +20,7 @@ delegate_to: "{{ groups.tt_targets_bare[0] }}" remote_user: "{{ 'mitogen__has_sudo_nopw' | trim }}" vars: + ansible_host: "{{ hostvars[groups['test-targets'][0]].host | default('localhost') }}" ansible_password: has_sudo_nopw_password ansible_port: "{{ hostvars[groups['test-targets'][0]].ansible_port | default(22) }}" ping: diff --git a/tests/ansible/templates/test-targets.j2 b/tests/ansible/templates/test-targets.j2 index 2df20613..cebf7067 100644 --- a/tests/ansible/templates/test-targets.j2 +++ b/tests/ansible/templates/test-targets.j2 @@ -44,7 +44,6 @@ ssh_args_canary_file=/tmp/ssh_args_by_inv_{{ '{{' }} inventory_hostname {{ '}}' tt-bare [tt_targets_bare:vars] -ansible_host={{ tt.hostname }} ansible_python_interpreter={{ tt.python_path }} [tt_become_bare] @@ -71,6 +70,7 @@ ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw [tt_targets_inventory] +tt-host ansible_host="{{ '{{' }} '{{ tt.hostname }}' | trim {{ '}}' }}" ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw tt-host-key-checking ansible_host={{ tt.hostname }} ansible_host_key_checking="{{ '{{' }} 'false' | trim {{ '}}' }}" ansible_password=has_sudo_nopw_password ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw tt-password ansible_host={{ tt.hostname }} ansible_password="{{ '{{' }} 'has_sudo_nopw_password' | trim {{ '}}' }}" ansible_port={{ tt.port }} ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw tt-port ansible_host={{ tt.hostname }} ansible_password=has_sudo_nopw_password ansible_port="{{ '{{' }} {{ tt.port }} | int {{ '}}' }}" ansible_python_interpreter={{ tt.python_path }} ansible_user=mitogen__has_sudo_nopw