Ansible Inventory - 管理主机的清单
2018-02-28 本文已影响347人
Anoyi
默认存于 /etc/ansible/hosts
文件中,也可以通过 -i <PATH>
指定到文件。
主机和组
示例:INI 格式
all.example.com
other ansible_port=5555 ansible_host=192.0.2.50
[webservers]
web1.example.com
web2.example.com
[dbservers]
db1.example.com
db2.example.com
db3.example.com
[otherservers]
other.example.com:8080
示例:YAML 格式
all:
hosts:
all.example.com
other:
ansible_port: 5555
ansible_host: 192.0.2.50
children:
webservers:
hosts:
web1.example.com:
web2.example.com:
dbservers:
hosts:
db1.example.com:
db2.example.com:
db3.example.com:
主机变量
可供 playbooks 使用,示例:
[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 格式
[atlanta]
host1
host2
[raleigh]
host2
host3
[southeast:children]
atlanta
raleigh
[southeast:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
[usa:children]
southeast
northeast
示例: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
northeast:
如果需要存储列表或散列数据,建议将主机和组特定变量与 Inventory 文件分开。
关于群组的补充说明:
- 任何属于子组的成员都自动成为父组的成员
- 子组的变量将具有更高的优先级(覆盖父组的变量)
- 组可以有多个父母和孩子,但不是循环关系
- 主机也可以在多个组中,但只有一个主机实例,合并来自多个组的数据
默认组
有两个默认组:all
和 ungrouped
:
-
all
包含每个主机。 -
ungrouped
包含所有没有另一个组的主机。
即每个主机将永远属于至少2个组。