ansible / ansible-runner

A tool and python library that helps when interfacing with Ansible directly or as part of another system whether that be through a container image interface, as a standalone tool, or as a Python module that can be imported. The goal is to provide a stable and consistent interface abstraction to Ansible.
Other
968 stars 356 forks source link

How to implement <remote_user> and <become> directives in python mode? #1378

Open MaxDXDX opened 4 months ago

MaxDXDX commented 4 months ago

I'm using runner in my python project as a python module (not via command line tool). And I do not use playbook files. I pass required parameters of tasks by module and module_args arguments of ansible_runner.run function. So far so good. But i got stuck then I needed to run tasks as root user. I connect to host under user with sudo rights, but i do not understand how to execute operations under sudo mode. I used remote_user=root become=true earlier in my playbooks files (then i used ansible in CLI mode). How implement that pattern in ansible-runner?

Shrews commented 4 months ago

See the comments under issue #1082.

Basically, you need to use the cmdline keyword argument available in the ansible_runner.run() method to pass along options to do privilege escalation (example: cmdline="-K -b"). For the password prompt handling, you'll have to utilize the env/passwords file in the private data directory (see https://ansible.readthedocs.io/projects/runner/en/stable/intro/#env-passwords).