Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Unify usage of sudo() and ansible(..., become=true, become_user="...")

Abierto
#692 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
ansible, python
Área
devops, testing

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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)
Lenguaje dominante
Python
Estrellas
2.5k
Forks
360
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de pytest-dev/pytest-testinfra

Todos los issues de pytest-dev/pytest-testinfra

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.