Ansible 七(ad hoc任務)
Ansible 七(ad hoc任務)
ansible任務
ad hoc任務就是執行shell命令、或shell腳本。
ansible ad-hoc命令
可以執行一些簡單的命令,不需要將這些執行的命令特別保存下來。
適合執行簡單的命令。
ansible playbook
可以解決比較復雜的任務,可以將命令保存下來。
適合執行配置管理或部署客戶機。
並行性和shell命令
重啟webservers主機組裏的所以機器,每次重啟10臺
ansible webservers -a "/sbin/reboot" -f 10
以test01用戶身份在webservers組的所以主機運行foo命令
ansible webservers -a "/usr/bin/foo" -u test01
以test01用戶身份sudo執行foo(如果有sudo密碼加上--ask-sudo-pass(-k))
ansible webservers -a "/usr/bin/foo" -u test01 --sudo [--ask-sudo-pass]
也可以sudo到其他用戶執行命令非root
ansible webservers -a "/usr/bin/foo" -u username -U otheruser [--ask-sudo-pass]
前面命令用到的-f 10選項表示使用10個並行的進程。
這個選項也可以在ansible的配置文件中設置,默認是5。
如果主機數大於設置的並發數,ansible會自行協調,花的時間稍微長一點。
ansible有許多模塊,默認是命令模塊:“command” ,這個模塊不支持shell變量和管道等。
我們可以通過-m選項來指定不同的模塊。例如shell模塊
模塊相關了解:http://www.ansible.com.cn/docs/modules.html
使用shell模塊在客戶主機上執行命令
ansible webservers -m shell -a "echo $TERM" #查看當前系統是linux
文件傳輸(file transfer)
拷貝本地的/etc/hosts文件到webservers主機組所以主機的/tmp/hosts;
ansible webservers -m copy -a "src=/etc/hosts dest=/tmp/hosts"
若使用playbooks,則可以利用template模塊來做到跟進一步的事情:(請參見 module 和 playbook 的文檔)
使用file模塊修改文件的屬主和權限(在這裏可替換為copy模塊是等效的)
ansible webservers -m file -a "dest=/tmp/hosts mode=600" ansible webservers -m file -a "dest=/tmp/hosts mode=600 owner=test01 group=test01"
#修改遠程客戶機/tmp/hosts權限為600,屬主和屬組都為test01
使用file模塊創建目錄,與執行mkdir -p 效果類似
ansible webservers -m file -a "dest=/tmp/abc mode=755 owner=test01 group=test01 state=directory"
#在webservers組裏的所以主機上創建abc目錄,權限為755,屬主和屬組都為test01
使用file模塊刪除目錄(遞歸的刪除)和刪除文件
ansible webservers -m file -a "dest=/tmp/abc state=absent"
管理軟件包(managing packages)
ansible提供對yum和apt的支持,這裏是關於yum的示例。
確認httpd軟件包安裝,不更新(如果已安裝不更新)
ansible webservers -m yum -a "name=httpd state=present" 返回如下: 1.1.1.2 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "httpd-2.4.6-45.el7.centos.4.x86_64 providing httpd is already installed" ] } 1.1.1.3 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "httpd-2.4.6-45.el7.centos.4.x86_64 providing httpd is already installed" ] }
確認httpd軟件包的版本
ansible webservers -m yum -a "name=httpd-2.4* state=present"
確認httpd軟件包更新到最新版本
ansible webservers -m yum -a "name=httpd state=latest" 返回如下: 1.1.1.2 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "All packages providing httpd are up to date", "" ] } 1.1.1.3 | SUCCESS => { "changed": false, "msg": "", "rc": 0, "results": [ "All packages providing httpd are up to date", #所有的軟件包提供的服務器是最新的 "" ] }
確認httpd軟件包還沒有安裝(卸載httpd)
ansible webservers -m yum -a "name=httpd state=absent"
用戶和組(user and groups)
使用user模塊可以方便的創建用戶、刪除用戶、或管理現有的用戶
#創建用戶test02
ansible all -m user -a "name=test02 password=123456789"
#刪除用戶test02
ansible all -m user -a "name=test02 state=absent"
源碼部署
直接使用 git 部署 webapp:
ansible模塊能夠通知變更,當代碼更新時,可以告訴ansible做一些特定的任務。
比如從git部署代碼然後重啟apache服務等
ansible webservers -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"
服務管理(managing services)
安裝httpd服務
ansible all -m yum -a "name=httpd state=present"
啟動httpd服務
ansible all -m service -a "name=httpd state=started"
重啟httpd服務
ansible all -m service -a "name=httpd state=restarted"
關閉httpd服務
ansible all -m service -a "name=httpd state=stopped"
卸載httpd服務
ansible webservers -m yum -a "name=httpd state=absent"
後臺運行(需要長時間運行的命令Time Limited Background Operations)
後臺執行命令3600s,-B表示後臺執行的時間
ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
檢查任務的狀態
ansible all -m async_status -a "jid=123456789"
後臺執行命令最大時間是1800s 即30 分鐘,-P 每60s 檢查下狀態默認15s
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
搜集系統信息(gathering facts)
搜集主機的所以系統信息
ansible webservers -m setup
搜集系統信息並以主機名為文件名分別保存在/tmp/facts目錄
ansible webservers -m setup --tree /tmp/facts
#搜集系統版本信息
ansible webservers -m setup -a "filter=ansible_distribution*"
搜集和內存相關的信息
ansible webservers -m setup -a "filter=ansible_*_mb"
搜集和cpu相關的信息
ansible webservers -m setup -a "filter=ansible_processor*"
搜集網卡信息
ansible webservers -m setup -a "filter=ansible_eth*"
本文出自 “506554897” 博客,請務必保留此出處http://506554897.blog.51cto.com/2823970/1955210
Ansible 七(ad hoc任務)