一。 ansible介紹
ansbile是基於python的強大的批量命令執行工具,在管理數量龐大的集羣中,有很強大的功能,而且體積小,依賴少,大有一統江湖的趨勢。
二。安裝
RHEL6中,提供了ansible的二進制包,在使用CentOS6的時候,可能需要添加epel源,具體如何添加,請自行百度
# yum install ansible
安裝後會在/etc/ansible/目錄下生成2個文件 ansible.cfg hosts
三。使用
演示環境介紹:3臺節點,分別爲node1,node2,node3,在/etc/hosts裏添加如下信息
172.16.0.1 node1 172.16.0.2 node2 172.16.0.3 node3
其中只需要1檯安裝ansible,這裏選用node1做“host”
1.node1生成密匙,並將公匙推送給node2和node3
# ssh-copy-id node{1,2,3}(注意本機也要傳)
2.編輯/etc/ansible/hosts,將內容全部註釋,添加如下信息
[test] node1 node2 node3
必須保證/etc/hosts裏面有主機名對應的IP
3.查看ansible幫助文檔
#man ansible
Usage: ansible <host-pattern> [options]
4.進行測試
#ansible test -a "date"
5.ansible常見模塊及其功能
yum,user,common
#yum all -a "ntpdate 172.16.0.100" (將所有節點與時間服務器同步,默認使用common模塊,可省略) #yum all -m yum -a “name=htop state=present" (使用yum模塊在所有節點上安裝htop,關於yum模塊的使用, 可以man ansible.yum) #yum all -m cron -a 'name=time minute="*/3" job="ntpdate 172.16.0.100 &>/dev/null"' (給所有節點制定cron任務,每3分鐘更新一次時間)
具體其他用法可自行man ansible.<module>