自動化運維
自動化工具--Ansible
- 自動化運維
- 一、Ansible
- 二、ansible環境安裝部署
-
-
- 管理端安裝ansible
- ansible 目錄結構
- 配置主機清單
-
- 三、ansible 指令行子產品
-
- 1. command 子產品
- 2. shell 子產品
- 3. cron子產品.
- 4. user子產品
- 5. group子產品I
- 6. copy子產品
- 7. file 子產品
- 8. hostname子產品
- 9. ping子產品
- 10. yum子產品
- 12. script子產品
- 13. setup 子產品
- 四、inventory主機清單
-
- inventory中的變量
- (1)主機變量
- (2)組變量
- (3)組嵌套.
一、Ansible
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去執行任務,重複執行某個任務絕大多數時候不會産生任何副作用。
程式員會把編寫好的程式代碼通過SVN git(github bitlab)上傳到代碼版本管理系統中
運維人員可以通過SVN git 下載下傳指定版本的代碼,對代碼封裝打包建構成鏡像,docker 容器進行運作
jenkins通過一整套流水線自動進行持續的內建和建構,鏡像放入到私有倉庫中
50台主機部署這個容器
compose docker容器單機編排 多機編排 swarm+machine K8S kubernetes
ansible應用級多機編排工具
初始化伺服器,初始化腳本傳到各個伺服器,執行初始化腳本
server ---> 50主機同時或分批進行自動化操作
docker run 鏡像 運作容器
二、ansible環境安裝部署
管理端: 192.168.100.100 ansible
被管理端: 192.168.100.110
被管理端: 192.168.100.120
管理端安裝ansible
yum install -y epel-release #先安裝epel源
yum install -y ansible
ansible 目錄結構
/etc/ansible/
|——ible.cfg #ansible的配置檔案,一般 無需修改
|——hosts #ansible的主機清單,用于存儲需要管理的遠端主機的相關資訊
|——roles/ #公共角色目錄
配置主機清單
cd /etc/ansible
vim hosts
[webservers] #配置組名
192.168.100.110 #組裡包含的被管理的主機IP位址或主機名(主機名需要先修改/etc/hosts檔案)
[dbservers]
192.168.100.120
配置密鑰對驗證
ssh-keygen -t rsa #一路回車,使用免密登入
sshpass -p 'as123456' ssh-copy-id [email protected]
sshpass -p 'as123456' ssh-copy-id [email protected]
三、ansible 指令行子產品
1. command 子產品
在遠端主機執行指令,不支援管道,重定向等shell的特性。
ansible-doc -s command #-s列出指定子產品的描述資訊和操作動作
ansible 192.168.100.110 -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子產品,則預設運作comnand子產品
常用的參數
chdir: 在遠端主機上運作指令前提前進入日錄
creates: 判斷指定檔案是否存在,如果存在,不執行後面的操作
removes: 判斷指定檔案是否存在,如果存在,執行後面的操作
2. shell 子產品
在遠端主機執行指令,相當于調用遠端主機的shell程序,然後在該shell下打開一個子shell運作指令( 支援管道符号等功能)
ansible-doc -s shell
3. cron子產品.
在遠端主機定義任務計劃。其中有兩種狀态(state) : present表示添加(可以省略),absent表示移除。
常用的參數:
minute/hour/day/minute/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子產品
使用者管理的子產品
常用的參數
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子產品I
使用者組管理的子產品
ansible dbservers -m group -a 'name=mysql gid=306 system=yes' #建立mysq1組
ansible dbservers -a 'tail /etc/group'
ansible dbservers -m user -a 'name=test01 uid=306 system=yes group=mysql' #将test01使用者添加到mysq1l組中
ansible dbservers -a 'tail /etc/passwd'
ansible dbservers -a 'id test01'
6. 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 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子產品
用于管理遠端主機上的主機名
9. ping子產品
檢測遠端主機的連通性
10. yum子產品
在遠端主機上安裝與解除安裝軟體包
ansible webservers -m yum -a 'name=httpd' #安裝服務
ansible webservers -m yum -a 'name=httpd state=absent' #解除安裝服務
-
service/ systemd 子產品
用于管理遠端主機上的管理服務的運作狀态
常用的參數
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腳本
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 webservers -m setup #擷取mysql組主機的facts資訊
ansible dbservers -m setup -a 'filter=*ipv4' #使用filter可以篩選指定的facts資訊
四、inventory主機清單
Inventory支援對主機進行分組,每個組内可以定義多個主機,每個主機都可以定義在任何一個或多個主機組内。
如果是名稱類似的主機,可以使用清單的方式辨別各個主機。
vim /etc/ansible/hosts
[webservers]
192.168.100.110:2222 #冒号後定義遠端連接配接端口,預設是ssh的22端口
192.168.100.1[2:5]0
[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)主機變量
[webservers]
192.168.100.110 ansible_port=22 ansible_user=root ansible_password=as123456
(2)組變量
[webservers:vars] #表示為webservers 組内所有主機定義變量
ansible_user=root
ansible_password=abc1234
[all:vars] #表示為所有組内的所有主機定義變量
ansible_port=22
(3)組嵌套.
[nginx]
192.168.100.20
192.168.100.21
192.168.100.22
[apache ]
192.168.100.3[0:3]
[webservers:children] #表示為webservers 主機組中包含了nginx組合apache組内的所有主機
nginx
apache