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
复制代码