用ansible 來管理遠程的主機,最大的好處是方便,ansible不用在遠程的主機上安裝ansible的客戶端,ansible只要能經過ssh鏈接上遠程主機就bash
能對它進行管理。也就是說ansible是經過ssh來鏈接遠程主機的,因而就引出了鏈接到遠程主機的兩種認證方式,一種是用戶名+密碼,另外一種是ssh
~.ssh/id_rsa ~.ssh/id_rsa.pub 經過祕鑰對的方式來認證;天然ansible也是逃不出已經有的條條框框。ide
1、ansible經過用戶名+密碼方式認證的相關變量:this
ansible_host=遠程主機ip ansible_port=遠程主機的ssh端口 ansible_user=遠程主機中的一個用戶名 ansible_ssh_pass=遠程主機用戶所對應的密碼
在這裏須要在主機上安裝sshpass:spa
sshpass下載地址:http://sourceforge.net/projects/sshpass/.net
運行下列命令安裝code
tar -zxvf sshpass-1.05.tar.gz cd sshpass-1.05 #./configure ./configure --prefix=/opt/sshpass #指定安裝目錄 --prefix=/opt/sshpass(也可不指定) make && make install
安裝完成後輸入sshpass出現以下提示即安裝成功:orm
# sshpass -h Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters -f filename Take password to use from file -d number Use number as file descriptor for getting password -p password Provide password as argument (security unwise) -e Password is passed as env-var "SSHPASS" With no parameters - password will be taken from stdin -h Show help (this screen) -V Print version information At most one of -f, -d, -p or -e should be used
2、ansible經過祕鑰認證相關的變量:blog
ansible_host=遠程主機ip ansible_port=遠程主機的ssh端口 ansible_ssh_private_key_file=本地用戶的ssh密鑰文件(私鑰)
注意:ip
[root@ansible_master ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub 10.1.6.72 -bash: ssh-copy-id: command not found 報錯了 解決方法: yum -y install openssh-clients
3、有時候遠程用戶沒有"權限"完成特定的操做,它要sudo一下才行,ansible中就要設置一下了:
ansible_become_user=root ansible_become=true
ansible_become_user=root 說的是到時候su到root身份執行操做,注意單單只是設置ansible_become_user是不會su的,還有一個開關控制着
是不啓動su,這個開關就是ansible_become!