.. _become: ********************************** Understanding Privilege Escalation ********************************** Ansible can use existing privilege escalation systems to allow a user to execute tasks as another. .. contents:: Topics Become ====== Ansible allows you to 'become' another user, different from the user that logged into the machine (remote user). This is done using existing privilege escalation tools such as `sudo`, `su`, `pfexec`, `doas`, `pbrun`, `dzdo`, `ksu`, `runas` and others. .. note:: Prior to version 1.9, Ansible mostly allowed the use of `sudo` and a limited use of `su` to allow a login/remote user to become a different user and execute tasks and create resources with the second user's permissions. As of Ansible version 1.9, `become` supersedes the old sudo/su, while still being backwards compatible. This new implementation also makes it easier to add other privilege escalation tools, including `pbrun` (Powerbroker), `pfexec`, `dzdo` (Centrify), and others. .. note:: Become vars and directives are independent. For example, setting ``become_user`` does not set ``become``. Directives ========== These can be set from play to task level, but are overridden by connection variables as they can be host specific. become set to ``yes`` to activate privilege escalation. become_user set to user with desired privileges — the user you `become`, NOT the user you login as. Does NOT imply ``become: yes``, to allow it to be set at host level. become_method (at play or task level) overrides the default method set in ansible.cfg, set to `sudo`/`su`/`pbrun`/`pfexec`/`doas`/`dzdo`/`ksu`/`runas` become_flags (at play or task level) permit the use of specific flags for the tasks or role. One common use is to change the user to nobody when the shell is set to no login. Added in Ansible 2.2. For example, to manage a system service (which requires ``root`` privileges) when connected as a non-``root`` user (this takes advantage of the fact that the default value of ``become_user`` is ``root``):: - name: Ensure the httpd service is running service: name: httpd state: started become: yes To run a command as the ``apache`` user:: - name: Run a command as the apache user command: somecommand become: yes become_user: apache To do something as the ``nobody`` user when the shell is nologin:: - name: Run a command as nobody command: somecommand become: yes become_method: su become_user: nobody become_flags: '-s /bin/sh' Connection variables -------------------- Each allows you to set an option per group and/or host, these are normally defined in inventory but can be used as normal variables. ansible_become equivalent of the become directive, decides if privilege escalation is used or not. ansible_become_method which privilege escalation method should be used ansible_become_user set the user you become through privilege escalation; does not imply ``ansible_become: yes`` ansible_become_pass set the privilege escalation password. See :doc:`playbooks_vault` for details on how to avoid having secrets in plain text For example, if you want to run all tasks as ``root`` on a server named ``webserver``, but you can only connect as the ``manager`` user, you could use an inventory entry like this:: webserver ansible_user=manager ansible_become=yes Command line options -------------------- --ask-become-pass, -K ask for privilege escalation password; does not imply become will be used. Note that this password will be used for all hosts. --become, -b run operations with become (no password implied) --become-method=BECOME_METHOD privilege escalation method to use (default=sudo), valid choices: [ sudo | su | pbrun | pfexec | doas | dzdo | ksu | runas ] --become-user=BECOME_USER run operations as this user (default=root), does not imply --become/-b For those from Pre 1.9 , sudo and su still work! ------------------------------------------------ For those using old playbooks will not need to be changed, even though they are deprecated, sudo and su directives, variables and options will continue to work. It is recommended to move to become as they may be retired at one point. You cannot mix directives on the same object (become and sudo) though, Ansible will complain if you try to. Become will default to using the old sudo/su configs and variables if they exist, but will override them if you specify any of the new ones. Limitations ----------- Although privilege escalation is mostly intuitive, there are a few limitations on how it works. Users should be aware of these to avoid surprises. Becoming an Unprivileged User ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Ansible 2.0.x and below has a limitation with regards to becoming an unprivileged user that can be a security risk if users are not aware of it. Ansible modules are executed on the remote machine by first substituting the parameters into the module file, then copying the file to the remote machine, and finally executing it there. Everything is fine if the module file is executed without using ``become``, when the ``become_user`` is root, or when the connection to the remote machine is made as root. In these cases the module file is created with permissions that only allow reading by the user and root. The problem occurs when the ``become_user`` is an unprivileged user. Ansible 2.0.x and below make the module file world readable in this case, as the module file is written as the user that Ansible connects as, but the file needs to be readable by the user Ansible is set to ``become``. .. note:: In Ansible 2.1, this window is further narrowed: If the connection is made as a privileged user (root), then Ansible 2.1 and above will use chown to set the file's owner to the unprivileged user being switched to. This means both the user making the connection and the user being switched to via ``become`` must be unprivileged in order to trigger this problem. If any of the parameters passed to the module are sensitive in nature, then those pieces of data are located in a world readable module file for the duration of the Ansible module execution. Once the module is done executing, Ansible will delete the temporary file. If you trust the client machines then there's no problem here. If you do not trust the client machines then this is a potential danger. Ways to resolve this include: * Use `pipelining`. When pipelining is enabled, Ansible doesn't save the module to a temporary file on the client. Instead it pipes the module to the remote python interpreter's stdin. Pipelining does not work for python modules involving file transfer (for example: :ref:`copy `, :ref:`fetch `, :ref:`template