diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py index 511cba5f..c9aa9860 100644 --- a/ansible_mitogen/transport_config.py +++ b/ansible_mitogen/transport_config.py @@ -447,7 +447,9 @@ class MitogenViaSpec(Spec): return self._inventory_name def remote_addr(self): + # play_context.py::MAGIC_VARIABLE_MAPPING return ( + self._host_vars.get('ansible_ssh_host') or self._host_vars.get('ansible_host') or self._inventory_name ) diff --git a/tests/ansible/hosts/transport_config.hosts b/tests/ansible/hosts/transport_config.hosts index ac046dd5..18cac116 100644 --- a/tests/ansible/hosts/transport_config.hosts +++ b/tests/ansible/hosts/transport_config.hosts @@ -3,7 +3,7 @@ # tansport() -tc-transport-unset +tc-transport-unset tc-transport-local ansible_connection=local # python_path() @@ -11,3 +11,11 @@ tc-python-path-unset tc-python-path-hostvar ansible_python_interpreter=/hostvar/path/to/python tc-python-path-local-unset ansible_connection=local tc-python-path-local-explicit ansible_connection=local ansible_python_interpreter=/a/b/c + +# remote_addr() +tc-remote-addr-unset # defaults to inventory_hostname +tc-remote-addr-explicit-ssh ansible_ssh_host=ansi.ssh.host +tc-remote-addr-explicit-host ansible_host=ansi.host +tc-remote-addr-explicit-both ansible_ssh_host=a.b.c ansible_host=b.c.d + + diff --git a/tests/ansible/integration/transport_config/all.yml b/tests/ansible/integration/transport_config/all.yml index cd3f7ac2..ae86bb18 100644 --- a/tests/ansible/integration/transport_config/all.yml +++ b/tests/ansible/integration/transport_config/all.yml @@ -1,2 +1,3 @@ - include: python_path.yml +- include: remote_addr.yml - include: transport.yml diff --git a/tests/ansible/integration/transport_config/remote_addr.yml b/tests/ansible/integration/transport_config/remote_addr.yml new file mode 100644 index 00000000..b9887202 --- /dev/null +++ b/tests/ansible/integration/transport_config/remote_addr.yml @@ -0,0 +1,95 @@ + +# Each case is followed by mitogen_via= case to test hostvars method. + + +# When no ansible_host/ansible_ssh_host= is set, hostname is same as inventory +# name. +- name: integration/transport_config/remote_addr.yml + hosts: tc-remote-addr-unset + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.hostname + right: "tc-remote-addr-unset" + +- hosts: tc-remote-addr-unset + vars: {mitogen_via: tc-remote-addr-explicit-ssh} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.hostname + right: "ansi.ssh.host" + - assert_equal: + left: out.result[1].kwargs.hostname + right: "tc-remote-addr-unset" + + +# ansible_ssh_host= + +- hosts: tc-remote-addr-explicit-ssh + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.hostname + right: "ansi.ssh.host" + +- hosts: tc-remote-addr-explicit-ssh + vars: {mitogen_via: tc-remote-addr-unset} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.hostname + right: "tc-remote-addr-unset" + - assert_equal: + left: out.result[1].kwargs.hostname + right: "ansi.ssh.host" + + +# ansible_host= + +- hosts: tc-remote-addr-explicit-host + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.hostname + right: "ansi.host" + +- hosts: tc-remote-addr-explicit-host + vars: {mitogen_via: tc-remote-addr-unset} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.hostname + right: "tc-remote-addr-unset" + - assert_equal: + left: out.result[1].kwargs.hostname + right: "ansi.host" + + +# both; ansible_ssh_host= takes precedence according to play_context.py. + +- hosts: tc-remote-addr-explicit-both + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.hostname + right: "a.b.c" + +- hosts: tc-remote-addr-explicit-both + vars: {mitogen_via: tc-remote-addr-unset} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert_equal: + left: out.result[0].kwargs.hostname + right: "tc-remote-addr-unset" + - assert_equal: + left: out.result[1].kwargs.hostname + right: "a.b.c"