Unify usage of sudo() and ansible(..., become=true, become_user="...")
还没有人认领这个 Issue。
评估
调研方向
Start by tracing the sudo() context manager and the direct ansible module invocation shown in the example, then compare how explicit become and become_user arguments are handled. Done means an ansible module called inside sudo(user="root") runs as root without explicit become arguments, while the existing explicit-become behavior remains unchanged; verify this with the supplied example.
由索引模型根据 Issue 内容生成。
描述
It's possible to elevate privileges with sudo() in a context manager. This works fine.
Unfortunately, the privilege elevation does not affect the direct invocation of Ansible modules. That means Ansible modules must get elevated privileges with become=True, become_user="root" additionally even if sudo permissions are already set.
Please extend Testinfra to automatically use the elevated privileges from the sudo context manager when calling Ansible modules.
Example:
def test_sudo(host):
with open("result.txt", "w") as f:
whoami_wo_sudo = ansiblehost.check_output("id")
shell_wo_sudo = ansiblehost.ansible("shell id", check=False)
with ansiblehost.sudo(user="root"):
whoami_w_sudo = ansiblehost.check_output("id")
shell_w_sudo = ansiblehost.ansible("shell id", check=False)
shell_w_become = ansiblehost.ansible("shell id", check=False, become=True, become_user="root")
print(f"""\
Test results
============
without sudo:
OS cmd "whoami": {whoami_wo_sudo}
Ansible module "shell": {shell_wo_sudo["stdout"]}
with sudo:
OS cmd "whoami": {whoami_w_sudo}
with sudo & without become:
Ansible module "shell": {shell_w_sudo["stdout"]}
with sudo & with become:
Ansible module "shell": {shell_w_become["stdout"]}
""", file=f)
# cat result.txt
Test results
============
without sudo:
OS cmd "whoami": uid=999(ansible) gid=999(ansible) groups=999(ansible)
Ansible module "shell": uid=999(ansible) gid=999(ansible) groups=999(ansible)
with sudo:
OS cmd "whoami": uid=0(root) gid=0(root) groups=0(root)
with sudo & without become:
Ansible module "shell": uid=999(ansible) gid=999(ansible) groups=999(ansible)
with sudo & with become:
Ansible module "shell": uid=0(root) gid=0(root) groups=0(root)
- 主要语言
- Python
- 星标
- 2.5k
- 派生
- 359
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytest-dev/pytest-testinfra 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 58/100
pytest-dev/pytest-testinfra#815 ·
-
service is_enabled does not work with exit code 4可能已有人在做 @pfuntner 于 576 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 45/100
pytest-dev/pytest-testinfra#801 · 1 条评论 ·
-
Chroot backend未关闭
难度 4/5 3-5 天 新手友好度 25/100
pytest-dev/pytest-testinfra#789 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 42/100
pytest-dev/pytest-testinfra#788 ·
-
难度 4/5 3-5 天 新手友好度 25/100
pytest-dev/pytest-testinfra#786 ·
查看 pytest-dev/pytest-testinfra 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
namespace operations
难度 1/5 1 小时以内 新手友好度 65/100
EclipseFdn/open-vsx.org#15057 ·
维护者通常 1 天内回复
-
Mission statement: why this is free, in the README, Why This Exists and the wiki Home可能已有人在做 @ChiefGyk3D 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
ChiefGyk3D/Clouds-Against-Humanity#348 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 74/100
vllm-project/vime#508 ·
维护者通常 4 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复