2015-12-28 16:24:28 +01:00
Become (Privilege Escalation)
+++++++++++++++++++++++++++++
2014-11-24 22:36:31 +01:00
Ansible can use existing privilege escalation systems to allow a user to execute tasks as another.
.. contents :: Topics
Become
`` ` ` ``
2016-03-10 23:27:43 +01:00
Ansible allows you 'become' another user, different from the user that logged into the machine (remote user). This is done using existing
2016-04-25 17:24:26 +02:00
privilege escalation tools, which you probably already use or have configured, like 'sudo', 'su', 'pfexec', 'doas', 'pbrun', 'dzdo', and others.
2014-11-24 22:36:31 +01:00
2016-02-05 02:35:12 +01:00
.. note :: Before 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, create resources with the 2nd user's permissions. As of 1.9 `become` supersedes the old sudo/su, while still being backwards compatible.
2016-04-25 17:24:26 +02:00
This new system also makes it easier to add other privilege escalation tools like `pbrun` (Powerbroker), `pfexec` , `dzdo` (Centrify), and others.
2016-02-05 02:35:12 +01:00
2016-04-27 03:08:22 +02:00
.. note :: Become vars & directives are independent, i.e. setting `become_user` does not set `become` .
2016-02-24 16:58:40 +01:00
2016-02-05 02:35:12 +01:00
Directives
-----------
These can be set from play to task level, but are overriden by connection variables as they can be host specific.
2014-11-24 22:36:31 +01:00
become
2016-02-05 02:35:12 +01:00
set to 'true'/'yes' to activate privilege escalation.
2014-11-24 22:36:31 +01:00
become_user
2016-02-24 16:58:40 +01:00
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.
2014-11-24 22:36:31 +01:00
become_method
2016-04-25 17:24:26 +02:00
at play or task level overrides the default method set in ansible.cfg, set to 'sudo'/'su'/'pbrun'/'pfexec'/'doas'/'dzdo'
2014-11-24 22:36:31 +01:00
2016-05-27 23:54:05 +02:00
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
2016-06-07 04:28:20 +02:00
state: started
2016-05-27 23:54:05 +02:00
become: true
To run a command as the `` apache `` user::
2016-07-18 10:59:45 +02:00
- name: Run a command as the apache user
2016-05-27 23:54:05 +02:00
command: somecommand
become: true
become_user: apache
2014-11-24 22:36:31 +01:00
2016-02-05 02:35:12 +01:00
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.
2014-11-24 22:36:31 +01:00
ansible_become
2016-02-05 02:35:12 +01:00
equivalent of the become directive, decides if privilege escalation is used or not.
2014-11-24 22:36:31 +01:00
ansible_become_method
allows to set privilege escalation method
ansible_become_user
2016-02-24 16:58:40 +01:00
allows to set the user you become through privilege escalation, does not imply `ansible_become: True`
2014-11-24 22:36:31 +01:00
ansible_become_pass
2016-02-05 02:35:12 +01:00
allows you to set the privilege escalation password
2014-11-24 22:36:31 +01:00
2016-05-27 23:54:05 +02:00
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=true
2014-11-24 22:36:31 +01:00
New command line options
2015-04-28 20:11:49 +02:00
------------------------
2014-11-24 22:36:31 +01:00
2016-02-05 02:35:12 +01:00
--ask-become-pass, -K
ask for privilege escalation password, does not imply become will be used
2014-11-24 22:36:31 +01:00
2016-02-05 02:35:12 +01:00
--become, -b
2015-04-16 18:30:54 +02:00
run operations with become (no password implied)
2014-11-24 22:36:31 +01:00
--become-method=BECOME_METHOD
privilege escalation method to use (default=sudo),
2016-04-25 17:24:26 +02:00
valid choices: [ sudo | su | pbrun | pfexec | doas | dzdo ]
2014-11-24 22:36:31 +01:00
--become-user=BECOME_USER
2016-02-24 16:58:40 +01:00
run operations as this user (default=root), does not imply --become/-b
2014-11-24 22:36:31 +01:00
2016-02-05 02:35:12 +01:00
For those from Pre 1.9 , sudo and su still work!
------------------------------------------------
2014-11-24 22:36:31 +01:00
2016-02-05 02:35:12 +01:00
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.
2016-02-28 22:19:20 +01:00
You cannot mix directives on the same object (become and sudo) though, Ansible will complain if you try to.
2014-11-24 22:36:31 +01:00
2016-02-05 02:35:12 +01:00
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.
2014-11-24 22:36:31 +01:00
2016-03-16 18:58:16 +01:00
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
=============================
2016-03-21 22:17:53 +01:00
Ansible 2.0.x and below has a limitation with regards to becoming an
2016-03-16 18:58:16 +01:00
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,
2016-03-21 22:17:53 +01:00
and finally executing it there.
2016-03-16 18:58:16 +01:00
2016-03-21 22:17:53 +01:00
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
2016-04-29 15:51:04 +02:00
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
2016-04-27 03:16:41 +02:00
be readable by the user Ansible is set to `` become `` .
2016-03-21 22:17:53 +01:00
.. note :: In Ansible 2.1, this window is further narrowed: If the connection
2016-04-29 15:51:04 +02:00
is made as a privileged user (root), then Ansible 2.1 and above will use
2016-03-21 22:17:53 +01:00
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.
2016-03-16 18:58:16 +01:00
2016-04-29 15:51:04 +02:00
If any of the parameters passed to the module are sensitive in nature, then
2016-03-21 22:17:53 +01:00
those pieces of data are located in a world readable module file for the
2016-04-29 15:51:04 +02:00
duration of the Ansible module execution. Once the module is done executing,
2016-03-21 22:17:53 +01:00
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
2016-03-16 18:58:16 +01:00
a potential danger.
Ways to resolve this include:
* Use :ref: `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
non-python modules.
2016-06-17 19:01:11 +02:00
* (Available in Ansible 2.1) Install POSIX.1e filesystem acl support on the
managed host. If the temporary directory on the remote host is mounted with
POSIX acls enabled and the :command: `setfacl` tool is in the remote `` PATH ``
then Ansible will use POSIX acls to share the module file with the second
unprivileged user instead of having to make the file readable by everyone.
2016-03-21 22:17:53 +01:00
2016-03-16 18:58:16 +01:00
* Don't perform an action on the remote machine by becoming an unprivileged
user. Temporary files are protected by UNIX file permissions when you
2016-03-21 22:17:53 +01:00
`` become `` root or do not use `` become `` . In Ansible 2.1 and above, UNIX
file permissions are also secure if you make the connection to the managed
machine as root and then use `` become `` to an unprivileged account.
2016-06-17 19:01:11 +02:00
.. warn :: Although the Solaris ZFS filesystem has filesystem ACLs, the ACLs
are not POSIX.1e filesystem acls (they are NFSv4 ACLs instead). Ansible
cannot use these ACLs to manage its temp file permissions so you may have
to resort to `` allow_world_readable_tmpfiles `` if the remote machines use ZFS.
2016-03-21 22:17:53 +01:00
.. versionchanged :: 2.1
In addition to the additional means of doing this securely, Ansible 2.1 also
makes it harder to unknowingly do this insecurely. Whereas in Ansible 2.0.x
and below, Ansible will silently allow the insecure behaviour if it was unable
to find another way to share the files with the unprivileged user, in Ansible
2.1 and above Ansible defaults to issuing an error if it can't do this
2016-04-29 15:51:04 +02:00
securely. If you can't make any of the changes above to resolve the problem,
2016-03-21 22:17:53 +01:00
and you decide that the machine you're running on is secure enough for the
2016-04-29 15:51:04 +02:00
modules you want to run there to be world readable, you can turn on
2016-03-21 22:17:53 +01:00
`` allow_world_readable_tmpfiles `` in the :file: `ansible.cfg` file. Setting
`` allow_world_readable_tmpfiles `` will change this from an error into
a warning and allow the task to run as it did prior to 2.1.
2016-03-16 18:58:16 +01:00
Connection Plugin Support
=========================
Privilege escalation methods must also be supported by the connection plugin
used. Most connection plugins will warn if they do not support become. Some
will just ignore it as they always run as root (jail, chroot, etc).
Only one method may be enabled per host
=======================================
Methods cannot be chained. You cannot use `` sudo /bin/su - `` to become a user,
you need to have privileges to run the command as that user in sudo or be able
to su directly to it (the same for pbrun, pfexec or other supported methods).
Can't limit escalation to certain commands
==========================================
2014-11-24 22:36:31 +01:00
2016-03-16 18:58:16 +01:00
Privilege escalation permissions have to be general. Ansible does not always
use a specific command to do something but runs modules (code) from
a temporary file name which changes every time. If you have '/sbin/service'
or '/bin/chmod' as the allowed commands this will fail with ansible as those
paths won't match with the temporary file that ansible creates to run the
module.
2015-04-21 21:32:35 +02:00
2014-11-24 22:36:31 +01:00
.. seealso ::
`Mailing List <http://groups.google.com/group/ansible-project> `_
Questions? Help? Ideas? Stop by the list on Google Groups
`irc.freenode.net <http://irc.freenode.net> `_
#ansible IRC chat channel