日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Ansible 七(ad hoc任务)

發布時間:2023/12/20 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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*"


轉載于:https://blog.51cto.com/506554897/1955210

總結

以上是生活随笔為你收集整理的Ansible 七(ad hoc任务)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。