您現在的位置是:首頁 > 音樂首頁音樂
Ansible系列(5):Inventory介紹,如何管理主機組
主機檔案管理在哪兒
這是Ansible系列課程第五節,Ansible Inventory介紹。介紹一下Inventory是什麼,以及如何管理主機組。
該系列課程前後章節都是有關聯性的,對於初學者建議按順序閱讀。也可以選擇特定的章節瞭解單個知識點。
Ansible可以遠端操作一臺或一組主機,這些主機的清單是在稱為inventory的檔案中配置的。預設的inventory檔案是/etc/ansible/hosts,也可以在命令列中透過引數-i來指定其他路徑下的inventory檔案,或者從其他系統中動態獲取相同格式的清單,比如CMDB。
01
Inventory檔案示例
inventory檔案有INI和YAML兩種格式,建議INI格式。一個基本的inventory檔案內容如下:
mail。example。com #單個主機地址
[webservers]
#主機組, []內為組名
foo。example。com http_port
=
80 maxRequests
=
30 #定義主機變數
bar。example。com ansible_connection
=
ssh ansible_user
=
myuser #指定連線資訊
[dbservers]
db-[1
:
5]。example。com #定義1-5範圍的主機
bar-[a
:
f]。example。com #定義a-f範圍的主機
[all:vars]
#定義全域性變數
ansible_ssh_private_key_file
=
/root/。ssh/id_rsa
ansible_ssh_port
=
22
ansible_ssh_user
=
root
[dbservers:vars]
#定義dbservers組的變數
mysql_port
=
3806
[server:children]
#定義server組的子成員
webservers
dbservers
上面對檔案配置做個介紹:
主機可以單獨配置、可以屬於一個或多個主機組
主機組定義的格式是:[groupname],如:[webservers],[dbservers]
主機組變數定義的格式是:[groupname:vars],如:[webservers:vars],[all:vars]
主機組巢狀定義的格式是:[parentgroup:children],如:[server:children]
自定義主機變數定義格式:如:foo。example。com http_port=80 maxRequests=30
內建主機變數定義格式:如:bar。example。com ansible_connection=ssh ansible_user=myuser
02
預設組
在inventory中,有2個預設組:all和ungrouped。
all
:包含所有的主機
ungrouped
:包含不屬於組的主機
儘管all和ungrouped永遠存在,但他們是隱式的,不會出現在任何組列表中。
03
多組共存
為了滿足不同的場景,會定義不同的組名,比如:
What:是什麼,應用程式、資料庫還是快取。
Where:在哪裡,哪個資料中心或者區域,如Beijing,ShangHai
When:開發階段,開發環境、測試環境、生產環境
每個主機也可能會同時屬於多個組,如下面inventory所示,foo。example。com同時屬於webservers和east,one。example。com同時屬於dbservers和east等。
mail。example。com
[webservers]
foo。example。com
bar。example。com
[dbservers]
one。example。com
two。example。com
three。example。com
[east]
foo。example。com
one。example。com
[west]
bar。example。com
three。example。com
[prod:children]
east
[test:children]
west
04
Inventory中的變數
我們可以在Inventory檔案中指定特定主機或特定組相關的變數,這些在前面的示例中也有說明。但隨著inventory中管理的節點越來越多,也可以透過變數檔案的方式有效管理。
主機變數
新增主機變數很簡單,直接在主機後面新增key=value鍵值對,如下所示:
[webservers]
foo。example。com http_port
=
80 maxRequests
=
30
bar。example。com ansible_connection
=
ssh ansible_user
=
myuser
另外,也可以直接給主機起個別名,在後續playbook中直接使用別名即可,使用別名的主機,需要指定
ansible_host
引數。
jumper ansible_port=5555 ansible_host=192。0。2。50
組變數
如果組裡的所有主機都需要相同的變數值,可以將變數新增到組上,格式如下:
[dbservers]
db-[1
:
5]。example。com
bar-[a
:
f]。example。com
[dbservers:vars]
mysql_port
=
3806
組變數是一種可以將變數同時應用到多個主機的方法。但是,在執行之前,Ansible需要將變數展平到host級別。如果一個主機同時屬於多個組,如果為不同組中的同一個變數指定了不同的值,該按什麼規則選擇呢?在後面變數優先順序部分會介紹。
巢狀組變數
inventory檔案中對組的管理非常靈活,可以將多個組在封裝成一個新組,可以對這個新組指定變數。
[webservers]
foo。example。com
bar。example。com
[dbservers]
db-[1
:
5]。example。com
bar-[a
:
f]。example。com
[server:children]
#利用已有的組封裝成新的組
webservers
dbservers
[server:vars]
#巢狀組變數
connect_timeout
=
30
變數檔案
雖然在Inventory檔案中可以指定變數,但如果變數多了管理成本會很高,也會很混亂。Ansible中可以透過變數檔案來管理組變數或主機變數,檔案內容也是YAML語法。
Ansible透過搜尋相對於inventory和playbook檔案的路徑來載入host和group變數檔案。假如:在
/etc/ansible/hosts
的inventory檔案中包含一個名為
foo。example。com
的主機,分別屬於
webservers
和
dbservers
兩個組,那麼該主機將從以下位置獲取變數:
/etc/ansible/group_vars/dbservers
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foo。example。com
檔案的格式如下:
——-
ntp_server
:
acme。example。org
database_server
:
storage。example。org
有可以在playbook目錄下新增
group_vars/
和
host_vars/
目錄,
ansible-playbook命令預設會從當前工作目錄
下查詢這些目錄。但其他的ansible命令
只會從inventory目錄下查詢group_vars和host_vars,如ansible,ansible-console
。如果要想讓這些命令也能從playbook目錄下查詢,需要在命令列提供
——playbook-dir
引數。如果想從playbook和inventory這兩個地方載入變數,那麼playbook目錄下的變數會覆蓋inventory目錄下的變數。
變數的優先順序
預設情況下,在任務執行之前,所有的變數都會進行合併和鋪平然後應用到特定的host上,這就涉及到如果從多個數據源獲取變數,那麼變數的優先順序是什麼樣的。
由低到高的順序:
預設情況下,ansible在同一級別按照字母的順序進行合併,後面載入的組覆蓋之前載入的組。比如,a_group和b_group進行合併,那麼b_group組的變數將覆蓋a_group中的變數。
05
多源Inventory
在Ansible的最佳實踐中,建議按不同的環境劃分inventory檔案,如果有個任務想同時在不同的環境中執行,這個時候就可以指定多個inventory檔案,格式如下:
$ansible-playbook get_logs。yml -i staging -i production
當這兩個inventory檔案中存在相同的變數時,也是按
上面的優先順序
進行取捨。如:staging中變數a=1,production中a=2,那麼按上面的命令,合併後的結果是a=2,如果想讓a=1,需要改變傳入的順序。
$ansible-playbook get_logs。yml -i production -i staging
06
主機內建變數清單
下面是ansible內建的變數清單,可以收藏備用。
主機連線配置
ansible_connection
:與主機的連線型別。可以是ansible的連線外掛的名稱。SSH協議型別為smart、ssh或paramiko。預設值是smart。
所有連線配置
ansible_host
:要連線到遠端主機的名稱。
ansible_port
:要連線到遠端主機的埠,預設為22。
ansible_user
:連線到遠端主機的使用者名稱。
ansible_password
:連線到遠端主機的使用者名稱密碼。
SSH連線配置
ansible_ssh_private_key_file
:ssh使用的私有檔案,適用於有多個秘鑰,但不想使用ssh agent情況。
ansible_ssh_common_args
:該設定附加到預設的sftp、scp和ssh命令列上。
ansible_sftp_extra_args
:該設定總是附加到預設的sftp命令列上。
ansible_scp_extra_args
:該設定總是附加到預設的scp命令列上。
ansible_ssh_extra_args
:該設定總是附加到預設的ssh命令列上。
ansible_ssh_pipelining
:確定是否使用SSH pipelining,該引數會覆蓋ansible。cfg中的pipelining設定。
ansible_ssh_executable
:此設定會覆蓋使用系統ssh的預設行為,會覆蓋ansible。cfg中的ssh_executable引數。
特權提升配置
ansible_become
:允許特權升級,等同於
ansible_sudo
、
ansible_su
。
ansible_become_method
:設定特權提升的方法,比如sudo。
ansible_become_user
:設定特權提升的使用者,等同於
ansible_sudo_user
,
`ansible_su_user
ansible_become_password
:設定特權使用者的密碼,等同於
ansible_sudo_password
,
ansible_su_password
ansible_become_exe
:設定提權方法所用的可執行檔案,等同於
ansible_sudo_exe
,
ansible_su_exe
ansible_become_flags
:設定提權方法所用的引數,等同於
ansible_sudo_flags,ansible_su_flags
遠端主機環境配置
ansible_shell_type
:目標系統的shell型別。預設情況下,命令的執行使用 ‘sh’ 語法,可設定為 ‘csh’ 或 ‘fish’。
ansible_python_interpreter
:目標主機的 python 路徑。適用於的情況: 系統中有多個 Python, 或者命令路徑不是“/usr/bin/python”,比如 *BSD, 或者 /usr/bin/python。
ansible_*_interpreter
:這裡的“*”可以是 ruby 或 perl 或其他語言的直譯器,作用和ansible_python_interpreter 類似。
ansible_shell_executable
:這將設定ansible控制器將在目標機器上使用的shell,覆蓋ansible。cfg中的配置,預設為/bin/sh。
07
總結
這篇文章主要介紹了inventory檔案以及inventory中涉及的group變數和host變數的設定。inventory檔案在Ansible中是非常重要的一個元件,如何更有效、合理的管理主機,對於ansible的任務執行有很大幫助。