Ansible提供兩種方式去完成任務(wù),一是 ad-hoc 命令,一是寫 Ansible playbook逗载。前者可以解決一些簡單的任務(wù)豁遭, 后者解決較復(fù)雜的任務(wù)。
ad hoc——臨時的膝宁,在ansible中是指需要快速執(zhí)行,并且不需要保存的命令根吁。說白了就是執(zhí)行簡單的命令—一條命令员淫。
定義主機清單
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模塊 默認是command击敌,它不會通過shell進行處理介返,所以像$ HOME和像“<”,“>”沃斤,“|”圣蝎,“;” 和“&”將不工作(如果您需要這些功能,請使用shell模塊)衡瓶。
以shell解釋器執(zhí)行腳本
ansible web -m shell -a "ifconfig eth0|grep addr"
以raw模塊執(zhí)行腳本
ansible web -m raw -a "ifconfig eth0|grep addr"
將本地腳本傳送到遠程節(jié)點上運行
ansible web -m script -a ip.sh
傳輸文件
拷貝本地的/etc/hosts 文件到web組所有主機的/tmp/hosts(空目錄除外)
ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts"
拷貝本地的ntp文件到目的地址徘公,設(shè)置其用戶及權(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 模塊允許更改文件的用戶及權(quán)限
ansible web -m file -a "dest=/tmp/a.txt mode=600 owner=user group=user"
使用file 模塊創(chuàng)建目錄坦袍,類似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è)置所屬用戶和用戶組
ansible web -m file -a "src=/file/to/link/to dest=/path/to/symlink owner=user group=user state=link"
touch 一個文件并添加用戶讀寫權(quán)限缭裆,用戶組去除寫執(zhí)行權(quán)限,其他組減去讀寫執(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)軟件包
更新倉庫緩存澈驼,并安裝"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"
注釋:Ansible 支持很多操作系統(tǒng)的軟件包管理,使用時-m 指定相應(yīng)的軟件包管理工具模塊筛武,如果沒有這樣的模塊缝其,可以自己定義類似的模塊或者使用command 模塊來安裝軟件包
安裝 最新的 Apache
ansible web -m yum -a "name=httpd state=latest"
刪除apache
ansible web -m yum -a "name=httpd state=absent"
從testing 倉庫中安裝最后一個版本得apache
ansible web -m yum -a "name=httpd enablerepo=testing state=present"
更新所有的包
ansible web -m yum -a "name=* state=latest"
安裝遠程的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"
用戶和用戶組
添加用戶 'user'并設(shè)置其 uid 和主要的組'admin'
ansible web -m user -a "name=user comment='I am user ' uid=1040 group=admin"
添加用戶 'user'并設(shè)置其登陸shell,并將其假如admins和developers組
ansible web -m user -a "name=user shell=/bin/bash groups=admins,developers append=yes"
刪除用戶 'user '
ansible web -m user -a "name=user state=absent remove=yes"
創(chuàng)建 user用戶得 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è)置用戶過期日期
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)代碼更新時待锈,可以告訴Ansible 做一些特定的任務(wù)漠其,比如從git 部署代碼然后重啟apache 服務(wù)等
ansible web-m git -a "repo=https://github.com/Icinga/icinga2.git dest=/tmp/myapp version=HEAD"
服務(wù)管理
確保web組所有主機的httpd 是啟動的
ansible web-m service -a "name=httpd state=started"
重啟web組所有主機的httpd 服務(wù)
ansible web-m service -a "name=httpd state=restarted"
確保web組所有主機的httpd 是關(guān)閉的
ansible web-m service -a "name=httpd state=stopped"
后臺運行
長時間運行的操作可以放到后臺執(zhí)行,ansible 會檢查任務(wù)的狀態(tài)竿音;在主機上執(zhí)行的同一個任
務(wù)會分配同一個job ID
后臺執(zhí)行命令3600s和屎,-B 表示后臺執(zhí)行的時間
ansible all -B 3600 -a "/usr/bin/long_running_operation --do-stuff"
檢查任務(wù)的狀態(tài)
ansible all -m async_status -a "jid=123456789"
后臺執(zhí)行命令最大時間是1800s 即30 分鐘,-P 每60s 檢查下狀態(tài)默認15s
ansible all -B 1800 -P 60 -a "/usr/bin/long_running_operation --do-stuff"
定時任務(wù)
每天5點春瞬,2點得時候執(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)信息
搜集主機的所有系統(tǒng)信息
ansible all -m setup
搜集系統(tǒng)信息并以主機名為文件名分別保存在/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]'
更多文章請看 Ansible 專題文章總覽