路徑:/sxjteam/ansible/ansible-1.1
rpm -Uvh ansible-1.1-1.el6.noarch.rpm
安裝方法2:
sudo easy_install pip
sudo pip install ansible
免密匙登錄
$ssh-keygen -t rsa
$ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.25.26
為某與用戶生成免密鑰登錄
ssh-copy-id -i ~/.ssh/id_rsa.pub
/******************************************************************************
從設備:192.168.25.26
python 2.6.6
ansible 環境
/***********************************************************************************
功能0:詢問所有連接
ansible all -m ping
功能1:檢查從機連通情況
ansible slave -i /etc/ansible/hosts -m ping
功能2:檢查本機連通情況
ansible localhost -i /etc/ansible/hosts -m ping
功能3:以普通用戶詢問所有節點
ansible all -m ping -u luomin
功能4:引用sudo詢問所有節點
sudo ansible all -m ping -u luomin
ansible all -m ping -u bruce --sudo --sudo-user batman
比如:
(venv)[root@sxjteam bin]# ansible all -m ping -u luominmini
127.0.0.1 | FAILED => FAILED: Authentication failed.
192.168.25.12 | FAILED => FAILED: Authentication failed.
192.168.25.13 | FAILED => FAILED: Authentication failed.
192.168.25.11 | success >> {
}
功能5:引用模塊批量執行
ansible all -a "/bin/echo hello"
ansible all -a "/bin/mkdir hellodb"
功能6:查看功能copy模塊幫助
ansible-doc copy
功能7:將服務器端的start.sh 傳送到從機的/目錄下
(venv)[root@sxjteam /]# ansible all -m copy -a "src=/start.sh dest=/"
127.0.0.1 | FAILED => FAILED: Authentication failed.
192.168.25.26 | success >> {
}
功能8:將從機根目錄下的start.sh 權限改為777
(venv)[root@sxjteam /]# ansible slave -m file -a "dest=/start.sh mode=777 owner=root group=root" -s
192.168.25.26 | success >> {
}
功能9:在主機上遠程執行從機的start.sh腳本
(venv)[root@sxjteam /]# ansible slave -m script -a "/start.sh"
192.168.25.26 | success >> {
}
或:
ansible webservers -m command -a "/sbin/reboot -t now"
功能10:在主機上操作從機上的shell指令
(venv)[root@sxjteam /]# ansible slave -m shell -a "touch /tmp/test.sh"
192.168.25.26 | success | rc=0 >>
功能11:在從機上創建webgame組
(venv)[root@sxjteam /]# ansible all -m group -a "name=webgame state=present" -s
127.0.0.1 | FAILED => FAILED: Authentication failed.
192.168.25.26 | success >> {
}
功能12:在從機上創建用戶,并加入相關組以及創建用戶文件路徑
(venv)[root@sxjteam /]# ansible all -m user -a "name=luomin group=webgame home=/root/luomin state=present" -s
127.0.0.1 | FAILED => FAILED: Authentication failed.
192.168.25.26 | success >> {
}
功能13:yum遠程安裝linuxqq
(venv)[root@sxjteam /]# ansible slave -m yum -a "name=linuxqq state=latest" -s
192.168.25.26 | FAILED >> {
}
或:
[luomin@sxjteam /]$ sudo ansible slave -m yum -a "name=httpd state=latest" -s
192.168.25.11 | success >> {
}
192.168.25.12 | success >> {
}
.......
功能14:遠程開啟從機的httpd服務
(venv)[root@sxjteam /]# ansible slave -m service -a "name=httpd state=running" -s
192.168.25.26 | success >> {
}
功能15:并行5個線程來重啟從機
ansible slave -a "/usr/bin/reboot" -f 5
功能16:文件的復制
(venv)[root@sxjteam /]# ansible slave -m copy -a "src=/YUM dest=/"
192.168.25.26 | success >> {
}
功能17:創建目錄
(venv)[root@sxjteam /]# ansible slave -m file -a "dest=/newdir mode=600 owner=root group=root state=directory"
192.168.25.26 | success >> {
}
功能18:刪除從機下的newdir目錄
(venv)[root@sxjteam /]# ansible slave -m file -a "dest=/newdir state=absent"
192.168.25.26 | success >> {
}
功能19:創建用戶ans,密碼123456
(venv)[root@sxjteam /]# ansible slave -m user -a "name=ans password=123456"
192.168.25.26 | success >> {
}
功能20:服務狀態查看
A:查看是否已經開啟
ansible slave -m service -a "name=httpd state=started"
比如:
[luomin@sxjteam /]$ sudo ansible slave -m service -a "name=httpd state=started"
192.168.25.11 | success >> {
}//表示已經開啟
192.168.25.11 | success >> {
}//表示未開啟
B:查看是否已經關閉
ansible slave -m service -a "name=httpd state=stopped"
192.168.25.11 | success >> {
}//已經關閉
192.168.25.11 | success >> {
}//已經開啟
C:遠程重啟服務
ansible slave -m service -a "name=httpd state=restarted"
功能21:在同一組(webgame)的所有從機用10個線程來進行重啟
ansible webgame -a "/sbin/reboot" -f 10
功能22:以luomin賬戶執行
ansible webgame -a "/usr/bin/ls" -u luomin
功能23:以luomin賬戶sudo運行
ansible webgame -a "/usr/bin/foo" -u luomin --sudo [--ask-sudo-pass]
功能24:查看腳本幫助
ansible-playbook --help
上一篇 如何讓網站被搜索引擎更好地收錄
下一篇 網站布局關鍵詞在首頁分布的位置