Skip to content

Commit 0526f8e

Browse files
authored
Merge pull request #1169 from moreati/issue1083-become_pass
ansible_mitogen: Support templated become passwords
2 parents 21e002a + 7e5b064 commit 0526f8e

File tree

8 files changed

+129
-17
lines changed

8 files changed

+129
-17
lines changed

ansible_mitogen/transport_config.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -450,19 +450,7 @@ def become_user(self):
450450
return self._become_option('become_user')
451451

452452
def become_pass(self):
453-
# become_pass is owned/provided by the active become plugin. However
454-
# PlayContext is intertwined with it. Known complications
455-
# - ansible_become_password is higher priority than ansible_become_pass,
456-
# `play_context.become_pass` doesn't obey this (atleast with Mitgeon).
457-
# - `meta: reset_connection` runs `connection.reset()` but
458-
# `ansible_mitogen.connection.Connection.reset()` recreates the
459-
# connection object, setting `connection.become = None`.
460-
become_plugin = self._connection.become
461-
try:
462-
become_pass = become_plugin.get_option('become_pass', playcontext=self._play_context)
463-
except AttributeError:
464-
become_pass = self._play_context.become_pass
465-
return optional_secret(become_pass)
453+
return optional_secret(self._become_option('become_pass'))
466454

467455
def password(self):
468456
return optional_secret(self._connection_option('password'))

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ In progress (unreleased)
2424
* :gh:issue:`905` :mod:`ansible_mitogen`: Support templated SSH command
2525
arguments (e.g. ``ansible_ssh_args``, ``ansible_ssh_extra_args``).
2626
* :gh:issue:`692` tests: Fix and re-enable several sudo tests
27+
* :gh:issue:`1083` :mod:`ansible_mitogen`: Support templated become password
28+
(e.g. ``ansible_become_pass``, ``ansible_sudo_pass``)
2729

2830

2931
v0.3.14 (2024-10-16)

tests/ansible/hosts/default.hosts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ansible_host=localhost
3333
ansible_user="{{ lookup('pipe', 'whoami') }}"
3434

3535
[tt_become_by_inv]
36+
tt-become-pass ansible_become=true ansible_become_pass="{{ 'pw_required_password' | trim }}" ansible_become_user=mitogen__pw_required
3637
tt-become-user ansible_become=true ansible_become_user="{{ 'root' | trim }}"
3738

3839
[tt_become_by_inv:vars]

tests/ansible/integration/become/templated_by_inv.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@
22
hosts: tt_become_by_inv
33
gather_facts: false
44
tasks:
5+
- name: Gather facts (avoiding any unprivileged become)
6+
vars:
7+
ansible_become: false
8+
setup:
9+
510
- meta: reset_connection
11+
612
- name: Templated become in inventory
13+
vars:
14+
expected_become_users:
15+
tt-become-pass: mitogen__pw_required
16+
tt-become-user: root
717
command:
818
cmd: whoami
919
changed_when: false
1020
check_mode: false
1121
register: become_templated_by_inv_whoami
1222
failed_when:
1323
- become_templated_by_inv_whoami is failed
14-
or become_templated_by_inv_whoami.stdout != 'root'
24+
or become_templated_by_inv_whoami.stdout != expected_become_users[inventory_hostname]
25+
when:
26+
# https://github.com/ansible/ansible/pull/70785
27+
- ansible_become_user in ['root']
28+
or ansible_facts.distribution not in ["MacOSX"]
29+
or ansible_version.full is version("2.11", ">=", strict=True)
30+
or is_mitogen

tests/ansible/integration/become/templated_by_play_keywords.yml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
become_user: "{{ 'root' | trim }}"
66
tasks:
77
- meta: reset_connection
8-
- name: Templated become by play keywords
8+
9+
- name: Templated become by play keywords, no password
910
command:
1011
cmd: whoami
1112
changed_when: false
@@ -14,3 +15,33 @@
1415
failed_when:
1516
- become_templated_by_play_keywords_whoami is failed
1617
or become_templated_by_play_keywords_whoami.stdout != 'root'
18+
19+
- name: integration/become/templated_by_play_keywords.yml
20+
hosts: tt_become_bare
21+
gather_facts: false
22+
become: true
23+
become_user: "{{ 'mitogen__pw_required' | trim }}"
24+
vars:
25+
ansible_become_pass: "{{ 'pw_required_password' | trim }}"
26+
tasks:
27+
- name: Gather facts (avoiding any unprivileged become)
28+
vars:
29+
ansible_become: false
30+
setup:
31+
32+
- meta: reset_connection
33+
34+
- name: Templated become by play keywords, password
35+
command:
36+
cmd: whoami
37+
changed_when: false
38+
check_mode: false
39+
register: become_templated_by_play_keywords_password_whoami
40+
failed_when:
41+
- become_templated_by_play_keywords_password_whoami is failed
42+
or become_templated_by_play_keywords_password_whoami.stdout != 'mitogen__pw_required'
43+
when:
44+
# https://github.com/ansible/ansible/pull/70785
45+
- ansible_facts.distribution not in ["MacOSX"]
46+
or ansible_version.full is version("2.11", ">=", strict=True)
47+
or is_mitogen

tests/ansible/integration/become/templated_by_play_vars.yml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
ansible_become: true
66
ansible_become_user: "{{ 'root' | trim }}"
77
tasks:
8-
- name: Templated become by play vars
8+
- name: Templated become by play vars, no password
99
command:
1010
cmd: whoami
1111
changed_when: false
@@ -14,3 +14,33 @@
1414
failed_when:
1515
- become_templated_by_play_vars_whoami is failed
1616
or become_templated_by_play_vars_whoami.stdout != 'root'
17+
18+
- name: integration/become/templated_by_play_vars.yml
19+
hosts: tt_become_bare
20+
gather_facts: false
21+
vars:
22+
ansible_become: true
23+
ansible_become_pass: "{{ 'pw_required_password' | trim }}"
24+
ansible_become_user: "{{ 'mitogen__pw_required' | trim }}"
25+
tasks:
26+
- name: Gather facts (avoiding any unprivileged become)
27+
vars:
28+
ansible_become: false
29+
setup:
30+
31+
- meta: reset_connection
32+
33+
- name: Templated become by play vars, password
34+
command:
35+
cmd: whoami
36+
changed_when: false
37+
check_mode: false
38+
register: become_templated_by_play_vars_password_whoami
39+
failed_when:
40+
- become_templated_by_play_vars_password_whoami is failed
41+
or become_templated_by_play_vars_password_whoami.stdout != 'mitogen__pw_required'
42+
when:
43+
# https://github.com/ansible/ansible/pull/70785
44+
- ansible_facts.distribution not in ["MacOSX"]
45+
or ansible_version.full is version("2.11", ">=", strict=True)
46+
or is_mitogen

tests/ansible/integration/become/templated_by_task_keywords.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,47 @@
2525
failed_when:
2626
- become_templated_by_task_with_delegate_to_whoami is failed
2727
or become_templated_by_task_with_delegate_to_whoami.stdout != 'root'
28+
29+
30+
- name: integration/become/templated_by_task_keywords.yml
31+
hosts: tt_become_bare
32+
gather_facts: false
33+
# FIXME Resetting the connection shouldn't require credentials
34+
# https://github.com/mitogen-hq/mitogen/issues/1132
35+
become: true
36+
become_user: "{{ 'mitogen__pw_required' | trim }}"
37+
vars:
38+
ansible_become_pass: "{{ 'pw_required_password' | trim }}"
39+
tasks:
40+
- name: Reset connection to target that will be delegate_to
41+
meta: reset_connection
42+
43+
- name: Test connection template by task keywords, with delegate_to
44+
hosts: test-targets[0]
45+
gather_facts: false
46+
tasks:
47+
- name: Gather facts (avoiding any unprivileged become)
48+
delegate_to: "{{ groups.tt_become_bare[0] }}"
49+
vars:
50+
ansible_become: false
51+
setup:
52+
53+
- name: Templated become by task keywords, with delegate_to
54+
become: true
55+
become_user: "{{ 'mitogen__pw_required' | trim }}"
56+
delegate_to: "{{ groups.tt_become_bare[0] }}"
57+
vars:
58+
ansible_become_pass: "{{ 'pw_required_password' | trim }}"
59+
command:
60+
cmd: whoami
61+
changed_when: false
62+
check_mode: false
63+
register: become_templated_by_task_with_delegate_to_password_whoami
64+
failed_when:
65+
- become_templated_by_task_with_delegate_to_password_whoami is failed
66+
or become_templated_by_task_with_delegate_to_password_whoami.stdout != 'mitogen__pw_required'
67+
when:
68+
# https://github.com/ansible/ansible/pull/70785
69+
- ansible_facts.distribution not in ["MacOSX"]
70+
or ansible_version.full is version("2.11", ">=", strict=True)
71+
or is_mitogen

tests/ansible/templates/test-targets.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ tt-bare
4545

4646
[tt_targets_bare:vars]
4747
ansible_host={{ tt.hostname }}
48-
ansible_port={{ tt.port }}
4948
ansible_python_interpreter={{ tt.python_path }}
5049

5150
[tt_become_bare]
@@ -59,6 +58,7 @@ ansible_python_interpreter={{ tt.python_path }}
5958
ansible_user=mitogen__has_sudo_nopw
6059

6160
[tt_become_by_inv]
61+
tt-become-pass ansible_become=true ansible_become_pass="{{ '{{' }} 'pw_required_password' | trim {{ '}}' }}" ansible_become_user=mitogen__pw_required
6262
tt-become-user ansible_become=true ansible_become_user="{{ '{{' }} 'root' | trim {{ '}}' }}"
6363

6464
[tt_become_by_inv:vars]

0 commit comments

Comments
 (0)