ansible使用2-命令

併發與shellgit

# bruce用戶身份,-m指定模塊名稱,默認模塊名command,all全部目標主機,也能夠指定組名或者主機名
ansible all -m ping -u bruce

# bruce用戶身份,sudoing到root用戶
ansible all -m ping -u bruce --sudo

# bruce用戶身份,sudoing到batman用戶,-U(--sudo-user)指定sudo用戶,-K(--ask-sudo-pass)指定sudo用戶密碼
ansible all -m ping -u bruce --sudo -U batman

# -f指定併發數, command模塊不支持變量
ansible atlanta -m command -a '/sbin/reboot' -f 10

# -k指定用戶密碼,-a模塊參數,shell模塊支持變量,命令參數必須使用雙引號
ansible all -u root -k -m shell -a "/bin/echo $i"

文件傳輸web

# copy模塊能夠拷貝文件
ansible atlanta -m copy -a 'src=/etc/hosts dest=/tmp/hosts'

# file模塊能夠改變文件屬性,創建刪除目錄文件
ansible webservers -m file -a 'dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan'
ansible webservers -m file -a 'dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory'
ansible webservers -m file -a 'dest=/path/to/c state=absent'

包管理(yum模塊)shell

ansible webservers -m yum -a 'name=acme state=present' # 確保acme包被安裝,可是不升級
ansible webservers -m yum -a 'name=acme-1.5 state=present' # 指定acme包版本號 
ansible webservers -m yum -a 'name=acme state=latest' # acme包升級到最新版本
ansible webservers -m yum -a 'name=acme state=absent' # 確保acme包不被安裝

用戶&組(user&group模塊)bash

ansible all -m user -a 'name=foo password=<crypted password here>' # 創建用戶
ansible all -m user -a 'name=foo state=absent' # 刪除用戶或者組

版本控制工具(git模塊)併發

ansible webservers -m git -a 'repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD'

服務管理(service模塊)app

ansible webservers -m service -a 'name=httpd state=started' #啓動
ansible webservers -m service -a 'name=httpd state=restarted' #重啓
ansible webservers -m service -a 'name=httpd state=stopped' #中止

有時限的後臺操做async

# 後臺運行long_running_operation命令,時間爲1小時(3600s),系統會給執行同一任務主機一個jid
ansible all -B 3600 -a '/usr/bin/long_running_operation --do-stuff'
# 輪詢任務的執行狀態
ansible all -m async_status -a 'jid=123456789'
# 後臺運行時間1800秒,每60分鐘輪詢檢查一次
ansible all -B 1800 -P 60 -a '/usr/bin/long_running_operation --do-stuff'

系統facts(setup模塊)工具

# 獲取目標主機信息
ansible all -m setup
相關文章
相關標籤/搜索