Skip to content

Commit a07489d

Browse files
authored
Merge pull request #1148 from mordekasg/#1083
ansible_mitogen: Support templated `become_user`
2 parents 3b2b03b + bf6607e commit a07489d

File tree

12 files changed

+122
-5
lines changed

12 files changed

+122
-5
lines changed

ansible_mitogen/connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ def _connect_stack(self, stack):
814814

815815
self.context = dct['context']
816816
self.chain = CallChain(self, self.context, pipelined=True)
817-
if self._play_context.become:
817+
if self.become:
818818
self.login_context = dct['via']
819819
else:
820820
self.login_context = self.context
@@ -926,7 +926,7 @@ def reset(self):
926926
self.close()
927927

928928
inventory_name, stack = self._build_stack()
929-
if self._play_context.become:
929+
if self.become:
930930
stack = stack[:-1]
931931

932932
worker_model = ansible_mitogen.process.get_worker_model()

ansible_mitogen/mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def _remote_expand_user(self, path, sudoable=True):
294294
if not path.startswith('~'):
295295
# /home/foo -> /home/foo
296296
return path
297-
if sudoable or not self._play_context.become:
297+
if sudoable or not self._connection.become:
298298
if path == '~':
299299
# ~ -> /home/dmw
300300
return self._connection.homedir

ansible_mitogen/transport_config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,10 @@ def __init__(self, connection, play_context, transport, inventory_name):
417417
# used to run interpreter discovery
418418
self._action = connection._action
419419

420+
def _become_option(self, name):
421+
plugin = self._connection.become
422+
return plugin.get_option(name, self._task_vars, self._play_context)
423+
420424
def _connection_option(self, name):
421425
try:
422426
return self._connection.get_option(name, hostvars=self._task_vars)
@@ -437,13 +441,13 @@ def remote_user(self):
437441
return self._connection_option('remote_user')
438442

439443
def become(self):
440-
return self._play_context.become
444+
return self._connection.become
441445

442446
def become_method(self):
443447
return self._play_context.become_method
444448

445449
def become_user(self):
446-
return self._play_context.become_user
450+
return self._become_option('become_user')
447451

448452
def become_pass(self):
449453
# become_pass is owned/provided by the active become plugin. However

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ In progress (unreleased)
2323

2424
* :gh:issue:`1159` CI: Reduce number of Jobs by parameterizing Mitogen Docker
2525
SSH tests
26+
* :gh:issue:`1083` :mod:`ansible_mitogen`: Support templated become username.
2627

2728

2829
v0.3.13 (2024-10-09)

docs/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ sponsorship and outstanding future-thinking of its early adopters.
134134
<li>luto</li>
135135
<li><a href="https://mayeu.me/">Mayeu a.k.a Matthieu Maury</a></li>
136136
<li><a href="https://github.com/madsi1m">Michael D'Silva</a></li>
137+
<li><a href="https://github.com/mordekasg">mordek</a></li>
137138
<li><a href="https://twitter.com/nathanhruby">@nathanhruby</a></li>
138139
<li><a href="https://github.com/opoplawski">Orion Poplawski</a></li>
139140
<li><a href="https://github.com/philfry">Philippe Kueck</a></li>

tests/ansible/hosts/default.hosts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ tt-bare
2525
[tt_targets_bare:vars]
2626
ansible_host=localhost
2727

28+
[tt_become_bare]
29+
tt-become-bare
30+
31+
[tt_become_bare:vars]
32+
ansible_host=localhost
33+
ansible_user="{{ lookup('pipe', 'whoami') }}"
34+
35+
[tt_become_by_inv]
36+
tt-become-user ansible_become=true ansible_become_user="{{ 'root' | trim }}"
37+
38+
[tt_become_by_inv:vars]
39+
ansible_host=localhost
40+
ansible_user="{{ lookup('pipe', 'whoami') }}"
41+
2842
[tt_targets_inventory]
2943
tt-password ansible_password="{{ 'has_sudo_nopw_password' | trim }}" ansible_user=mitogen__has_sudo_nopw
3044
tt-port ansible_password=has_sudo_nopw_password ansible_port="{{ 22 | int }}" ansible_user=mitogen__has_sudo_nopw

tests/ansible/integration/become/all.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
- import_playbook: sudo_nopassword.yml
66
- import_playbook: sudo_password.yml
77
- import_playbook: sudo_requiretty.yml
8+
- import_playbook: templated_by_inv.yml
9+
- import_playbook: templated_by_play_keywords.yml
10+
- import_playbook: templated_by_play_vars.yml
11+
- import_playbook: templated_by_task_keywords.yml
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
- name: integration/become/templated_by_inv.yml
2+
hosts: tt_become_by_inv
3+
gather_facts: false
4+
tasks:
5+
- meta: reset_connection
6+
- name: Templated become in inventory
7+
command:
8+
cmd: whoami
9+
changed_when: false
10+
check_mode: false
11+
register: become_templated_by_inv_whoami
12+
failed_when:
13+
- become_templated_by_inv_whoami is failed
14+
or become_templated_by_inv_whoami.stdout != 'root'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- name: integration/become/templated_by_play_keywords.yml
2+
hosts: tt_become_bare
3+
gather_facts: false
4+
become: true
5+
become_user: "{{ 'root' | trim }}"
6+
tasks:
7+
- meta: reset_connection
8+
- name: Templated become by play keywords
9+
command:
10+
cmd: whoami
11+
changed_when: false
12+
check_mode: false
13+
register: become_templated_by_play_keywords_whoami
14+
failed_when:
15+
- become_templated_by_play_keywords_whoami is failed
16+
or become_templated_by_play_keywords_whoami.stdout != 'root'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
- name: integration/become/templated_by_play_vars.yml
2+
hosts: tt_become_bare
3+
gather_facts: false
4+
vars:
5+
ansible_become: true
6+
ansible_become_user: "{{ 'root' | trim }}"
7+
tasks:
8+
- name: Templated become by play vars
9+
command:
10+
cmd: whoami
11+
changed_when: false
12+
check_mode: false
13+
register: become_templated_by_play_vars_whoami
14+
failed_when:
15+
- become_templated_by_play_vars_whoami is failed
16+
or become_templated_by_play_vars_whoami.stdout != 'root'

0 commit comments

Comments
 (0)