Skip to content

Commit f150387

Browse files
committed
ansible_mitogen: Correct ansible_become_pass/ansible_become_password precendence
Until Ansible 2.9 it looks like ansible_become_password had higher priority. From Ansible 2.10 ansible_become_pass has higher priority [1]. Mitogen was not respecting this. I may need to rework this further, instatiating the become plugin may have slowed down execution. [1] Based on testing with ``` [ubuntus] become-pass-pass ansible_become_pass=1234 become-pass-password ansible_become_password=1234 become-pass-both ansible_become_password=wrong ansible_become_pass=1234 [ubuntus:vars] ansible_host=ubuntu2004.local ansible_user=ubuntu ``` ``` - hosts: ubuntus gather_facts: false become: true tasks: - ping: ```
1 parent ad4b686 commit f150387

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

ansible_mitogen/loaders.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
__all__ = [
4141
'action_loader',
42+
'become_loader',
4243
'connection_loader',
4344
'module_loader',
4445
'module_utils_loader',
@@ -90,6 +91,7 @@ def assert_supported_release():
9091

9192

9293
from ansible.plugins.loader import action_loader
94+
from ansible.plugins.loader import become_loader
9395
from ansible.plugins.loader import connection_loader
9496
from ansible.plugins.loader import module_loader
9597
from ansible.plugins.loader import module_utils_loader

ansible_mitogen/transport_config.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
except ImportError:
8080
from ansible.vars.unsafe_proxy import AnsibleUnsafeText
8181

82+
import ansible_mitogen.loaders
8283
import mitogen.core
8384

8485

@@ -435,7 +436,10 @@ def become_user(self):
435436
return self._play_context.become_user
436437

437438
def become_pass(self):
438-
return optional_secret(self._play_context.become_pass)
439+
become_method = self.become_method()
440+
become_plugin = ansible_mitogen.loaders.become_loader.get(become_method)
441+
become_pass = become_plugin.get_option('become_pass', hostvars=self._task_vars)
442+
return optional_secret(become_pass)
439443

440444
def password(self):
441445
return optional_secret(self._play_context.password)
@@ -652,8 +656,8 @@ def become_user(self):
652656

653657
def become_pass(self):
654658
return optional_secret(
655-
self._host_vars.get('ansible_become_password') or
656-
self._host_vars.get('ansible_become_pass')
659+
self._host_vars.get('ansible_become_pass') or
660+
self._host_vars.get('ansible_become_password')
657661
)
658662

659663
def password(self):

tests/ansible/hosts/transport_config.hosts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ tc-become-user-set ansible_become_user=ansi-become-user
4747
tc-become-pass-unset
4848
tc-become-pass-password ansible_become_password=apassword
4949
tc-become-pass-pass ansible_become_pass=apass
50-
tc-become-pass-both ansible_become_password=a.b.c ansible_become_pass=c.b.a
50+
tc-become-pass-both ansible_become_pass=bpass ansible_become_password=bpassword
5151

5252
# port()
5353
tc-port-unset

tests/ansible/integration/transport_config/become_pass.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,6 @@
119119
fail_msg: out={{out}}
120120

121121

122-
123-
# ansible_become_pass & ansible_become_password set, password used to take precedence
124-
# but it's possible since https://github.com/ansible/ansible/pull/69629/files#r428376864, now it doesn't
125122
- hosts: tc-become-pass-both
126123
become: true
127124
tasks:
@@ -132,7 +129,9 @@
132129
- out.result|length == 2
133130
- out.result[0].method == "ssh"
134131
- out.result[1].method == "sudo"
135-
- out.result[1].kwargs.password == "c.b.a"
132+
# Ansible >= 2.10 builtin become plugins (e.g. sudo, su) give priority
133+
# to ansible_become_pass over ansible_become_password.
134+
- out.result[1].kwargs.password == "bpass"
136135
fail_msg: out={{out}}
137136

138137

@@ -147,6 +146,6 @@
147146
- out.result|length == 3
148147
- out.result[0].method == "ssh"
149148
- out.result[1].method == "sudo"
150-
- out.result[1].kwargs.password == "a.b.c"
149+
- out.result[1].kwargs.password == "bpass"
151150
- out.result[2].method == "ssh"
152151
fail_msg: out={{out}}

0 commit comments

Comments
 (0)