一、環境準備
[root@master ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
hostname ip
Master master 10.0.0.28
Minion client01 10.0.0.20
Minion client02 10.0.0.21
二、安裝Ansible
- 更改yum 源:
[root@master ~]#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
2.軟件包安裝
EPEL 已經提供了ansible 所需的所有支持軟件包,所以在這裏使用epel 源進行安裝:
[root@master ~]#
rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
Retrieving http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.MYaX9K: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:epel-release warning: /etc/yum.repos.d/epel.repo created as /etc/yum.repos.d/epel.repo.rpmnew
########################################### [100%]
3.安裝Ansible
[root@master ~]# yum install ansible -y
4.免密要認證
[root@master ~]#ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.20
ssh-copy-id -i ~/.ssh/id_rsa.pub 10.0.0.21
cp /etc/ansible/hosts /etc/ansible/hosts.ori
> /etc/ansible/hosts
vim /etc/ansible/hosts
ansible 默認提供了很多模塊來供我們使用。在 Linux 中,我們可以通過 ansible-doc -l 命令查看到當前 ansible 都支持哪些模塊,通過 ansible-doc -s 模塊名 又可以查看該模塊有哪些參數可以使用。
5.建立host文件
Ansible 的host文件默認在/etc/ansible/ 這個目錄下面,採用rpm安裝的ansible會將該host文件作爲範例,其中提示ansible是支持域名和IP的兩種客戶端命名格式的,進過測試時沒有問題的,還支持不同的安裝分組方法,建議好好看看,這裏提供三臺機器,分爲master client01 client02 將他們分爲兩組 master 和slave
[root@master ~]# cat /etc/ansible/hosts
[localhost]
127.0.0.1
[slave]
10.0.0.20
10.0.0.21
6.測試ansible 的使用
在這裏使用ping模塊:
[root@master ~]# ansible slave -i /etc/ansible/hosts -m ping
10.0.0.20 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.0.0.21 | SUCCESS => {
"changed": false,
"ping": "pong"
}
}
解讀:從返回值分析,ansiable salve 接待你10.0.0.20,10.0.0.21 的ping
值成功。說明ansiable 已經能夠使用。
7.驗證是否支持域名解析測試:
tail -3 /etc/hosts
10.0.0.28 master.test.com
10.0.0.21 client02
10.0.0.20 agent.test.com
[root@master ~]# cat /etc/ansible/hosts
[localhost]
master.test.com
[slave]
client02
agent.test.com
[root@master ~]# ansible slave -i /etc/ansible/hosts -m ping
paramiko: The authenticity of host 'agent.test.com' can't be established.
The ssh-rsa key fingerprint is 3d906ef1d450e4cc7031aef5e8c296f6.
Are you sure you want to continue connecting (yes/no)?
yes
paramiko: The authenticity of host 'client02' can't be established.
The ssh-rsa key fingerprint is 3d906ef1d450e4cc7031aef5e8c296f6.
Are you sure you want to continue connecting (yes/no)?
agent.test.com | SUCCESS => {
"changed": false,
"ping": "pong"
}
yes
client02 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@master ~]# ansible slave -i /etc/ansible/hosts -m ping
agent.test.com | SUCCESS => {
"changed": false,
"ping": "pong"
}
client02 | SUCCESS => {
"changed": false,
"ping": "pong"
}
驗證是支持服務器本地域名解析的