ansible.cfg
中的 inventory
參數定義主機的列表,默認存放在 /etc/ansible/hosts
。除此配置文件外,也能夠同時使用多個 inventory
文件,或者從動態雲資源拉取 inventory
配置信息,支持不一樣的格式,如 yaml
、ini
等。python
在本機 ubuntu 18.04
操做其餘三主機:git
localhost
10.53.141.252
:ubuntu 18.04
,用戶 cec
10.53.128.20
:RHEL 5.8
,用戶 durant
,安裝多版本 Python
配置好 ssh keys
。web
INI
shell
localhost ansible_connection=local
10.53.141.252 ansible_user=cec
10.53.128.20 ansible_user=durant ansible_python_interpreter="/usr/bin/env python3"
複製代碼
YAML
:all
也可以使用其餘名字json
all:
hosts:
localhost:
ansible_connection: local
10.53.141.252:
ansible_user: cec
10.53.128.20:
ansible_user: durant
ansible_python_interpreter: "/usr/bin/env python3"
複製代碼
INIubuntu
localhost ansible_connection=local
[dev]
10.53.141.252 ansible_user=cec
10.53.128.20 ansible_user=durant ansible_python_interpreter="/usr/bin/env python3"
複製代碼
YAML
bash
all:
hosts:
localhost:
ansible_connection: local
children:
dev:
hosts:
10.53.141.252:
ansible_user: cec
10.53.128.20:
ansible_user: durant
ansible_python_interpreter: "/usr/bin/env python3"
複製代碼
在上面的例子中,包含三個組:all
、ungrouped
和 dev
。ssh
all
:全部的主機ungrouped
:不在除 all
以外的其餘的組的主機所任何一個主機都至少在兩個組中。即便 all
、ungrouped
一直有,但不會出如今組列表(如group_names
)中。ui
給主機分配變量,在 playbook
中會用到spa
[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
複製代碼
請注意,這只是個同時給多個主機賦予變量的簡便方法,即便你能夠根據組指定目標主機。variables are always flattened to the host level before a play is executed.
在 INI
文件使用 :children
或在 YAML
文件中使用 children:
入口,能夠一個組設置爲另外一個組的成員,使用 :vars
或 vars:
設置變量。
[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
複製代碼
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:
複製代碼
子組有下列注意點:
YAML
文件中,組的下一層只能是vars
、children
和hosts
。
若是要存儲序列或 hash 值,或把 host、group 的變量與 inventory
文件分開,也有辦法作到。
Ansible
實踐中也不推薦在 inventory
文件中存儲變量。
將主機、組的變量存放在 inventory
相關的一個獨立文件中(不是目錄,一般是文件),這些變量文件是 YAML
格式,擴展名通常是 yml
、yaml
、json
或者沒有擴展名。
若是 inventory
文件路徑是 /etc/ansible/hosts
,主機名是 football
,在組 raleigh
和 webservers
中。下面的變量文件都是有效:
/etc/ansible/group_vars/raleigh # can optionally end in '.yml', '.yaml', or '.json'
/etc/ansible/group_vars/webservers
/etc/ansible/host_vars/foosball
複製代碼
變量文件內容:
---
ntp_server: acme.example.org
database_server: storage.example.org
複製代碼
固然,這些文件不存在也不要緊,由於是可選特性。
在高級用法中,也能夠在組或主機下繼續建立目錄,Ansible
使字典編纂順序讀取這文件,例如 raleigh
組:
/etc/ansible/group_vars/raleigh/db_settings
/etc/ansible/group_vars/raleigh/cluster_settings
複製代碼
raleigh
組的全部主機都會有這些文件定義的變量,當一個單獨文件太大的時候,用這個方法可更好的組織文件結構,或者當使用 Ansible Vault
做爲組變量的一部分時。
group_vars/
和host_vars/
能夠存放在playbook
或者inventory
目錄。若是兩個路徑都有,playbook
目錄下的變量會覆蓋inventory
目錄下的變量。
將
inventory
文件和變量加入git
倉庫(或其餘版本控制系統)是一個追蹤inventory
和主機變量變化的好方法。
在 play
運行前,會對特定的主機進行變量合併,這可使 Ansible
專一於主機和任務,所以組實際只存在於 inventory
和主機匹配中。
下面是變量的優先順序(低 -> 高):
all
,由於 all
是全部組的父組根據字母順序合併相同級別的父組、子組,後面的覆前面的。
從 Ansible 2.4
開始,可使用 ansible_group_priority
改變相同級別組的合併順序( parent/child 的順序被解析後)。數字越大,越後合併,即優先度更高,默認是 1。
例如,若是這兩個組優先級相同,結果是 testvar == b
,但因爲給 a_group
更高的權限,結果是 testvar == a
a_group:
testvar: a
ansible_group_priority: 10
b_group:
testvar: b
複製代碼