實驗環境:
主機:Ansible 172.25.4.100
主機:node1 172.25.4.10
主機:node2 172.25.4.20
主機:node4 172.24.4.30
1.Ansible對于企業運維的重大意義
通過Ansible可以實作運維自動化,提高運維工程師的工作效率,減少人為失誤。
2.Ansible的安裝
epel源
dnf install ansible -y
ansible --viersion
可以根據阿裡雲上的epel源提示來下載下傳阿裡雲的ansible

下載下傳ansible服務
ansible的基本資訊 | |
---|---|
/etc/ansible/ansible.conf | 全局配置檔案,預設很少修改 |
/etc/ansible/hosts | 全局主機清單清單檔案 |
3.建構Anisble清單
清單就是ansible控制主機的清單
/etc/ansible/hosts ##全局清單檔案
1.直接書寫受管主機名或ip,每行一個
建構清單檔案,将其他虛拟機加入控制清單
2.設定受管主機的組[組名稱]
先在全局配置檔案中釋放指令
再生成鑰匙
将鑰匙發送給清單中的虛拟機
真機上的網橋添加一個位址
真機網橋配置檔案
#清單檢視:
ansible 清單中組名稱 [-i 清單檔案] --list-hosts
ansible ungrouped --list-hosts
ansible all --list-hosts
#單層清單#
[list1]
172.25.4.10 ( node1.westos.com )
[list2]
172.25.4.10
172/25.4.20
[list3]
172.25.4.30
#嵌套清單#
[westos:children]
list1
list3
#測試:
[[email protected] ansible]# ansible list1 --list-hosts
hosts (1):
172.25.4.10
[[email protected] ansible]# ansible list2 --list-hosts
hosts (1):
172.25.4.10
172.25.4.20
[[email protected] ansible]# ansible list3 --list-hosts
hosts (1):
172.25.4.30
[[email protected] ansible]# ansible westos --list-hosts
hosts (3):
172.25.4.10
172.25.4.20
172.25.4.30
3.主機規格的範圍化操作
#通過指定主機名稱或IP的範圍可以簡化Ansible主機清單
#文法:
#[start:end]
[inventory]
172.25.4.[10:20]
#測試:
4.指定其他清單檔案
這裡
測試時将全局配置檔案cp到家目錄下的建立的目錄下
再cp其他清單檔案到該目錄中
給配置檔案隻在該目錄下生效
#測試
ansible all -i inventory --list-hosts
ansible westostest -i inventory --list-hosts
ansible ungrouped -i inventory --list-hosts
#ansible指令指定清單的正規表達式
* ##所有
##172.25.4.*
##westos*
: ##邏輯或
##westos1:linux
##172.25.4.100:172.25.4.200
:& ##邏輯與
##westos1:&linux
##主機即在westos1清單也在linux清單中
:! ##邏輯非
##westos1:!linux
##在westos1中不在linux中
##EXAMPLE
vim /mnt/inventory
172.25.4.250
192.25.4.250
[westos_list1]
node1.westos.com
[westos_list2]
172.25.4.2
172.25.4.1
[westos_list3]
172.25.4.1
172.25.4.3
[westos_all:children]
westos_list2
westos_list3
[]:
ansible 'westos’ -i inventory --list-host
ansible ‘172.*’ -i inventory --list-host
[:]:
ansible ‘westos_list1:westos_list2’ -i inventory --list-host
[:&]
ansible ‘westos_list2:&westos_list3’ -i inventory --list-host
[:!]
ansible ‘westos_list2:!westos_list3’ -i inventory --list-host
[~]
ansible ‘~westos’ -i inventory --list-host
4.Ansible配置檔案參數詳解
ansible 清單中組名稱 -m 子產品 -u remote_user
1.配置檔案的分類與優先級
/etc/ansible/ansible.cfg | 基本配置檔案,找不到其他配置檔案此檔案生效 |
---|---|
~/.ansible.cfg | 使用者目前目錄中沒有ansible.cfg此檔案生效 |
./ansible.cfg | 優先級最高 |
2.常用配置參數
#[default] | 基本資訊設定 |
---|---|
inventory= | 指定清單路徑 |
remote_user= | 在受管主機上登陸的使用者名稱,未指定使用目前使用者 |
ask_pass= | 是否提示輸入SSH密碼,如果公鑰登陸設定為false |
library= | 庫檔案存放目錄 |
local_tmp= | 本機臨時指令執行目錄 |
remote_tmp= | 遠端主機臨時py指令檔案存放目錄 |
forks= | 預設并發數量 |
host_key_checking= | 第一次連接配接受管主機時是否要輸入yes建立host_key |
sudo_user= | 預設sudo使用者 |
ask_sudo_pass= | 每次在受控主機執行ansible指令時是否詢問sudo密碼 |
module_name= | 預設子產品,預設使用command,可以修改為shell |
log_path= | 日志檔案路徑 |
[privilege_escalation] | 身份資訊設定 |
---|---|
become= | 連接配接後是否自動切換使用者 |
become_method= | 設定切換使用者的方式,通常用sudo |
become_user= | 在受管主機中切換到的使用者,通常為root |
become_ask_pass | 是否需要為become_method提示輸入密碼,預設為false |
當ansible搭建好後,可以在ansible服務主機上執行指令,并在其清單檔案下的虛拟主機上執行同樣的指令