Ansible常用模块介绍及使用
ansible常用命令解析:
? ? 查看當(dāng)前 ansible 都支持哪些模塊: ansible-doc -l
? ? ? ? ? ? ? ? 查看某一模塊可以使用的參數(shù):ansible-doc -s copy (eg:copy模塊)
ansible用法:
ansible 主機名 -m 模塊名 -a 模塊參數(shù)
-m:模塊的名字,如果執(zhí)行單一的命令不用加-m,默認(rèn)走command
-a: 模塊參數(shù),如果模塊是command,-a 后面跟的就是命令參數(shù)
-i : 指定hosts文件位置 ? ? ?默認(rèn):/etc/ansible/hosts
-f: 并行任務(wù)數(shù),一定是整數(shù)
-u:指定遠(yuǎn)程用戶,默認(rèn)是root
ping模塊
? ? ? ansible 192.168.118.14 -m ping
可以看到在不使用管道的時候shell和command兩個模塊是沒有分別的,但是使用了管道,可以看到command的管道是不生效的。
shell和command:這兩個模塊都是運行命令的模塊,區(qū)別是command模塊不支持shell變量和管道。
(1)、批量使用命令[command/shell]
1 | ????ansible?storm_cluster?-m?command?-a?'date'??#??如下圖 |
? ??
? ?# 通過這個命令可以直接將兩個測試環(huán)境的時間都顯示出來,success 表示成功執(zhí)行了。command
? ? 此參數(shù)表示“在遠(yuǎn)程主機上執(zhí)行命令”。
? ? 如此可以將date命令,換成/usr/local/tomcat/bin/./shutdown.sh等命令。
(2)、文件復(fù)制[copy] ? ? # ?復(fù)制文件到遠(yuǎn)程文件 ??
1 2 3 4 | ??????ansible?storm_cluster?-m?copy?-a?"\ ??????src=/alidata/www/WEB-INF1214.zip\ ??????dest=/alidata/www/WEB-INF1214.zip\ ??????owner=root?group=root?backup=yes";?????#??如下圖 |
? ? src:被復(fù)制到遠(yuǎn)程主機的本地文件,可以是絕對路徑,也可以是相對路徑。
? ? owner:文件所有者
? ? group:文件所屬用戶組
? ? dest:復(fù)制到遠(yuǎn)程的文件。必填項
? ? mode:文件權(quán)限。如文件為755權(quán)限,mode="0755"即可。
(3)、遠(yuǎn)程查看文件內(nèi)容 ??
1 | ?????[root@localhost?/]#?ansible?storm_cluster?-m?command?-a?"cat?/1.txt"????#??如下圖 |
? ??
(4)文件管理[file]
file:更改文件的用戶及權(quán)限,創(chuàng)建或刪除文件和目錄。
修改所有服務(wù)器的/tmp/test.txt文件的權(quán)限為755:
#?ansible?all?-m?file?-a?"dest=/tmp/test.txt?mode=755"
? 創(chuàng)建/tmp/test目錄:
? #?ansible?testservers?-m?file?-a?"dest=/tmp/test?mode=755?owner=root?group=root?state=directory"
??刪除/tmp/test目錄:
? #?ansible?testservers?-m?file?-a?"dest=/tmp/test.txt?state=absent"
?(5) 軟件包管理:【yum/apt】
atp(ubuntu),yum(redhat):
安裝一個軟件包(links):
#?ansible?testservers?-m?yum?-a?"name=curl state=present"
安裝軟件到最新版本
#?ansible?testservers?-m?yum?-a?"name=links?state=latest"?
??刪除一個軟件包:
#?ansible?testservers?-m?yum?-a?"name=links?state=absent"
(6) 用戶和用戶組[user/group]:
user:創(chuàng)建,修改,刪除用戶。
創(chuàng)建cmh用戶:
#?ansible?all?-m?user?-a?"name=cmh?password=123456"
刪除cmh用戶:
#?ansible?all?-m?user?-a?"name=cmh?state=absent"?
創(chuàng)建ansible組:
#?ansible 192.168.118.14 -m group -a "name=ansible gid=2016"
(7)服務(wù)管理[service]:
? ?service:啟動,重啟,關(guān)閉系統(tǒng)服務(wù)。
? ?關(guān)閉服務(wù):
?#?ansible?testservers?-m?service?-a?"name=nfs?state=stopped"
?開啟服務(wù):
?#?ansible?testservers?-m?service?-a?"name=nfs?state=started"
?重啟或者重新加載服務(wù):
?#?ansible?testservers?-m?service?-a?"name=nfs?state=restarted"
?#?ansible?testservers?-m?service?-a?"name=nfs?state=reloaded"
(8)收集系統(tǒng)信息[setup]:
收集匹配主機的所有信息:
#?ansible?all?-m?setup
收集信息并以主機名為文件保存在指定目錄中:
#?ansible?all?-m?setup?--tree?/tmp/facts
過濾信息:(收集內(nèi)存相關(guān))
#?ansible?all?-m?setup?-a?'filter=ansible_*_mb'
(9)定義任務(wù)計劃[cron]
??#?ansible all -m cron -a 'name="Cron job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/bin/ntpdate tiger.sina.com.cn"
(10)下載文件[get_url]
??#?ansible 192.168.118.14 -m get_url -a 'url=http://192.168.118.14/1.png dest=/tmp'
? (11)文件同步[ synchronize]
? ??ansible 192.168.118.14 -m synchronize -a 'src=/root/test dest=/tmp/ compress=yes'
? ?
http://docs.ansible.com/ansible/modules_by_category.html? # 更多的模塊請查看此鏈接
轉(zhuǎn)載于:https://blog.51cto.com/tfbaby/1902966
總結(jié)
以上是生活随笔為你收集整理的Ansible常用模块介绍及使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hadoop(ha)hbase(双mas
- 下一篇: typeof 与 js数据类型