Ansible提供兩種方式去完成任務(wù),一是 ad-hoc 命令,一是寫(xiě) Ansible playbook。前者可以解決一些簡(jiǎn)單的任務(wù), 后者解決較復(fù)雜的任務(wù)。
ad hoc——臨時(shí)的,在ansible中是指需要快速執(zhí)行,并且不需要保存的命令。說(shuō)白了就是執(zhí)行簡(jiǎn)單的命令—一條命令。
定義主機(jī)清單
cat /etc/ansible/hosts
[web]
192.168.77.129 ansible_ssh_user=root ansible_ssh_pass=123456
執(zhí)行shell
獲取web組里得eth0接口信息
ansible web -a "ifconfig eth0"
執(zhí)行ifconfig eth0 命令,ansible模塊 默認(rèn)是command,它不會(huì)通過(guò)shell進(jìn)行處理,所以像$ HOME和像“<”,“>”,“|”,“;” 和“&”將不工作(如果您需要這些功能,請(qǐng)使用shell模塊)。
以shell解釋器執(zhí)行腳本
ansible web -m shell -a "ifconfig eth0|grep addr"
以raw模塊執(zhí)行腳本
ansible web -m raw -a "ifconfig eth0|grep addr"
將本地腳本傳送到遠(yuǎn)程節(jié)點(diǎn)上運(yùn)行
ansible web -m script -a ip.sh
傳輸文件
拷貝本地的/etc/hosts 文件到web組所有主機(jī)的/tmp/hosts(空目錄除外)
ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts"
拷貝本地的ntp文件到目的地址,設(shè)置其用戶(hù)及權(quán)限,如果目標(biāo)地址存在相同的文件,則備份源文件。
ansible web -m copy -a "src=/mine/ntp.conf dest=/etc/ntp.conf owner=root group=root mode=644 backup=yes force=yes"
file 模塊允許更改文件的用戶(hù)及權(quán)限
ansible web -m file -a "dest=/tmp/a.txt mode=600 owner=user group=user"
使用file 模塊創(chuàng)建目錄,類(lèi)似mkdir -p
ansible web -m file -a "dest=/tmp/test mode=755 owner=user group=user state=directory"
使用file 模塊刪除文件或者目錄
ansible web -m file -a "dest=/tmp/test state=absent"
創(chuàng)建軟連接,并設(shè)置所屬用戶(hù)和用戶(hù)組
ansible web -m file -a "src=/file/to/link/to dest=/path/to/symlink owner=user group=user state=link"
touch 一個(gè)文件并添加用戶(hù)讀寫(xiě)權(quán)限,用戶(hù)組去除寫(xiě)執(zhí)行權(quán)限,其他組減去讀寫(xiě)執(zhí)行權(quán)限
ansible web -m file -a "path=/etc/foo.conf state=touch mode='u+rw,g-wx,o-rwx'"
管理軟件包
apt、yum 模塊分別用于管理Ubuntu 系列和RedHat 系列系統(tǒng)軟件包
更新倉(cāng)庫(kù)緩存,并安裝"foo"
ansible web -m apt -a "name=foo update_cache=yes"
刪除 "foo"
ansible web -m apt -a "name=foo state=absent"
安裝 "foo"
ansible web -m apt -a "name=foo state=present"
安裝 1.0版本的 "foo"
ansible web -m apt -a "name=foo=1.00 state=present"
安裝最新得"foo"
ansible web -m apt -a "name=foo state=latest"
注釋?zhuān)篈nsible 支持很多操作系統(tǒng)的軟件包管理,使用時(shí)-m 指定相應(yīng)的軟件包管理工具模塊,如果沒(méi)有這樣的模塊,可以自己定義類(lèi)似的模塊或者使用command 模塊來(lái)安裝軟件包
安裝 最新的 Apache
ansible web -m yum -a "name=httpd state=latest"
刪除apache
ansible web -m yum -a "name=httpd state=absent"
從testing 倉(cāng)庫(kù)中安裝最后一個(gè)版本得apache
ansible web -m yum -a "name=httpd enablerepo=testing state=present"
更新所有的包
ansible web -m yum -a "name=* state=latest"
安裝遠(yuǎn)程的rpm包
ansible web -m yum -a "name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present"
安裝 'Development tools' 包組
ansible web -m yum -a "name='@Development tools' state=present"
用戶(hù)和用戶(hù)組
添加用戶(hù) 'user'并設(shè)置其 uid 和主要的組'admin'
ansible web -m user -a "name=user comment='I am user ' uid=1040 group=admin"
添加用戶(hù) 'user'并設(shè)置其登陸shell,并將其假如admins和developers組
ansible web -m user -a "name=user shell=/bin/bash groups=admins,developers append=yes"
刪除用戶(hù) 'user '
ansible web -m user -a "name=user state=absent remove=yes"
創(chuàng)建 user用戶(hù)得 2048-bit SSH key,并存放在 ~user/.ssh/id_rsa
ansible web -m user -a "name=user generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa"
設(shè)置用戶(hù)過(guò)期日期
ansible web -m user -a "name=user shell=/bin/zsh groups=nobdy expires=1422403387"
創(chuàng)建test組,并設(shè)置git為1000
ansible web -m group -a "name=test gid=1000 state=present"
刪除test組
ansible web -m group -a "name=test state=absent"
源碼部署
Ansible 模塊能夠通知變更,當(dāng)代碼更新時(shí),可以告訴Ansible 做一些特定的任務(wù),比如從git 部署代碼然后重啟apache 服務(wù)等
ansible web-m git -a "repo=https://github.com/Icinga/icinga2.git dest=/tmp/myapp version=HEAD"
服務(wù)管理
確保web組所有主機(jī)的httpd 是啟動(dòng)的
ansible web-m service -a "name=httpd state=started"
重啟web組所有主機(jī)的httpd 服務(wù)
ansible web-m service -a "name=httpd state=restarted"
確保web組所有主機(jī)的httpd 是關(guān)閉的
ansible web-m service -a "name=httpd state=stopped"
后臺(tái)運(yùn)行
長(zhǎng)時(shí)間運(yùn)行的操作可以放到后臺(tái)執(zhí)行,ansible 會(huì)檢查任務(wù)的狀態(tài);在主機(jī)上執(zhí)行的同一個(gè)任
務(wù)會(huì)分配同一個(gè)job ID
后臺(tái)執(zhí)行命令3600s,-B 表示后臺(tái)執(zhí)行的時(shí)間
ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
檢查任務(wù)的狀態(tài)
ansible all -m async_status -a "jid=123456789"
后臺(tái)執(zhí)行命令最大時(shí)間是1800s 即30 分鐘,-P 每60s 檢查下?tīng)顟B(tài)默認(rèn)15s
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
定時(shí)任務(wù)
每天5點(diǎn),2點(diǎn)得時(shí)候執(zhí)行 ls -alh > /dev/null
ansible test -m cron -a "name='check dirs' minute='0' hour='5,2' job='ls -alh > /dev/null'"
搜集系統(tǒng)信息
搜集主機(jī)的所有系統(tǒng)信息
ansible all -m setup
搜集系統(tǒng)信息并以主機(jī)名為文件名分別保存在/tmp/facts 目錄
ansible all -m setup --tree /tmp/facts
搜集和內(nèi)存相關(guān)的信息
ansible all -m setup -a 'filter=ansible_*_mb'
搜集網(wǎng)卡信息
ansible all -m setup -a 'filter=ansible_eth[0-2]'
更多文章請(qǐng)看 Ansible 專(zhuān)題文章總覽