ansible常用模組詳解

2021-09-29 20:12:07 字數 3662 閱讀 4291

1.模組介紹

明確一點:模組的執行就類似是linux命令的一條命令,就單單的是為了執行一條語句,不是批量的操作,批量操作需要用到playbook內類似shell編寫指令碼進行批量。

1.1 模組的使用方法

檢視ansible支援的所有模組:ansible-doc -l 然後在 ansible-doc -l | grep [ module_name ] 找到想用的模組名字 ;再使用ansible-doc -s [module_name]來檢視此模組的用法,也可以使用ansible-doc help module來檢視該模組更詳細的資訊。下面給出

官方模組列表和說明:

1.2 模組注意事項

關於模組的使用方法,需要注意的是"state"。很多模組都會有該選項,且其值幾乎都包含有"present"和"absent",表示肯定和否定的意思,

另ansible的多數的模組都是具有冪等性的,只有極少數模組如shell和command模組不具備冪等性。所謂的冪等性是指多次執行同乙個操作不會影響最終結果,ansible具有冪等性的模組在執行時,都會自動判斷是否要執行。

例如: file模組建立目錄時,如果建立過的目錄已經建立過了,則再次或多次執行安裝操作都不會真正的執行下去。

再例如:copy模組拷貝檔案時,如果目標主機上已經有了完全相同的檔案,則多次執行copy模組不會真正的拷貝。

2.常用模組

2.1,ping模組:測試節點連通性示例如下

2.2 ,hostname模組,用於更改節點主機名稱示例如下

注意:(1)host模組不會修改/etc/hosts檔案中的主機名解析,注意修改

(2)批量修改主機名時最好加變數,防止所有主機名一致

2.3 .2.4 copy複製模組

功能:複製檔案到遠端主機,這裡插張圖

將本地的nginx檔案 複製到遠端的節點根目錄下,如果檔案存在就優先備份。示例如下

驗證是否成功

content 簡單生成檔案內容並驗證:

2.4 file檔案模組

功能:建立目錄,管理目錄和檔案屬性。

注意:root@log1 ~]# ansible all -m cron -a 『minute=* weekday=1,3,5 job="/usr/bin/wall warning" name=test』 -o

192.168.188.136 | success =>

[root@log1 ~]# ansible all -m shell -a 『crontab -l』

192.168.188.136 | success | rc=0 >>

#ansible: test

[root@log1 ~]# ansible all -m cron -a 『disabled=true job="/usr/bin/wall warning" name=test』

192.168.188.136 | success =>

[root@log1 ~]# ansible all -m shell -a 『crontab -l』

192.168.188.136 | success | rc=0 >>

#ansible: test

#* * * * * /usr/bin/wall warning

[root@log1 ~]# ansible all -m cron -a 『disabled=false job="/usr/bin/wall warning" name=test』

192.168.188.136 | success =>

[root@log1 ~]# ansible all -m shell -a 『crontab -l』

192.168.188.136 | success | rc=0 >>

#ansible: test

[root@log1 ~]# ansible all -m cron -a 『job="/usr/bin/wall warning" name=test state=absent』

192.168.188.136 | success =>

[root@log1 ~]# ansible all -m shell -a 『crontab -l』

192.168.188.136 | success | rc=0 >>

1.2 自定義的定時任務不能使用crontab-l檢視

建立乙個job,每5分鐘進行一次時間同步,並且自定義cron_file。

ansible all -m cron -a 『name=「ntpdate_time」 job="/usr/sbin/ntpdate ntp1.aliyun.com" cron_file=ntpdate_cron user=root minute=/5』 -o

移除乙個job,要求name必須匹配,需要同時指定cron_file和user。

ansible all -m cron -a 『name=「ntpdate_time」 state=absent cron_file=ntpdate_cron user=root』 -o

#檢視定時任務

ansible all -m shell -a 『cat /etc/cron.d/ntpdate_cron』

執行過程:

[root@log1 ~]# ansible all -m cron -a 'name=「ntpdate_time」 job="/usr/sbin/ntpdate ntp1.aliyun.com" cron_file=ntpdate_cron user=root minute=/5』 -o

192.168.188.136 | success =>

[root@log1 ~]# ansible all -m shell -a 『cat /etc/cron.d/ntpdate_cron』

192.168.188.136 | success | rc=0 >>

#ansible: ntpdate_time

*/5 * * * * root /usr/sbin/ntpdate ntp1.aliyun.com

[root@log1 ~]# ansible all -m cron -a 『name=「ntpdate_time」 state=absent cron_file=ntpdate_cron user=root』 -o

192.168.188.136 | success =>

[root@log1 ~]# ansible all -m shell -a 『cat /etc/cron.d/ntpdate_cron』

192.168.188.136 | success | rc=0 >>

參考連線:

Ansible模組詳解

上面我們已經提到過 ansible 是基於 ssh 協議實現的,所以其配置公私鑰的方式與 ssh 協議的方式相同,具體操作步驟如下 生成金鑰 root localhost ssh keygen t rsa 向主機分發私鑰 root localhost ssh copy id root 10.0.0....

Ansible 常用檔案模組使用詳解

1 file模組 1 file模組常用的引數列表 setype設定目標檔案安全上下文屬性 owner裝置目標檔案的所屬主 group設定目標檔案的所屬組 mode設定檔案的許可權 src指定鏈結檔案的路徑 2 演示例項 2 fetch模組 1 fetch模組常用的引數列表 2 演示例項 3 line...

Ansible常用模組

1.ping模組2.ansible command模組是ansible預設模組,主要用於執行linux基礎命令,可以執行遠端伺服器命令執行 任務執行等操作。但command模組不支援變數 重定向 管道符等,這些操作需要用shell模組執行 command模組使用詳解 chdir 執行命令前,切換到目...