| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2018, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
DOCUMENTATION = """ |
| 7 |
name: sudo |
| 8 |
short_description: Substitute User DO |
| 9 |
description: |
| 10 |
- This become plugin allows your remote/login user to execute commands as another user via the sudo utility. |
| 11 |
author: ansible (@core) |
| 12 |
version_added: "2.8" |
| 13 |
options: |
| 14 |
become_user: |
| 15 |
description: User you 'become' to execute the task |
| 16 |
default: root |
| 17 |
ini: |
| 18 |
- section: privilege_escalation |
| 19 |
key: become_user |
| 20 |
- section: sudo_become_plugin |
| 21 |
key: user |
| 22 |
vars: |
| 23 |
- name: ansible_become_user |
| 24 |
- name: ansible_sudo_user |
| 25 |
env: |
| 26 |
- name: ANSIBLE_BECOME_USER |
| 27 |
- name: ANSIBLE_SUDO_USER |
| 28 |
keyword: |
| 29 |
- name: become_user |
| 30 |
become_exe: |
| 31 |
description: Sudo executable |
| 32 |
default: sudo |
| 33 |
ini: |
| 34 |
- section: privilege_escalation |
| 35 |
key: become_exe |
| 36 |
- section: sudo_become_plugin |
| 37 |
key: executable |
| 38 |
vars: |
| 39 |
- name: ansible_become_exe |
| 40 |
- name: ansible_sudo_exe |
| 41 |
env: |
| 42 |
- name: ANSIBLE_BECOME_EXE |
| 43 |
- name: ANSIBLE_SUDO_EXE |
| 44 |
keyword: |
| 45 |
- name: become_exe |
| 46 |
become_flags: |
| 47 |
description: Options to pass to sudo |
| 48 |
default: -H -S -n |
| 49 |
ini: |
| 50 |
- section: privilege_escalation |
| 51 |
key: become_flags |
| 52 |
- section: sudo_become_plugin |
| 53 |
key: flags |
| 54 |
vars: |
| 55 |
- name: ansible_become_flags |
| 56 |
- name: ansible_sudo_flags |
| 57 |
env: |
| 58 |
- name: ANSIBLE_BECOME_FLAGS |
| 59 |
- name: ANSIBLE_SUDO_FLAGS |
| 60 |
keyword: |
| 61 |
- name: become_flags |
| 62 |
become_pass: |
| 63 |
description: Password to pass to sudo |
| 64 |
type: str |
| 65 |
secret: true |
| 66 |
required: False |
| 67 |
vars: |
| 68 |
- name: ansible_become_password |
| 69 |
- name: ansible_become_pass |
| 70 |
- name: ansible_sudo_pass |
| 71 |
env: |
| 72 |
- name: ANSIBLE_BECOME_PASS |
| 73 |
- name: ANSIBLE_SUDO_PASS |
| 74 |
ini: |
| 75 |
- section: sudo_become_plugin |
| 76 |
key: password |
| 77 |
sudo_chdir: |
| 78 |
description: Directory to change to before invoking sudo; can avoid permission errors when dropping privileges. |
| 79 |
type: string |
| 80 |
required: False |
| 81 |
version_added: '2.19' |
| 82 |
vars: |
| 83 |
- name: ansible_sudo_chdir |
| 84 |
env: |
| 85 |
- name: ANSIBLE_SUDO_CHDIR |
| 86 |
ini: |
| 87 |
- section: sudo_become_plugin |
| 88 |
key: chdir |
| 89 |
""" |
| 90 |
|
| 91 |
import re |
| 92 |
import shlex |
| 93 |
|
| 94 |
from ansible.plugins.become import BecomeBase |
| 95 |
from ansible.errors import AnsibleError |
| 96 |
|
| 97 |
|
| 98 |
class BecomeModule(BecomeBase): |
| 99 |
|
| 100 |
name = 'sudo' |
| 101 |
|
| 102 |
# messages for detecting prompted password issues |
| 103 |
fail = ('Sorry, try again.',) |
| 104 |
missing = ('Sorry, a password is required to run sudo', 'sudo: a password is required') |
| 105 |
|
| 106 |
def check_password_prompt(self, b_output): |
| 107 |
matched = super().check_password_prompt(b_output) |
| 108 |
if not matched: |
| 109 |
# might be using sudo-rs, which is not backwards compatible |
| 110 |
prompt = self.prompt |
| 111 |
self.prompt = f"[sudo: {prompt}] Password:" # handle extra text from sudo-rs |
| 112 |
matched = super().check_password_prompt(b_output) |
| 113 |
self.prompt = prompt |
| 114 |
|
| 115 |
return matched |
| 116 |
|
| 117 |
def build_become_command(self, cmd, shell): |
| 118 |
super(BecomeModule, self).build_become_command(cmd, shell) |
| 119 |
|
| 120 |
if not cmd: |
| 121 |
return cmd |
| 122 |
|
| 123 |
becomecmd = self.get_option('become_exe') or self.name |
| 124 |
|
| 125 |
flags = self.get_option('become_flags') or '' |
| 126 |
prompt = '' |
| 127 |
if self.get_option('become_pass'): |
| 128 |
self.prompt = f'[sudo via ansible, key={self._id}] password:' |
| 129 |
if flags: # this could be simplified, but kept as is for now for backwards string matching |
| 130 |
reflag = [] |
| 131 |
for flag in shlex.split(flags): |
| 132 |
if flag in ('-n', '--non-interactive'): |
| 133 |
continue |
| 134 |
elif not flag.startswith('--'): |
| 135 |
# handle -XnxxX flags only |
| 136 |
flag = re.sub(r'^(-\w*)n(\w*.*)', r'\1\2', flag) |
| 137 |
reflag.append(flag) |
| 138 |
flags = shlex.join(reflag) |
| 139 |
|
| 140 |
prompt = f'-p "{self.prompt}"' |
| 141 |
|
| 142 |
user = self.get_option('become_user') or '' |
| 143 |
if user: |
| 144 |
user = f'-u {user}' |
| 145 |
|
| 146 |
if chdir := self.get_option('sudo_chdir'): |
| 147 |
try: |
| 148 |
becomecmd = f'{shell.CD} {shlex.quote(chdir)} {shell._SHELL_AND} {becomecmd}' |
| 149 |
except AttributeError as ex: |
| 150 |
raise AnsibleError(f'The {shell._load_name!r} shell plugin does not support sudo chdir. It is missing the {ex.name!r} attribute.') |
| 151 |
|
| 152 |
return ' '.join([becomecmd, flags, prompt, user, self._build_success_command(cmd, shell)]) |