天天看點

配置檔案ansible.cfg中Become (Privilege Escalation)

ansible版本及系統環境

配置檔案ansible.cfg中Become (Privilege Escalation)
配置檔案ansible.cfg中Become (Privilege Escalation)

Ansible中become的說明

Ansible允許你成為另一個使用者,與登入到本機的使用者或遠端使用者不同。這是使用現有的特權更新工具(privilege escalation tools)完成的,您可能已經使用或已經配置了這些工具,如sudo,su,pfexec,doas,pbrun,dzdo,ksu等。

說明:

(1)在1.9 Ansible之前,大多數情況下都允許使用sudo和有限的su來允許登入/遠端使用者成為不同的使用者并執行任務,用第二個使用者的權限建立資源。從1.9開始become代替舊的sudo / su,同時仍然向後相容。這個新系統也使得添加諸如pbrun(Powerbroker),pfexec,dzdo(Centrify)等其他特權更新工具變得更加容易。

(2)變量和指令是獨立的,即設定become_user并不是設定become。

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

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賬戶的密碼!)

舉例如下:

[root@iZ2ze5n6gzuzcclehq1v9gZ ansible]# vim hosts

繼續閱讀