1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
"""Become abstraction for interacting with test hosts."""
from __future__ import annotations
import abc
import shlex
from .util import (
get_subclasses,
)
class Become(metaclass=abc.ABCMeta):
"""Base class for become implementations."""
@classmethod
def name(cls) -> str:
"""The name of this plugin."""
return cls.__name__.lower()
@property
@abc.abstractmethod
def method(self) -> str:
"""The name of the Ansible become plugin that is equivalent to this."""
@abc.abstractmethod
def prepare_command(self, command: list[str]) -> list[str]:
"""Return the given command, if any, with privilege escalation."""
class Doas(Become):
"""Become using 'doas'."""
@property
def method(self) -> str:
"""The name of the Ansible become plugin that is equivalent to this."""
raise NotImplementedError('Ansible has no built-in doas become plugin.')
def prepare_command(self, command: list[str]) -> list[str]:
"""Return the given command, if any, with privilege escalation."""
become = ['doas', '-n']
if command:
become.extend(['sh', '-c', shlex.join(command)])
else:
become.extend(['-s'])
return become
class DoasSudo(Doas):
"""Become using 'doas' in ansible-test and then after bootstrapping use 'sudo' for other ansible commands."""
@classmethod
def name(cls) -> str:
"""The name of this plugin."""
return 'doas_sudo'
@property
def method(self) -> str:
"""The name of the Ansible become plugin that is equivalent to this."""
return 'sudo'
class Su(Become):
"""Become using 'su'."""
@property
def method(self) -> str:
"""The name of the Ansible become plugin that is equivalent to this."""
return 'su'
def prepare_command(self, command: list[str]) -> list[str]:
"""Return the given command, if any, with privilege escalation."""
become = ['su', '-l', 'root']
if command:
become.extend(['-c', shlex.join(command)])
return become
class SuSudo(Su):
"""Become using 'su' in ansible-test and then after bootstrapping use 'sudo' for other ansible commands."""
@classmethod
def name(cls) -> str:
"""The name of this plugin."""
return 'su_sudo'
@property
def method(self) -> str:
"""The name of the Ansible become plugin that is equivalent to this."""
return 'sudo'
class Sudo(Become):
"""Become using 'sudo'."""
@property
def method(self) -> str:
"""The name of the Ansible become plugin that is equivalent to this."""
return 'sudo'
def prepare_command(self, command: list[str]) -> list[str]:
"""Return the given command, if any, with privilege escalation."""
become = ['sudo', '-in']
if command:
become.extend(['sh', '-c', shlex.join(command)])
return become
SUPPORTED_BECOME_METHODS = {cls.name(): cls for cls in get_subclasses(Become)}
|