Ansible 小手冊(cè)系列 七(Ad-hoc)

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)題文章總覽

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一.ansible (1) ansible: ansible是一款新出現(xiàn)的自動(dòng)化運(yùn)維系統(tǒng),基于python開(kāi)發(fā)并集...
    楠人幫閱讀 2,129評(píng)論 0 8
  • 1. 什么是Ansible,它有什么用? Ansible它是個(gè)集配置管理和應(yīng)用部署于一體的自動(dòng)化運(yùn)維工具。 應(yīng)用情...
    午覺(jué)不眠Orz閱讀 1,716評(píng)論 0 0
  • Ansible 安裝:apt-get install python-crypto python-lxml pipp...
    Jackzzg閱讀 1,805評(píng)論 0 3
  • 藥苦,沒(méi)你苦,吃藥不會(huì)哭,可是,想你,會(huì)。 一切的一切都是你給的借口,我一開(kāi)始就懂,可是直到現(xiàn)在卻還想重陷其中。
    恩宜淡威宜嚴(yán)閱讀 212評(píng)論 0 0
  • 記得前兩年流行的一句話(huà):“世界那么大,我想去看看。”就是這條短短的辭職信火了。打開(kāi)微博,轉(zhuǎn)的很火,朋友圈也在刷屏...
    孤子翼閱讀 624評(píng)論 0 2

友情鏈接更多精彩內(nèi)容