Ansible中become的使用
(1)become
set to ‘true’/’yes’ to activate privilege escalation.
使用“true”或“yes”来表示启用这个特权,如:become=true
表示打开了become开关。
(2)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_user=root 设置为root账户,相当于我们以普通账户登入到远程主机时,再使用su - root切换为root账户。
(3)become_method
(at play or task level) overrides the default method set in ansible.cfg, set to sudo/su/pbrun/pfexec/doas/dzdo/ksu
become_method=su 表示用什么方式将普通账户切换到root或所需的其他账户,这里可以用su或sudo。
(4)become_flags
(at play or task level) permit to use specific flags for the tasks or role. One common use is to change user to nobody when the shell is set to no login. Added in Ansible 2.2.
表示允许为任务或角色使用特定的标志。一个常见的用法是在shell设置为不登录时将用户更改为nobody。ansible2.2版本中增加。
Ansible中become的使用举例
说明:
例如,要以非root用户身份连接到服务器时,需要root用户权限:
(1)To run a command as the apache user:( 以apache账户运行命令),play.yml脚本如下:
- name: Run a command as the apache user command: somecommand become: true become_user: apache(2)To do something as the nobody user when the shell is nologin:(在shell设置为不登录时将用户更改为nobody),play.yml脚本如下:
- name: Run a command as nobody command: somecommand become: true become_method: su become_user: nobody become_flags: '-s /bin/sh'
become变量在hosts使用
说明:允许您设置每个组和/或主机的选项,这些选项通常在hosts中定义,但可以用作正常变量来使用。
(1)ansible_become
equivalent of the become directive, decides if privilege escalation is used or not.(相当于成为指令,决定是否使用特权升级。)
(2)ansible_become_method
allows to set privilege escalation method(允许设置权限升级方法)
(3)ansible_become_user
allows to set the user you become through privilege escalation, does not imply ansible_become: True
(允许通过权限升级来设置你成为用户,记得同时使用ansible_become:true)
(4)ansible_become_pass
allows you to set the privilege escalation password
(即如你要使用root账户,则这里要写的就是root账户的密码!)
举例如下:
[iyunv@iZ2ze5n6gzuzcclehq1v9gZ ansible]# vim hosts[yunwei] ##运维 wtf192.168.2.1 ansible_ssh_user=product ansible_become_user=root ansible_become=true ansible_become_pass='123456'