Inventory详解
Inventory詳解
Ansible 可以根據Inventory文件實現同時配置多個系統。這個文件位于/etc/ansible/hosts。實際使用的時候可以編寫多個Inventory文件,使用的時候用-i參數臨時指定就行。
這個文件不僅可以自己編寫、編寫多個。配置的時候不光可以使用本地的資源,也可以使用云端的資源。
這個文件可以編寫成多種格式,常用的有:INI,YAML。
Inventory基礎:多臺主機分組
表示同一個意思的inventory文件可以用INI寫也可以用YAML寫,不過需要安裝對應的插件。
INI格式的是這樣:
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
方括號里面的是組名,未達到見名知義的效果,最好讓這個名字體現出:這是個什么操作系統,它被用來干什么事,一般什么時間運行。
這個文件用YAML格式寫是這樣:
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
將多臺主機配置到多個組
可以給多個組同時裝系統,于是可以做到這種效果:一臺主機既是一個web服務器,又是一個數據庫服務器。
創建組的時候可以用這樣一種思路:
用來干啥
應用,棧,微服務。比如: database servers , web servers。
在哪里
在哪個數據中心或哪個區域的本地的DNS服務器,storage存儲服務器。比如:east,west。
什么階段
開發階段,需要避免測試線上代碼。比如:prod,test。
包含以上的三種思想的YAML格式的Inventory文件可以寫成這樣:
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
east:
hosts:
foo.example.com:
one.example.com:
two.example.com:
west:
hosts:
bar.example.com:
three.example.com:
prod:
hosts:
foo.example.com:
one.example.com:
two.example.com:
test:
hosts:
bar.example.com:
three.example.com:
可以看到one.example.com在dbservers組中,也在east組中,也在prod組中。
也可以使用嵌套的方式簡化這個文件:
all:
hosts:
mail.example.com:
children:
webservers:
hosts:
foo.example.com:
bar.example.com:
dbservers:
hosts:
one.example.com:
two.example.com:
three.example.com:
east:
hosts:
foo.example.com:
one.example.com:
two.example.com:
west:
hosts:
bar.example.com:
three.example.com:
# 以上相同
prod:
children:
east:
test:
children:
west:
如果用了這種簡寫的方法,要注意變量代表它包含的所有主機。
多臺主機使用不同端口
如果有的主機沒有用默認ssh端口,那么在主機名后加上端口就行。
如果要配置的主機的端口號確實不是默認端口22,那最好還是配一下端口。
就像這樣:
badwolf.example.com:5309
如果主機的IP是靜態的,不會變更,而且連接的時候需要通過隧道連接,那可以在hosts文件中配置一下別名。
INI文件這樣配:
jumper ansible_port=5555 ansible_host=192.0.2.50
別名 端口號 主機名
YAML文件這樣配:
...
hosts:
jumper:
ansible_port: 5555
ansible_host: 192.0.2.50
如果寫入的主機名稱有統一的模式,可以這樣簡寫:
INI格式:
[webservers]
www[01:50].example.com
YAML格式:
...
webservers:
hosts:
www[01:50].example.com:
對于數字的模式匹配,前導0可以根據需要加上或者去掉,前后的范圍是閉區間的,兩個邊界值都可以取到。
也可以定義字母范圍:
[databases]
db-[a:f].example.com
也可以選擇連接類型和每個主機的用戶:
[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_user=mdehaan
主機 連接類型 連接用戶
如上所述,在inventory文件中設置這些只是一種簡化方法,稍后我們將討論如何將它們存儲在host_vars目錄中的各個文件中。
用變量指代一臺主機:主機變量
主機也可以用變量指代,而且現在指定的變量在后面的playbook中會用到。
INI格式:
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
YAML格式:
atlanta:
host1:
http_port: 80
maxRequestsPerChild: 808
host2:
http_port: 303
maxRequestsPerChild: 909
用變量指代多臺主機:組變量
變量也可以一次應用到整個組。
INI格式:
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
YAML格式:
atlanta:
hosts:
host1:
host2:
vars:
ntp_server: ntp.atlanta.example.com
proxy: proxy.atlanta.example.com
組變量繼承:子組變量
也可以配置子組,以及使用子組變量
| INI格式 | YAML格式 | |
|---|---|---|
| 子組 | :children |
children: |
| 子組變量 | :vars |
vars: |
INI格式:
[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2
[usa:children]
southeast
northeast
southwest
northwest
YAML格式:
all:
children:
usa:
children:
southeast:
children:
atlanta:
hosts:
host1:
host2:
raleigh:
hosts:
host2:
host3:
vars:
some_server: foo.southeast.example.com
halon_system_timeout: 30
self_destruct_countdown: 60
escape_pods: 2
northeast:
northwest:
southwest:
幾個默認組
有兩個默認組:all和ungrouped。
all包含所有主機
ungrouped包含只屬于all組的主機
每個主機至少屬于兩個組
盡管all和ungrouped始終存在,但它們可以是隱式的,不會出現在group_names之類的組清單中。
總結
以上是生活随笔為你收集整理的Inventory详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华硕笔记本 x550c 升级 bios
- 下一篇: pua是什么意思(铲除PUA毒瘤需持续精