1. 程式人生 > >自動化運維之Ansible應用基礎模組(超詳細)

自動化運維之Ansible應用基礎模組(超詳細)

Ansible是一種基於模組進行工作的框架結構。批量部署能力就是由Ansible所執行的模組實現的。簡而言之Ansible是基於“模組”完成各種“任務”的。

Ansible常用的核心模組

模組的基本語法如下

ansible [主機] [-m 模組] [-a args]  //命令格式
# ansible-doc -l //列出所有已安裝的模組  注:按q退出
# ansible-doc -s yum //-s列出yum模組描述資訊和操作動作

1.command 模組

command模組用於被管理主機上執行命令。
# ansible 192.168.92.156 -m command -a 'date' //指定IP執行date
# ansible node2 -m command -a 'date' //指定被管理主機中的分類執行date
# ansible all -m command -a 'date' //所有主機清單上的主機執行date
# ansible all -a 'ls /etc' //若省略-m選項,預設執行command模組

自動化運維之Ansible應用基礎模組(超詳細)

2.cron模組

cron模組用於定義任務計劃,兩種狀態(state):present 表示新增(可以省略),absent 表示移除。
# ansible-doc -s cron //檢視cron模組資訊
# ansible node2 -m cron -a 'minute="*/1" job="/bin/echo hello" name="test cron job"' //新增計劃任務
# ansible node2 -a 'crontab -l' //檢視計劃任務資訊
# ansible node2 -m cron -a 'name="test cron job" state=absent' //移除計劃任務

自動化運維之Ansible應用基礎模組(超詳細)

3.user模組

user模組用於建立新使用者和更改、刪除已存在的使用者。其中name選項用來指明建立的使用者名稱稱。
# ansible-doc -s user //檢視user模組資訊
# ansible node2 -m user -a 'name="test01"' //建立使用者test01
# ansible node2 -a 'tail /etc/passwd' //檢視test01 使用者
# ansible node2 -m user -a 'name="test01" state=absent' //刪除使用者test01

自動化運維之Ansible應用基礎模組(超詳細)

4.group模組

group模組用於對使用者組進行管理
# ansible-doc -s group //檢視group模組資訊
# ansible node3 -m group -a 'name=node3 gid=306
system=yes' //建立node3系統組
# ansible node3 -a 'tail /etc/group' //檢視組資訊
# ansible node3 -m user -a 'name=test01 uid=306 system=yes group=node3' //將test01使用者新增到node3組中
# ansible node3 -a 'id test01' //檢視使用者資訊

自動化運維之Ansible應用基礎模組(超詳細)

5.copy 模組

copy模組用於實現檔案複製和批量下發檔案,其中用src定義本地檔案路徑,使用dest定義被管理主機檔案路徑。
# ansible-doc -s copy //檢視copy模組資訊
# ansible node3 -m copy -a 'src=/etc/fstab dest=/opt/fstab.bk owner=root mode=640'
//將本地檔案fstab複製到被管理主機上,所有者為root,許可權為640
# ansible node3 -a 'ls -l /opt' //檢視檔案資訊
# ansible node3 -a 'cat /opt/fstab.bk' //檢視檔案內容
# ansible node3 -m copy -a 'content="HELLO ansible" dest=/opt/fstab.bk' 
//在fstab.bk檔案中寫入HELLO ansible
# ansible node3 -a 'cat /opt/fstab.bk' //檢視內容

自動化運維之Ansible應用基礎模組(超詳細)
自動化運維之Ansible應用基礎模組(超詳細)

6.file 模組

filem模組是用來設定檔案屬性。其中使用path指定檔案路徑,使用src定義原檔案路徑,使用name或dest來替換建立檔案的符號連結。
# ansible-doc -s file //檢視file模組資訊
# ansible node3 -m file -a 'owner=jerry group=jerry mode=644 path=/opt/fstab.bk'
//設定檔案fstab.bk的所屬主為jerry,所屬組為jerry,許可權644
# ansible node3 -m file -a 'path=/opt/fstab.link src=/opt/fstab.bk state=link'
//設定/opt/fstab.link為/opt/fstab.bk的連結檔案
# ansible node3 -m file -a 'path=/opt/fstab.link state=absent' //刪除一個檔案
# ansible node3 -m file -a 'path=/opt/abc state=touch' //建立一個空檔案

自動化運維之Ansible應用基礎模組(超詳細)

7.ping模組

ping模組用來檢測指定主機的連通性。
# ansible all -m ping  //檢測所有主機

自動化運維之Ansible應用基礎模組(超詳細)

8.yum模組

yum 模組負責在被管理主機上安裝與解除安裝軟體包,其中name指定安裝的軟體包,present、latest用來表示安裝,absent表示解除安裝。
# ansible-doc -s yum //檢視yum模組資訊
# ansible node3 -m yum -a 'name=httpd' //安裝httpd軟體包
# ansible node3 -m yum -a 'name=httpd state=absent' //解除安裝httpd軟體包

自動化運維之Ansible應用基礎模組(超詳細)

9.service 模組

service模組用來控制被管理服務的執行狀態,其中enabled表示是否開機自啟動,取值true或false;使用name定義服務名稱,使用state指定服務狀態,取值分別為started、stoped、restarted。
# ansible-doc -s service //檢視service模組資訊
# ansible node3 -a 'systemctl status httpd' //檢視httpd服務狀態
# ansible node3 -m service -a 'enabled=true name=httpd state=started' 
//啟動httpd服務並設定開機自動啟動

自動化運維之Ansible應用基礎模組(超詳細)

10.shell模組

shell模組可以在被管理主機上執行命令,支援管道符功能的複雜命令。
# ansible-doc -s shell //檢視shell模組資訊
# ansible node3 -m user -a 'name=tom' //建立使用者tom
# ansible node3 -m shell -a 'echo 123123|passwd --stdin tom' 
//使用無互動模式給使用者設定密碼

自動化運維之Ansible應用基礎模組(超詳細)

11.script 模組

script 模組可以將本地指令碼複製到被管理主機上進行執行,需要注意的是,使用相對路徑來指定指令碼。
# ansible-doc -s script //檢視script模組資訊
編輯一個本地指令碼test.sh,複製到被管理主機上執行。
# vim test.sh
   #!/bin/bash
   echo "this is test script" > /opt/test.txt
   chmod 666 /opt/test.txt
# chmod +x test.sh 
# ansible node3 -m script -a 'test.sh' //執行指令碼

自動化運維之Ansible應用基礎模組(超詳細)

12.setup 模組

setup 模組用來收集、檢視被管理主機的facts(facts是Ansible採集被管理主機裝置資訊的一個功能)。每個被管理主機在接受並執行管理命令之前,都鬼將自己的相關資訊(作業系統版本、IP地址等)傳送給控制主機。
# ansible-doc -s setup //檢視setup模組資訊
# ansible node3 -m setup  //檢視node3主機輸出資訊

自動化運維之Ansible應用基礎模組(超詳細)