| 1 |
# Copyright (c) 2018, 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: pfexec |
| 8 |
short_description: Profile based execution |
| 9 |
description: |
| 10 |
- This become plugins allows your remote/login user to execute commands as another user using the C(pfexec) utility. |
| 11 |
author: Ansible Core Team |
| 12 |
options: |
| 13 |
become_user: |
| 14 |
description: |
| 15 |
- User you 'become' to execute the task. |
| 16 |
- This plugin ignores this setting as pfexec uses its own C(exec_attr) to figure this out, but it is supplied here for |
| 17 |
Ansible to make decisions needed for the task execution, like file permissions. |
| 18 |
type: string |
| 19 |
default: root |
| 20 |
ini: |
| 21 |
- section: privilege_escalation |
| 22 |
key: become_user |
| 23 |
- section: pfexec_become_plugin |
| 24 |
key: user |
| 25 |
vars: |
| 26 |
- name: ansible_become_user |
| 27 |
- name: ansible_pfexec_user |
| 28 |
env: |
| 29 |
- name: ANSIBLE_BECOME_USER |
| 30 |
- name: ANSIBLE_PFEXEC_USER |
| 31 |
become_exe: |
| 32 |
description: C(pfexec) executable. |
| 33 |
type: string |
| 34 |
default: pfexec |
| 35 |
ini: |
| 36 |
- section: privilege_escalation |
| 37 |
key: become_exe |
| 38 |
- section: pfexec_become_plugin |
| 39 |
key: executable |
| 40 |
vars: |
| 41 |
- name: ansible_become_exe |
| 42 |
- name: ansible_pfexec_exe |
| 43 |
env: |
| 44 |
- name: ANSIBLE_BECOME_EXE |
| 45 |
- name: ANSIBLE_PFEXEC_EXE |
| 46 |
become_flags: |
| 47 |
description: Options to pass to C(pfexec). |
| 48 |
type: string |
| 49 |
default: "" |
| 50 |
ini: |
| 51 |
- section: privilege_escalation |
| 52 |
key: become_flags |
| 53 |
- section: pfexec_become_plugin |
| 54 |
key: flags |
| 55 |
vars: |
| 56 |
- name: ansible_become_flags |
| 57 |
- name: ansible_pfexec_flags |
| 58 |
env: |
| 59 |
- name: ANSIBLE_BECOME_FLAGS |
| 60 |
- name: ANSIBLE_PFEXEC_FLAGS |
| 61 |
become_pass: |
| 62 |
description: C(pfexec) password. |
| 63 |
type: string |
| 64 |
required: false |
| 65 |
vars: |
| 66 |
- name: ansible_become_password |
| 67 |
- name: ansible_become_pass |
| 68 |
- name: ansible_pfexec_pass |
| 69 |
env: |
| 70 |
- name: ANSIBLE_BECOME_PASS |
| 71 |
- name: ANSIBLE_PFEXEC_PASS |
| 72 |
ini: |
| 73 |
- section: pfexec_become_plugin |
| 74 |
key: password |
| 75 |
wrap_exe: |
| 76 |
description: |
| 77 |
- Toggle to wrap the command C(pfexec) calls in C(shell -c) or not. |
| 78 |
- Unlike C(sudo), C(pfexec) does not interpret shell constructs internally, |
| 79 |
so commands containing shell operators must be wrapped in a shell invocation. |
| 80 |
- The current default of V(false) only works in very limited cases (for example |
| 81 |
with M(ansible.builtin.raw)). |
| 82 |
- The current default is B(deprecated) and will change to V(true) in community.general 14.0.0. |
| 83 |
To avoid the deprecation message, you can explicitly set this option to a value. |
| 84 |
type: bool |
| 85 |
ini: |
| 86 |
- section: pfexec_become_plugin |
| 87 |
key: wrap_execution |
| 88 |
vars: |
| 89 |
- name: ansible_pfexec_wrap_execution |
| 90 |
env: |
| 91 |
- name: ANSIBLE_PFEXEC_WRAP_EXECUTION |
| 92 |
notes: |
| 93 |
- This plugin ignores O(become_user) as pfexec uses its own C(exec_attr) to figure this out. |
| 94 |
""" |
| 95 |
|
| 96 |
from ansible.plugins.become import BecomeBase |
| 97 |
from ansible.utils.display import Display |
| 98 |
|
| 99 |
display = Display() |
| 100 |
|
| 101 |
|
| 102 |
class BecomeModule(BecomeBase): |
| 103 |
name = "community.general.pfexec" |
| 104 |
|
| 105 |
def build_become_command(self, cmd, shell): |
| 106 |
super().build_become_command(cmd, shell) |
| 107 |
|
| 108 |
if not cmd: |
| 109 |
return cmd |
| 110 |
|
| 111 |
exe = self.get_option("become_exe") |
| 112 |
flags = self.get_option("become_flags") |
| 113 |
|
| 114 |
wrap_exe = self.get_option("wrap_exe") |
| 115 |
if wrap_exe is None: |
| 116 |
display.deprecated( |
| 117 |
"The default value of the wrap_exe option for the community.general.pfexec " |
| 118 |
"become plugin will change from false to true in community.general 14.0.0. " |
| 119 |
"Set wrap_exe explicitly to silence this warning.", |
| 120 |
version="14.0.0", |
| 121 |
collection_name="community.general", |
| 122 |
) |
| 123 |
wrap_exe = False |
| 124 |
|
| 125 |
become_cmd = self._build_success_command(cmd, shell, noexe=not wrap_exe) |
| 126 |
return " ".join(part for part in (exe, flags, become_cmd) if part) |