hosts 是 Ansible 执行的远程主机列表,默认文件为
/etc/ansible/hosts
ini 格式
# 格式
<别名> [<参数> ...]
# 示例 1
172.17.0.3
172.17.0.4 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
h-3 ansible_host=172.17.0.5 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
# 分组示例,分别为:未分组、组A、组B
172.17.0.3
[A]
172.17.0.[3:5]
# 172.17.0.4 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
[B]
h-3 ansible_host=172.17.0.5 ansible_port=22 ansible_user=root ansible_ssh_pass=123456
# C 包含 A、B 共两组的所有主机
[C:children]
A
B参数说明:
ansible_host客户主机ipansible_port客户端sshd服务的端口ansible_user客户端用户ansible_ssh_pass客户端用户密码- 高级配置:
172.17.0.[3:5]表示3~5共三台主机host[1:100]表示host1 ~ 100,host1 为/etc/hosts配置
- 使用时
ansible A -m ping对 A 组的所有主机执行 ping 测试
yaml 格式
- Yaml 介绍
- ansible 主机清单文件
/etc/ansible/hosts支持 yaml 格式
# ini 格式
172.17.0.3
172.17.0.4
# yaml 格式
all:
hosts:
172.17.0.3:
172.17.0.4:
# ini 格式
172.17.0.3
[test1]
172.17.0.4
[test2]
172.17.0.5
# yaml 格式
all:
hosts:
172.17.0.3:
children:
test1:
hosts:
172.17.0.4:
test2:
hosts:
172.17.0.5:
# ini 格式
[A]
172.17.0.3
[B]
172.17.0.4
[C:children]
A
B
# yaml 格式
all:
children:
C:
children:
A:
hosts:
172.17.0.3:
B:
hosts:
172.17.0.4:
# ini 格式
172.17.0.3
h-2 ansible_host=172.17.0.4
172.17.0.5
# yaml 格式
all:
hosts:
172.17.0.3:
h-2:
ansible_host: 172.17.0.4
172.17.0.5: