diff --git a/ansible_mitogen/transport_config.py b/ansible_mitogen/transport_config.py index 15694523..edc9ce1e 100644 --- a/ansible_mitogen/transport_config.py +++ b/ansible_mitogen/transport_config.py @@ -486,7 +486,11 @@ class MitogenViaSpec(Spec): return bool(self._become_user) def become_method(self): - return self._become_method or C.DEFAULT_BECOME_METHOD + return ( + self._become_method or + self._host_vars.get('ansible_become_method') or + C.DEFAULT_BECOME_METHOD + ) def become_user(self): return self._become_user diff --git a/docs/changelog.rst b/docs/changelog.rst index 7b1c7584..9ce16e57 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -143,8 +143,8 @@ Fixes ``ansible_ssh_host`` parameter is respected when ``mitogen_via=`` is active. * `21ad299d `_: the - precedence of ``ansible_ssh_user`` and ``ansible_user`` was corrected when - when ``mitogen_via=`` is active. + precedence of ``ansible_ssh_user`` and ``ansible_user`` was corrected when + when ``mitogen_via=`` is active. Thanks! diff --git a/tests/ansible/hosts/transport_config.hosts b/tests/ansible/hosts/transport_config.hosts index 1546b26f..c98d4f41 100644 --- a/tests/ansible/hosts/transport_config.hosts +++ b/tests/ansible/hosts/transport_config.hosts @@ -27,3 +27,7 @@ tc-password-explicit-both ansible_password=a.b.c ansible_ssh_pass=c.b.a # become() tc-become-unset tc-become-set + +# become_method() +tc-become-method-unset +tc-become-method-su ansible_become_method=su diff --git a/tests/ansible/integration/transport_config/all.yml b/tests/ansible/integration/transport_config/all.yml index f259f0a0..908f6735 100644 --- a/tests/ansible/integration/transport_config/all.yml +++ b/tests/ansible/integration/transport_config/all.yml @@ -1,4 +1,5 @@ - include: become.yml +- include: become_method.yml - include: password.yml - include: python_path.yml - include: remote_addr.yml diff --git a/tests/ansible/integration/transport_config/become_method.yml b/tests/ansible/integration/transport_config/become_method.yml new file mode 100644 index 00000000..5129e5b8 --- /dev/null +++ b/tests/ansible/integration/transport_config/become_method.yml @@ -0,0 +1,83 @@ +# Each case is followed by mitogen_via= case to test hostvars method. + + +# No become-method set. +- name: integration/transport_config/become-method.yml + hosts: tc-become-method-unset + become: true + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert: + that: + - out.result|length == 2 + - out.result[0].method == "ssh" + - out.result[1].method == "sudo" + +- hosts: tc-become-method-unset + vars: {mitogen_via: becomeuser@tc-become-method-su} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert: + that: + - out.result|length == 3 + - out.result[0].method == "ssh" + - out.result[1].method == "su" + - out.result[1].kwargs.username == "becomeuser" + - out.result[2].method == "ssh" + - out.result[2].kwargs.hostname == "tc-become-method-unset" + + +# ansible_become_method=su +- hosts: tc-become-method-su + become: true + become_user: becomeuser + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert: + that: + - out.result|length == 2 + - out.result[0].method == "ssh" + - out.result[1].method == "su" + - out.result[1].kwargs.username == "becomeuser" + +- hosts: tc-become-method-su + vars: {mitogen_via: tc-become-method-unset} + become: true + become_user: becomeuser + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert: + that: + - out.result|length == 3 + - out.result[0].method == "ssh" + - out.result[0].kwargs.hostname == "tc-become-method-unset" + + - out.result[1].method == "ssh" + - out.result[1].kwargs.hostname == "tc-become-method-su" + + - out.result[2].method == "su" + - out.result[2].kwargs.username == "becomeuser" + + + +# mitogen_via used to specify explicit become method +- hosts: tc-become-method-unset + vars: {mitogen_via: "doas:doasuser@tc-become-method-su"} + tasks: + - include: ../_mitogen_only.yml + - {mitogen_get_stack: {}, register: out} + - assert: + that: + - out.result|length == 3 + - out.result[0].method == "ssh" + - out.result[0].kwargs.hostname == "tc-become-method-su" + + - out.result[1].method == "doas" + - out.result[1].kwargs.username == "doasuser" + + - out.result[2].method == "ssh" + - out.result[2].kwargs.hostname == "tc-become-method-unset"