参考文章:Ansible02--hosts主机清单书写方式_徐金宝的博客-CSDN博客
自动化运维ansible——ansible清单配置(INI语法&YAML语法)_鲸鱼妹子的博客-CSDN博客_ansible清单
官方文档:How to build your inventory — Ansible Documentation
几个默认组
有两个默认组:all和ungrouped。
all包含所有主机
ungrouped包含只属于all组的主机
每个主机至少属于两个组
尽管all和ungrouped始终存在,但它们可以是隐式的,不会出现在group_names之类的组清单中。
写法比较
示例1:
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:
示例二:
[test1]
192.168.10.11
192.168.10.12
[test2]
192.168.10.13
对应:
all:
children:
test1:
hosts:
192.168.10.11:
test2:
hosts:
192.168.10.12:
192.168.10.13:
示例三:
INI写法:
# 简单写法
172.25.254.2 ansible_port=22 ansible_user=root ansible_ssh_pass=redhat
# 别名写法:server2为该主机别名:
server2 ansible_host=172.25.254.2 ansible_port=22 ansible_user=root ansible_ssh_pass=redhat
[test1]
bieming1 ansible_host=192.168.10.11
[test2]
bieming2 ansible_host=192.168.10.12
bieming3 ansible_host=192.168.10.13
[A:children]
test1
test2
对应yml写法:
all:
children:
test1:
hosts:
bieming1:
# 需要注意的是,ansible_host:和IP之间要有空格,且ip后不要有冒号
ansible_host: 192.168.10.11
test2:
hosts:
bieming2:
ansible_host: 192.168.10.12
bieming3:
ansible_host: 192.168.10.13
示例四:
Adding ranges of hosts
In INI:
[webservers]
www[01:50].example.com
In YAML:
...
webservers:
hosts:
www[01:50].example.com:
另一种写法:
In INI:
[webservers]
www[01:50:2].example.com
#other
[databases]
db-[a:f].example.com
In YAML:
...
webservers:
hosts:
www[01:50:2].example.com:
示例五:
配置主机变量:
In INI:
[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
In YAML:
atlanta:
hosts:
host1:
http_port: 80
maxRequestsPerChild: 808
host2:
http_port: 303
maxRequestsPerChild: 909
示例六:
组变量: 将变量分给多个组:
In INI:
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
In YAML:
atlanta:
hosts:
host1:
host2:
vars:
ntp_server: ntp.atlanta.example.com
proxy: proxy.atlanta.example.com
示例七:
继承变量值,组的组变量:
You can make groups of groups using the
:children
suffix in INI or thechildren:
entry in YAML. You can apply variables to these groups of groups using:vars
orvars:
:
In 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
In 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:
示例八:
组写法示例:
INI:
[proA]
172.25.254.3
[proB]
172.25.254.4
[pro:children]
proA
proB
YAML:
all:
children:
pro:
children:
proA:
hosts:
172.25.254.3:
proB:
hosts:
172.25.254.4:
示例九:
使用别名的方式配置受管主机:
INI:
172.25.254.2
serverc ansible_host=172.25.254.3
172.25.254.4
YAML:
all:
hosts:
172.25.254.2:
serverc:
# 注意:ansible_host:后面要有空格,host后面不能有空格
ansible_host: 172.25.254.3
172.25.254.4: