1. 程式人生 > 實用技巧 >Ansible主機清單Inventory檔案hosts

Ansible主機清單Inventory檔案hosts

Ansible主機清單Inventory檔案hosts

Ansible 通過讀取預設的主機清單配置,可以同時連線到多個遠端主機上執行任務組和主機之間的關係通過 inventory 檔案配置. 預設的檔案路徑為 /etc/ansible/hosts。預設路徑可以通過修改 ansible.cfg 的 hostfile 引數指定路徑。

除預設檔案外,你還可以同時使用多個 inventory 檔案(後面會講到),也可以從動態源,或雲上拉取 inventory 配置資訊.詳見

動態 Inventory.

1.主機與組

/etc/ansible/hosts 檔案的格式與windows的ini配置檔案類似:

mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com

  1. 方括號[]中是組名,用於對系統進行分類,便於對不同系統進行個別的管理.可以根據自己的需求將龐大的主機分成具有標識的組,如上面分了兩個組webservers和dbservers組;
  2. 一個主機可以屬於不同的組,比如一臺伺服器可以同時屬於 webserver組 和 dbserver組.這時屬於兩個組的變數都可以為這臺主機所用,
  3. 如果有主機的SSH埠不是標準的22埠,可在主機名之後加上埠號,用冒號分隔.
badwolf.example.com:5309


4.假設你有一些靜態IP地址,希望設定一些別名,但不是在系統的 host 檔案中設定,那麼可以設定如下:

jumper ansible_ssh_port=5555 ansible_ssh_host=192.168.1.50


  1. 可以按照範圍指定主機,一組相似的 hostname , 可簡寫如下:
[webservers]
www[01:50].example.com
[databases]
db-[a:f].example.com

2.主機變數

前面已經提到過,分配變數給主機很容易做到,這些變數定義後可在 playbooks 中使用:

對於每一個 host,你還可以選擇連線型別和連線使用者名稱:

[targets]
localhost ansible_connection=local
other1.example.com ansible_connection=ssh ansible_ssh_user=mpdehaan
other2.example.com ansible_connection=ssh ansible_ssh_user=mdehaan

定義其他變數

[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909

3.組的變數

也可以定義屬於整個組的變數,應用到組內的所有成員:

[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

上面atlanta組中包含兩臺主機,通過對atlanta組指定vars變更,相應的host1和host2相當於相應的指定了ntp_server和proxy變數引數值 。

4.把一個組作為另一個組的子成員

可以把一個組作為另一個組的子成員,以及分配變數給整個組使用. 這些變數可以給 /usr/bin/ansible-playbook 使用,但不能給 /usr/bin/ansible 使用:

[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

5、分檔案定義 Host 和 Group 變數

在 inventory 主檔案中儲存所有的變數並不是最佳的方式.還可以儲存在獨立的檔案中,這些獨立檔案與 inventory 檔案保持關聯. 不同於 inventory 檔案(INI 格式),這些獨立檔案的格式為 YAML.詳見 YAML 語法 .

假設 inventory 檔案的路徑為:

/etc/ansible/hosts


假設有一個主機名為 ‘foosball’, 主機同時屬於兩個組,一個是 ‘raleigh’, 另一個是 ‘webservers’. 那麼以下配置檔案(YAML 格式)中的變數可以為 ‘foosball’ 主機所用.依次為 ‘raleigh’ 的組變數,’webservers’ 的組變數,’foosball’ 的主機變數:

/etc/ansible/group_vars/raleigh
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball

6.Inventory 引數的說明

如同前面提到的,通過設定下面的引數,可以控制 ansible 與遠端主機的互動方式,其中一些我們已經講到過:

ansible_ssh_host
將要連線的遠端主機名.與你想要設定的主機的別名不同的話,可通過此變數設定.
ansible_ssh_port
ssh埠號.如果不是預設的埠號,通過此變數設定.
ansible_ssh_user
預設的 ssh 使用者名稱
ansible_ssh_pass
ssh 密碼(這種方式並不安全,我們強烈建議使用 --ask-pass 或 SSH 金鑰)
ansible_sudo_pass
sudo 密碼(這種方式並不安全,我們強烈建議使用 --ask-sudo-pass)
ansible_sudo_exe (new in version 1.8)
sudo 命令路徑(適用於1.8及以上版本)
ansible_connection
與主機的連線型別.比如:local, ssh 或者 paramiko. Ansible 1.2 以前預設使用 paramiko.1.2 以後預設使用 'smart','smart' 方式會根據是否支援 ControlPersist, 來判斷'ssh' 方式是否可行.
ansible_ssh_private_key_file
ssh 使用的私鑰檔案.適用於有多個金鑰,而你不想使用 SSH 代理的情況.
ansible_shell_type
目標系統的shell型別.預設情況下,命令的執行使用 'sh' 語法,可設定為 'csh''fish'.
ansible_python_interpreter
目標主機的 python 路徑.適用於的情況: 系統中有多個 Python, 或者命令路徑不是"/usr/bin/python",比如 \*BSD, 或者 /usr/bin/python
不是 2.X 版本的 Python.我們不使用 "/usr/bin/env" 機制,因為這要求遠端使用者的路徑設定正確,且要求 "python" 可執行程式名不可為 python以外的名字(實際有可能名為python26).
與 ansible_python_interpreter 的工作方式相同,可設定如 ruby 或 perl 的路徑....

7.一個主機檔案的例子:

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
# - You can enter hostnames or ip addresses
# - A hostname/ip can be a member of multiple groups
 
# Ex 1: Ungrouped hosts, specify before any group headers.
 
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10
 
# Ex 2: A collection of hosts belonging to the 'webservers' group
 
## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110
 
# If you have multiple hosts following a pattern you can specify
# them like this:
 
## www[001:006].example.com
 
# Ex 3: A collection of database servers in the 'dbservers' group
 
## [dbservers]
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net
## 10.25.1.56
## 10.25.1.57
 
# Here's another example of host ranges, this time there are no
# leading 0s:
 
## db-[99:101]-node.example.com

好記性不如爛筆頭,生命不息,學習不止!