自動化運維工具Ansible(常用模組與主機清單配置)
Ansible
Ansible是一個基於Python開發的配置管理和應用部署工具,現在也在自動化管理領域大放異彩。它融合了眾多老牌運維工具的優點,Pubbet和Saltstack能實現的功能,Ansible基本上都可以實現。
Ansible能批量配置、部署、管理上千臺主機。比如以前需要切換到每個主機上執行的一或多個操作,使用Ansible只需在固定的一臺Ansible控制節點上去完成所有主機的操作。
*Ansible架構是無客戶端架構,只需要一臺機子就可以完成,所以替代了大部分自動化運維工具
Ansible是基於模組工作的,它只是提供了一種執行框架,它本身沒有完成任務的能力,真正執行操作的是Ansible的模組
比如copy模組用於拷貝檔案到遠端主機上,service模組用於管理服務的啟動、停止、重啟等。
Ansible其中一個比較鮮明的特性是Agentless,即無Agent的存在,它就像普通命令一樣,並非C/S軟體,也只需在某個作為控制節點的主機上安裝一次Ansible即可,通常它基於ssh連線來控制遠端主機,遠端主機上不需要安裝Ansible或其它額外的服務。
(Ansible的工作原理)
使用者在使用時,在伺服器終端輸入命令或者playbooks,會通過預定好的規則將playbook拆解為play,再組織成ansible可以識別的任務,呼叫模組和外掛,根據主機清單通過SSH將臨時檔案發給遠端的客戶端執行並返回結果,執行結束後自動刪除
Ansible的另一個比較鮮明的特性是它的絕大多數模組都具備冪等性(idempotence)。所謂冪等性,指的是多次操作或多次執行對系統資源的影響是一致的。比如執行 systemctl stop xxx 命令來停止服務,當發現要停止的目標服務已經處於停止狀態,它什麼也不會做,所以多次停止的結果仍然是停止,不會改變結果,它是冪等的,而 systemctl restart xxx 是非冪等的。
Ansible的很多模組在執行時都會先判斷目標節點是否要執行任務,所以,可以放心大膽地讓Ansible去執行任務,重複執行某個任務絕大多數時候不會產生任何副作用。
ansible 環境安裝部署
主機準備
伺服器 |
IP地址 |
安裝的工具 |
管理端 |
192.168.150.30 |
ansible |
被管理端 |
192.168.150.5 |
|
被管理端 |
192.168.150.10 |
|
管理端安裝 ansible
#需要使用線上yum源
yum install -y epel-release #先安裝 epel 源
yum install -y ansible
#ansible 目錄結構
yum -y install tree
tree /etc/ansible/
├── ansible.cfg#ansible的配置檔案,一般無需修改
├── hosts#ansible的主機清單,用於儲存需要管理的遠端主機的相關資訊
└── roles/#公共角色目錄
配置主機清單
cd /etc/ansible
vim hosts
[webservers]#配置組名
192.168.150.5#組裡包含的被管理的主機IP地址或主機名(主機名需要先修改/etc/hosts檔案)
[dbservers]
192.168.150.10
配置金鑰對驗證
ssh-keygen -t rsa #一路回車,使用免密登入
ssh-copy-id [email protected]
ssh-copy-id [email protected]
或者
sshpass -p '123456' ssh-copy-id [email protected].150.5
sshpass -p '123456' ssh-copy-id [email protected].150.10
ansible 命令列模組
命令格式:ansible <組名> -m <模組> -a <引數列表>
ansible-doc -l #列出所有已安裝的模組,按q退出
ansible-doc -s <模組名> #檢視某個模組的詳細資訊及常用引數
1、command 模組
在遠端主機執行命令,不支援管道,重定向等shell的特性。
ansible-doc -s command #-s 列出指定模組的描述資訊和操作動作
ansible 192.168.150.5-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主機,如省略-m模組,則預設執行command 模組
#在被管理伺服器(192.168.150.5)建立幾個目錄
mkdir /opt/abc
touch 123.txt 456.txt
ansible webservers -m command -a "chdir=/opt/abcls ./" #提前進入目錄執行命令
ansible webservers -m command -a "creates=/opt/abcls ./" #如果檔案存在不執行
ansible webservers -m command -a "removes=/opt/abcls /opt/abc/"#如果檔案存在則執行
常用的引數 |
|
chdir |
在遠端主機上執行命令前提前進入目錄 |
creates |
判斷指定檔案是否存在,如果存在,不執行後面的操作 |
removes |
判斷指定檔案是否存在,如果存在,執行後面的操作 |
2、shell 模組
在遠端主機執行命令,相當於呼叫遠端主機的shell程序,然後在該shell下開啟一個子shell執行命令,模擬使用者在主機上執行操作(支援管道符號等功能)
ansible-doc -s shell
ansible webservers-m shell -a 'echo $(ifconfig ens33 | awk "NR==2 {print \$2}")' #遠端操作webservers組內的主機截取出ip地址
3、cron模組
在遠端主機定義任務計劃。其中有兩種狀態(state):present表示新增(可以省略),absent表示移除。
常用的引數 |
|
minute/hour/day/month/weekday |
分/時/日/月/周 |
job |
任務計劃要執行的命令 |
name |
任務計劃的名稱 |
ansible-doc -s cron #按 q 退出
ansible webservers -m cron -a 'minute="*/30" hour="3" day="1" month="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
ansible webservers -m user -a 'name="aaa"' #建立使用者aaa
ansible webservers -m command -a 'tail -3 /etc/passwd'
ansible webservers -m user -a 'name="aaa" state=absent'#刪除使用者aaa
常用的引數 |
|
name |
使用者名稱,必選引數 |
state=present|absent |
建立賬號或者刪除賬號,present表示建立,absent表示刪除 |
system=yes|no |
是否為系統賬號 |
uid |
使用者uid |
group |
使用者基本組 |
shell |
預設使用的shell |
move_home=yse|no |
如果設定的家目錄已經存在,是否將已經存在的家目錄進行移動 |
password |
使用者的密碼,建議使用加密後的字串,直接輸入數字/etc/passwd中顯示的也為數字 |
comment |
使用者的註釋資訊 |
remove=yes|no |
當state=absent時,是否刪除使用者的家目錄 |
5、group 模組
使用者組管理的模組
ansible-doc -s group
ansible webservers -m group -a 'name=nginxgid=88system=yes' #建立nginx組,並且為系統賬戶
ansible webservers -a 'tail -3 /etc/group'
ansible webservers -m user -a 'name=aaauid=88system=yes group=nginx'#將aaa使用者新增到nginx組中
ansible webservers -a 'tail -3 /etc/passwd'
ansible webservers -a 'id aaa'
6、copy 模組
用於複製指定主機檔案到遠端主機的
ansible-doc -s copy
ansible webservers -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=aaamode=640'
ansible webservers -a 'ls -l /opt'
ansible webservers -m copy -a 'content="hello" dest=/opt/hi.txt' #將hello寫入/opt/hi.txt檔案中
ansible webservers -a 'cat /opt/hi.txt'
常用的引數 |
|
dest |
指出複製檔案的目標及位置,使用絕對路徑,如果是源目錄,指目標也要是目錄,如果目標檔案已經存在會覆蓋原有的內容 |
src |
指出原始檔的路徑,可以使用相對路徑或絕對路徑,支援直接指定目錄,如果源是目錄則目標也要是目錄 |
mode |
指出複製時,目標檔案的許可權 |
owner |
指出複製時,目標檔案的屬主 |
group |
指出複製時,目標檔案的屬組 |
content |
指出複製到目標主機上的內容,不能與src一起使用 |
7、file 模組
設定檔案屬性
ansible-doc -s file
ansiblewebservers-m file -a 'owner=aaagroup=nginxmode=644 path=/opt/fstab.bak' #修改檔案的屬主屬組許可權等
ansible webservers -a 'ls -l /opt'
ansiblewebservers-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/xxx.txt state=touch" #建立一個檔案
ansible dbservers -m file -a "path=/opt/xxx.txt state=absent" #刪除一個檔案
8、hostname 模組
用於管理遠端主機上的主機名
ansible webservers -m hostname -a "name=nginx01"
9、ping 模組
檢測遠端主機的連通性
ansible all -m ping
10、yum 模組
在遠端主機上安裝與解除安裝軟體包
ansible-doc -s yum
ansible webservers -m yum -a 'name=httpd' #安裝服務
ansible webservers -m yum -a 'name=httpd state=absent'#解除安裝服務
11、service/systemd 模組
用於管理遠端主機上的管理服務的執行狀態
ansible-doc -s service
ansible webservers -a 'systemctl status httpd' #檢視web伺服器httpd執行狀態
ansible webservers -m service -a 'enabled=true name=httpd state=started' #啟動httpd服務
ansible webservers -a 'systemctl is-enabled httpd' #檢視是否為開機自啟
常用的引數 |
|
name |
被管理的服務名稱 |
state=started|stopped|restarted |
動作包含啟動關閉或者重啟 |
enabled=yes|no |
表示是否設定該服務開機自啟(true/fales) |
runlevel |
如果設定了enabled開機自啟去,則要定義在哪些執行目標下自啟動 |
12、script 模組
實現遠端批量執行本地的 shell 指令碼
ansible-doc -s script
#在ansible主機編寫一個指令碼並給予許可權
vim 111.sh
#!/bin/bash
echo "hello ansible admin" > /opt/script.txt
chmod +x 111.sh
ansible webservers -m script -a '111.sh'
ansible webservers -a 'cat /opt/script.txt'
13、setup 模組
facts 元件是用來收集被管理節點資訊的,使用 setup 模組可以獲取這些資訊
ansible-doc -s setu
ansible webservers -m setup #獲取webservers組主機的facts資訊
ansible webservers-m setup -a 'filter=*ipv4' #使用filter可以篩選指定的facts資訊
inventory 主機清單
Inventory支援對主機進行分組,每個組內可以定義多個主機,每個主機都可以定義在任何一個或多個主機組內。
如果是名稱類似的主機,可以使用列表的方式標識各個主機,如果使用主機名需要新增埠對映。
vim /etc/ansible/hosts
[webservers]
192.168.150.10:2222 #冒號後定義遠端連線埠,預設是 ssh 的 22 埠
192.168.150.1[2:5]#可以使用[X:X]的格式定義一個範圍內的主機,可以是數字或字母
[dbservers]
db-[a:f].example.org#支援匹配 a~f
inventory中的變數
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)主機變數
vim /etc/ansible
--- 71 ---行取消註釋,不進行主機金鑰檢查,所有的主機的密碼都必須寫好在配置行中
host_key_checking = False
或者
echo 192.168.150.20 >> /etc/hosts #在服務端新增主機的對映
echo 192.168.150.5 >> /etc/hosts #在客戶端新增主機服務端的ip對映
[webservers]
192.168.150.15 ansible_port=22 ansible_user=root ansible_password=951753 #新增一臺新的主機
(2)組變數
[webservers:vars] #表示為 webservers 組內所有主機定義變數
ansible_user=root
ansible_password=123456
[all:vars] #表示為所有組內的所有主機定義變數
ansible_port=22
(3)組巢狀
[webservers]
192.168.150.5
[dbservers]
192.168.150.10
[aaa:children] #表示為aaa主機組中包含了webservers組和 dbservers組內的所有主機
webservers
dbservers