| 1 |
# Copyright (c) 2024, Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
DOCUMENTATION = r""" |
| 8 |
name: run0 |
| 9 |
short_description: Systemd's run0 |
| 10 |
description: |
| 11 |
- This become plugins allows your remote/login user to execute commands as another user using the C(run0) utility. |
| 12 |
author: |
| 13 |
- Thomas Sjögren (@konstruktoid) |
| 14 |
version_added: '9.0.0' |
| 15 |
options: |
| 16 |
become_user: |
| 17 |
description: User you 'become' to execute the task. |
| 18 |
default: root |
| 19 |
ini: |
| 20 |
- section: privilege_escalation |
| 21 |
key: become_user |
| 22 |
- section: run0_become_plugin |
| 23 |
key: user |
| 24 |
vars: |
| 25 |
- name: ansible_become_user |
| 26 |
- name: ansible_run0_user |
| 27 |
env: |
| 28 |
- name: ANSIBLE_BECOME_USER |
| 29 |
- name: ANSIBLE_RUN0_USER |
| 30 |
type: string |
| 31 |
become_exe: |
| 32 |
description: C(run0) executable. |
| 33 |
default: run0 |
| 34 |
ini: |
| 35 |
- section: privilege_escalation |
| 36 |
key: become_exe |
| 37 |
- section: run0_become_plugin |
| 38 |
key: executable |
| 39 |
vars: |
| 40 |
- name: ansible_become_exe |
| 41 |
- name: ansible_run0_exe |
| 42 |
env: |
| 43 |
- name: ANSIBLE_BECOME_EXE |
| 44 |
- name: ANSIBLE_RUN0_EXE |
| 45 |
type: string |
| 46 |
become_flags: |
| 47 |
description: Options to pass to C(run0). |
| 48 |
default: '' |
| 49 |
ini: |
| 50 |
- section: privilege_escalation |
| 51 |
key: become_flags |
| 52 |
- section: run0_become_plugin |
| 53 |
key: flags |
| 54 |
vars: |
| 55 |
- name: ansible_become_flags |
| 56 |
- name: ansible_run0_flags |
| 57 |
env: |
| 58 |
- name: ANSIBLE_BECOME_FLAGS |
| 59 |
- name: ANSIBLE_RUN0_FLAGS |
| 60 |
type: string |
| 61 |
notes: |
| 62 |
- This plugin only works when a C(polkit) rule is in place. |
| 63 |
- This become plugin does not work when connection pipelining is enabled. With ansible-core 2.19+, using it automatically |
| 64 |
disables pipelining. On ansible-core 2.18 and before, pipelining must explicitly be disabled by the user. |
| 65 |
""" |
| 66 |
|
| 67 |
EXAMPLES = r""" |
| 68 |
# An example polkit rule that allows the user 'ansible' in the 'wheel' group |
| 69 |
# to execute commands using run0 without authentication. |
| 70 |
/etc/polkit-1/rules.d/60-run0-fast-user-auth.rules: |- |
| 71 |
polkit.addRule(function(action, subject) { |
| 72 |
if(action.id == "org.freedesktop.systemd1.manage-units" && |
| 73 |
subject.isInGroup("wheel") && |
| 74 |
subject.user == "ansible") { |
| 75 |
return polkit.Result.YES; |
| 76 |
} |
| 77 |
}); |
| 78 |
""" |
| 79 |
|
| 80 |
from re import compile as re_compile |
| 81 |
|
| 82 |
from ansible.module_utils.common.text.converters import to_bytes |
| 83 |
from ansible.plugins.become import BecomeBase |
| 84 |
|
| 85 |
ansi_color_codes = re_compile(to_bytes(r"\x1B\[[0-9;]+m")) |
| 86 |
|
| 87 |
|
| 88 |
class BecomeModule(BecomeBase): |
| 89 |
name = "community.general.run0" |
| 90 |
|
| 91 |
prompt = "Password: " |
| 92 |
fail = ("==== AUTHENTICATION FAILED ====",) |
| 93 |
success = ("==== AUTHENTICATION COMPLETE ====",) |
| 94 |
require_tty = True # see https://github.com/ansible-collections/community.general/issues/6932 |
| 95 |
|
| 96 |
# See https://github.com/ansible/ansible/issues/81254, |
| 97 |
# https://github.com/ansible/ansible/pull/78111 |
| 98 |
pipelining = False |
| 99 |
|
| 100 |
@staticmethod |
| 101 |
def remove_ansi_codes(line): |
| 102 |
return ansi_color_codes.sub(b"", line) |
| 103 |
|
| 104 |
def build_become_command(self, cmd, shell): |
| 105 |
super().build_become_command(cmd, shell) |
| 106 |
|
| 107 |
if not cmd: |
| 108 |
return cmd |
| 109 |
|
| 110 |
become = self.get_option("become_exe") |
| 111 |
flags = self.get_option("become_flags") |
| 112 |
user = self.get_option("become_user") |
| 113 |
|
| 114 |
# SYSTEMD_COLORS=0 stops run0 from emitting terminal control |
| 115 |
# sequences (window title OSC, ANSI reset) around the child |
| 116 |
# command, which would otherwise corrupt the module JSON and |
| 117 |
# break result parsing. |
| 118 |
return f"SYSTEMD_COLORS=0 {become} --user={user} {flags} {self._build_success_command(cmd, shell)}" |
| 119 |
|
| 120 |
def check_success(self, b_output): |
| 121 |
b_output = self.remove_ansi_codes(b_output) |
| 122 |
return super().check_success(b_output) |
| 123 |
|
| 124 |
def check_incorrect_password(self, b_output): |
| 125 |
b_output = self.remove_ansi_codes(b_output) |
| 126 |
return super().check_incorrect_password(b_output) |
| 127 |
|
| 128 |
def check_missing_password(self, b_output): |
| 129 |
b_output = self.remove_ansi_codes(b_output) |
| 130 |
return super().check_missing_password(b_output) |