1. 程式人生 > >ansible的安裝配置和配合sshpass的使用

ansible的安裝配置和配合sshpass的使用

ansible安裝

  • centos使用yum安裝
sudo yum install ansible
  • Ubuntu使用apt-get安裝
sudo apt-get install ansible
  • MAC使用pip安裝
sudo pip install ansible

其他安裝方式,參考官網安裝文件吧。

ansible配置

我自己的配合檔案:

vim ~/.ansible.cfg

[defaults]
hostfile=$HOME/.ansible/hosts
deprecation_warnings=False
#host_key_checking=Falses
  • hostfile:host配置檔案的目錄,預設是在/etc/ansible/hosts
  • deprecation_warnings:不要警告資訊

hosts配置

在上面配置資訊hostfile的路徑找到hosts檔案編輯:

vim ~/.ansible/hosts

[test]
192.168.1.1
192.168.1.2
192.168.1.3

上面這種配置方式,是需要新增sshkey才可以使用的,這種使用方式更爽一些。

使用sshpass

如果想使用使用者名稱密碼來配置ansible,也是可以的,一樣是需要在hostfile的路徑找到hosts檔案編輯:

vim ~/.ansible/hosts

[test]
192.168.1.1 ansible_ssh_user=使用者名稱 ansible_ssh_pass=密碼
192.168.1.2 ansible_ssh_user=使用者名稱 ansible_ssh_pass=密碼
192.168.1.3 ansible_ssh_user=使用者名稱 ansible_ssh_pass=密碼

測試一下

無論是使用sshkey還是使用sshpass,都可以使用下面的測試:

ansible test -m ping

192.168.1.1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
192.168.1.2 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}
192.168.1.3| SUCCESS => {
    "changed": false,
    "ping": "pong"
}