| 1 |
# Copyright (c) 2021, 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 |
from __future__ import annotations |
| 5 |
|
| 6 |
DOCUMENTATION = r""" |
| 7 |
name: sudosu |
| 8 |
short_description: Run tasks using sudo su - |
| 9 |
description: |
| 10 |
- This become plugin allows your remote/login user to execute commands as another user using the C(sudo) and C(su) utilities |
| 11 |
combined. |
| 12 |
author: |
| 13 |
- Dag Wieers (@dagwieers) |
| 14 |
version_added: 2.4.0 |
| 15 |
options: |
| 16 |
become_user: |
| 17 |
description: User you 'become' to execute the task. |
| 18 |
type: string |
| 19 |
default: root |
| 20 |
ini: |
| 21 |
- section: privilege_escalation |
| 22 |
key: become_user |
| 23 |
- section: sudo_become_plugin |
| 24 |
key: user |
| 25 |
vars: |
| 26 |
- name: ansible_become_user |
| 27 |
- name: ansible_sudo_user |
| 28 |
env: |
| 29 |
- name: ANSIBLE_BECOME_USER |
| 30 |
- name: ANSIBLE_SUDO_USER |
| 31 |
become_flags: |
| 32 |
description: Options to pass to C(sudo). |
| 33 |
type: string |
| 34 |
default: -H -S -n |
| 35 |
ini: |
| 36 |
- section: privilege_escalation |
| 37 |
key: become_flags |
| 38 |
- section: sudo_become_plugin |
| 39 |
key: flags |
| 40 |
vars: |
| 41 |
- name: ansible_become_flags |
| 42 |
- name: ansible_sudo_flags |
| 43 |
env: |
| 44 |
- name: ANSIBLE_BECOME_FLAGS |
| 45 |
- name: ANSIBLE_SUDO_FLAGS |
| 46 |
become_pass: |
| 47 |
description: Password to pass to C(sudo). |
| 48 |
type: string |
| 49 |
required: false |
| 50 |
vars: |
| 51 |
- name: ansible_become_password |
| 52 |
- name: ansible_become_pass |
| 53 |
- name: ansible_sudo_pass |
| 54 |
env: |
| 55 |
- name: ANSIBLE_BECOME_PASS |
| 56 |
- name: ANSIBLE_SUDO_PASS |
| 57 |
ini: |
| 58 |
- section: sudo_become_plugin |
| 59 |
key: password |
| 60 |
alt_method: |
| 61 |
description: |
| 62 |
- Whether to use an alternative method to call C(su). Instead of running C(su -l user /path/to/shell -c command), it |
| 63 |
runs C(su -l user -c command). |
| 64 |
- Use this when the default one is not working on your system. |
| 65 |
required: false |
| 66 |
type: boolean |
| 67 |
ini: |
| 68 |
- section: community.general.sudosu |
| 69 |
key: alternative_method |
| 70 |
vars: |
| 71 |
- name: ansible_sudosu_alt_method |
| 72 |
env: |
| 73 |
- name: ANSIBLE_SUDOSU_ALT_METHOD |
| 74 |
version_added: 9.2.0 |
| 75 |
""" |
| 76 |
|
| 77 |
|
| 78 |
from ansible.plugins.become import BecomeBase |
| 79 |
|
| 80 |
|
| 81 |
class BecomeModule(BecomeBase): |
| 82 |
name = "community.general.sudosu" |
| 83 |
|
| 84 |
# messages for detecting prompted password issues |
| 85 |
fail = ("Sorry, try again.",) |
| 86 |
missing = ("Sorry, a password is required to run sudo", "sudo: a password is required") |
| 87 |
|
| 88 |
def build_become_command(self, cmd, shell): |
| 89 |
super().build_become_command(cmd, shell) |
| 90 |
|
| 91 |
if not cmd: |
| 92 |
return cmd |
| 93 |
|
| 94 |
becomecmd = "sudo" |
| 95 |
|
| 96 |
flags = self.get_option("become_flags") or "" |
| 97 |
prompt = "" |
| 98 |
if self.get_option("become_pass"): |
| 99 |
self.prompt = f"[sudo via ansible, key={self._id}] password:" |
| 100 |
if flags: # this could be simplified, but kept as is for now for backwards string matching |
| 101 |
flags = flags.replace("-n", "") |
| 102 |
prompt = f'-p "{self.prompt}"' |
| 103 |
|
| 104 |
user = self.get_option("become_user") or "" |
| 105 |
if user: |
| 106 |
user = f"{user}" |
| 107 |
|
| 108 |
if self.get_option("alt_method"): |
| 109 |
return f"{becomecmd} {flags} {prompt} su -l {user} -c {self._build_success_command(cmd, shell, True)}" |
| 110 |
else: |
| 111 |
return f"{becomecmd} {flags} {prompt} su -l {user} {self._build_success_command(cmd, shell)}" |