# ansible sudo 問題
官方下載centos7.6fcow2鏡像不給直接遠程ssh了,所以必須sudo,但是有的命令sudo也解決不了的如管道重定向還有多個命令組合。
解決辦法:
vim ansible.cfg
[defaults]
inventory=hosts
forks=10
host_key_checking=False
[privilege_escalation]
become=yes
become_method=sudo
become_user=root
vim hosts
[masters]
10.1.1.36
[nodes]
10.1.1.39
10.1.1.38
[k8s:children]
masters
nodes
[k8s:vars]
ansible_ssh_user="centos"
之后使用直接就是root權限了。
k8s# ansible k8s -m shell -a "whoami"
10.1.1.38 | SUCCESS | rc=0 >>
root
10.1.1.36 | SUCCESS | rc=0 >>
root
10.1.1.39 | SUCCESS | rc=0 >>
root
網上摘錄的一些使用說明:
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
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賬戶的密碼!)
舉例如下:
`vim hosts`
[yunwei]
192.168.2.1 ansible_ssh_user=product ansible_become_user=root ansible_become=true ansible_become_method=sudo ansible_become_pass='123456'
想了解更多詳情:
https://stackoverflow.com/questions/29966201/ansible-1-9-1-become-and-sudo-issue/30555969
Become (Privilege Escalation)
https://docs.ansible.com/ansible/2.4/become.html