1. 程式人生 > 其它 >Ansible批量管理工具

Ansible批量管理工具

  • ansible 環境安裝部署
  • ansible 命令列模組
    • 1.command 模組
    • 2.shell 模組
    • 3.cron 模組
    • 4.user 模組
    • 5.group 模組
    • 6.copy 模組
    • 7.file 模組
    • 8.hostname 模組
    • 9.ping 模組
    • 10.yum 模組
    • 11.service/systemd 模組
    • 12.script 模組
    • 13.setup 模組
  • inventory 主機清單

Ansible是一個基於Python開發的配置管理和應用部署工具,現在也在自動化管理領域大放異彩。它融合了眾多老牌運維工具的優點,Pubbet和Saltstack能實現的功能,Ansible基本上都可以實現。

Ansible能批量配置、部署、管理上千臺主機。比如以前需要切換到每個主機上執行的一或多個操作,使用Ansible只需在固定的一臺Ansible控制節點上去完成所有主機的操作。

Ansible是基於模組工作的,它只是提供了一種執行框架,它本身沒有完成任務的能力,真正執行操作的是Ansible的模組, 比如copy模組用於拷貝檔案到遠端主機上,service模組用於管理服務的啟動、停止、重啟等。

Ansible其中一個比較鮮明的特性是Agentless,即無Agent的存在,它就像普通命令一樣,並非C/S軟體,也只需在某個作為控制節點的主機上安裝一次Ansible即可,通常它基於ssh連線來控制遠端主機,遠端主機上不需要安裝Ansible或其它額外的服務。

使用者在使用時,在伺服器終端輸入命令或者playbooks,會通過預定好的規則將playbook拆解為play,再組織成ansible可以識別的任務,呼叫模組和外掛,根據主機清單通過SSH將臨時檔案發給遠端的客戶端執行並返回結果,執行結束後自動刪除

Ansible的另一個比較鮮明的特性是它的絕大多數模組都具備冪等性(idempotence)。所謂冪等性,指的是多次操作或多次執行對系統資源的影響是一致的。比如執行 systemctl stop xxx 命令來停止服務,當發現要停止的目標服務已經處於停止狀態, 它什麼也不會做,所以多次停止的結果仍然是停止,不會改變結果,它是冪等的,而 systemctl restart xxx 是非冪等的。

Ansible的很多模組在執行時都會先判斷目標節點是否要執行任務,所以,可以放心大膽地讓Ansible去執行任務,重複執行某個任務絕大多數時候不會產生任何副作用。


---------- ansible 環境安裝部署 ----------
管理端:192.168.73.30 ansible
被管理端:192.168.73.40
被管理端:192.168.73.50

//管理端安裝 ansible

yum install -y epel-release //先安裝 epel 源
yum install -y ansible

  

//ansible 目錄結構
/etc/ansible/
├── ansible.cfg #ansible的配置檔案,一般無需修改
├── hosts #ansible的主機清單,用於儲存需要管理的遠端主機的相關資訊
└── roles/ #公共角色目錄

//配置主機清單

cd /etc/ansible
vim hosts
[webservers] #配置組名
192.168.73.40 #組裡包含的被管理的主機IP地址或主機名(主機名需要先修改/etc/hosts檔案)

[dbservers]
192.168.73.50

  

//配置金鑰對驗證

ssh-keygen -t rsa #一路回車,使用免密登入
ssh-copy-id [email protected]
ssh-copy-id [email protected]

  


--------- ansible 命令列模組 ---------
命令格式:ansible <組名> -m <模組> -a <引數列表>

ansible-doc -l #列出所有已安裝的模組,按q退出

1.command 模組
//在遠端主機執行命令,不支援管道,重定向等shell的特性。

ansible-doc -s command #-s 列出指定模組的描述資訊和操作動作

ansible 192.168.73.40 -m command -a 'date' #指定 ip 執行 date
ansible webservers -m command -a 'date' #指定組執行 date
ansible dbservers -m command -a 'date'
ansible all -m command -a 'date' #all 代表所有 hosts 主機
ansible all -a 'ls /' #如省略 -m 模組,則預設執行 command 模組

  

//常用的引數:
chdir:在遠端主機上執行命令前提前進入目錄
creates:判斷指定檔案是否存在,如果存在,不執行後面的操作
removes:判斷指定檔案是否存在,如果存在,執行後面的操作

ansible all -m command -a "chdir=/home ls ./"

2.shell 模組
//在遠端主機執行命令,相當於呼叫遠端主機的shell程序,然後在該shell下開啟一個子shell執行命令(支援管道符號等功能)
ansible-doc -s shell

ansible dbservers -m shell -a 'echo 123456 | passwd --stdin test'  #遠端修改使用者密碼
ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print $2}") | cut -d " " -f2'
ansible dbservers -m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")'

  

3.cron 模組
//在遠端主機定義任務計劃。其中有兩種狀態(state):present表示新增(可以省略),absent表示移除。
ansible-doc -s cron #按 q 退出

//常用的引數:
minute/hour/day/month/weekday:分/時/日/月/周
job:任務計劃要執行的命令
name:任務計劃的名稱

ansible webservers -m cron -a 'minute="*/1" job="/bin/echo helloworld" name="test crontab"'
ansible webservers -a 'crontab -l'
ansible webservers -m cron -a 'name="test crontab" state=absent' #移除計劃任務,假如該計劃任務沒有取名字,name=None即可

  

4.user 模組
//使用者管理的模組
ansible-doc -s user

//常用的引數:
name:使用者名稱,必選引數
state=present|absent:建立賬號或者刪除賬號,present表示建立,absent表示刪除
system=yes|no:是否為系統賬號
uid:使用者uid
group:使用者基本組
shell:預設使用的shell
move_home=yse|no:如果設定的家目錄已經存在,是否將已經存在的家目錄進行移動
password:使用者的密碼,建議使用加密後的字串
comment:使用者的註釋資訊
remove=yes|no:當state=absent時,是否刪除使用者的家目錄

ansible dbservers -m user -a 'name="test01"' #建立使用者test01
ansible dbservers -m command -a 'tail /etc/passwd'
ansible dbservers -m user -a 'name="test01" state=absent' #刪除使用者test01

  

5.group 模組
//使用者組管理的模組
ansible-doc -s group

ansible dbservers -m group -a 'name=mysql gid=306 system=yes' #建立mysql組
ansible dbservers -a 'tail /etc/group'
ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql' #將test01使用者新增到mysql組中
ansible dbservers -a 'tail /etc/passwd'
ansible dbservers -a 'id test01'

  

6.copy 模組
//用於複製指定主機檔案到遠端主機的
ansible-doc -s copy

//常用的引數:
dest:指出複製檔案的目標及位置,使用絕對路徑,如果是源目錄,指目標也要是目錄,如果目標檔案已經存在會覆蓋原有的內容
src:指出原始檔的路徑,可以使用相對路徑或絕對路徑,支援直接指定目錄,如果源是目錄則目標也要是目錄
mode:指出複製時,目標檔案的許可權
owner:指出複製時,目標檔案的屬主
group:指出複製時,目標檔案的屬組
content:指出複製到目標主機上的內容,不能與src一起使用

ansible dbservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=root mode=640'
ansible dbservers -a 'ls -l /opt'
ansible dbservers -a 'cat /opt/fstab.bak'

ansible dbservers -m copy -a 'content="helloworld" dest=/opt/hello.txt' #將helloworld寫入/opt/hello.txt檔案中
ansible dbservers -a 'cat /opt/hello.txt'

  

7.file 模組
//設定檔案屬性
ansible-doc -s file

ansible dbservers -m file -a 'owner=test01 group=mysql mode=644 path=/opt/fstab.bak' #修改檔案的屬主屬組許可權等
ansible dbservers -m file -a 'path=/opt/fstab.link src=/opt/fstab.bak state=link' #設定/opt/fstab.link為/opt/fstab.bak的連結檔案
ansible dbservers -m file -a "path=/opt/abc.txt state=touch" #建立一個檔案
ansible dbservers -m file -a "path=/opt/abc.txt state=absent" #刪除一個檔案

  

8.hostname 模組
//用於管理遠端主機上的主機名

ansible dbservers -m hostname -a "name=mysql01"

  

9.ping 模組
//檢測遠端主機的連通性

ansible all -m ping

  

10.yum 模組
//在遠端主機上安裝與解除安裝軟體包
ansible-doc -s yum

ansible webservers -m yum -a 'name=httpd' #安裝服務,安裝前先檢查目標客戶端的yum源
ansible webservers -m yum -a 'name=httpd state=absent' #解除安裝服務

  

11.service/systemd 模組
//用於管理遠端主機上的管理服務的執行狀態
ansible-doc -s service

//常用的引數:
name:被管理的服務名稱
state=started|stopped|restarted:動作包含啟動關閉或者重啟
enabled=yes|no:表示是否設定該服務開機自啟
runlevel:如果設定了enabled開機自啟去,則要定義在哪些執行目標下自啟動

ansible webservers -a 'systemctl status httpd' #檢視web伺服器httpd執行狀態
ansible webservers -m service -a 'enabled=true name=httpd state=started' #啟動httpd服務

  

12.script 模組
//實現遠端批量執行本地的 shell 指令碼
ansible-doc -s script

vim test.sh
#!/bin/bash
echo "hello ansible from script" > /opt/script.txt

chmod +x test.sh
ansible webservers -m script -a 'test.sh'
ansible webservers -a 'cat /opt/script.txt'

  

13.setup 模組
//facts 元件是用來收集被管理節點資訊的,使用 setup 模組可以獲取這些資訊
ansible-doc -s setup

ansible webservers -m setup #獲取mysql組主機的facts資訊
ansible dbservers -m setup -a 'filter=*ipv4' #使用filter可以篩選指定的facts資訊

  


--------- inventory 主機清單 ---------
//Inventory支援對主機進行分組,每個組內可以定義多個主機,每個主機都可以定義在任何一個或多個主機組內。

//如果是名稱類似的主機,可以使用列表的方式標識各個主機。

vim /etc/ansible/hosts
[webservers]
192.168.73.40:2222 #冒號後定義遠端連線埠,預設是 ssh 的 22 埠
192.168.80.1[2:5]

[dbservers]
db-[a:f].example.org #支援匹配 a~f

 

//inventory 中的變數

變數名 含義
ansible_host ansible 連線節點時的IP地址
ansible_port 連線對方的埠號,ssh連線時預設為22
ansible_user 連線對方主機時使用的主機名。不指定時,將使用執行ansible或ansible-playbook命令的使用者
ansible_password 連線時的使用者的ssh密碼,僅在未使用金鑰對驗證的情況下有效
ansible_ssh_private_key_file 指定金鑰認證ssh連線時的私鑰檔案
ansible_ssh_common_args 提供給ssh、sftp、scp命令的額外引數
ansible_become 允許進行許可權提升
ansible_become_method 指定提升許可權的方式,例如可使用sudo/su/runas等方式
ansible_become_user 提升為哪個使用者的許可權,預設提升為root
ansible_become_password 提升為指定使用者許可權時的密碼

(1)主機變數

[webservers]
192.168.73.40 ansible_port=22 ansible_user=root ansible_password=abc1234

  

(2)組變數

[webservers:vars] #表示為 webservers 組內所有主機定義變數
ansible_user=root
ansible_password=abc1234

[all:vars] #表示為所有組內的所有主機定義變數
ansible_port=22

  

(3)組巢狀

[nginx]
192.168.80.20
192.168.80.21
192.168.80.22

[apache]
192.168.80.3[0:3]

[webs:children] #表示為 webs 主機組中包含了 nginx 組和 apache 組內的所有主機
nginx
apache