Ansible Inventory 主机清单
Ansible Inventory 主机清单
什么是 Inventory
Inventory 是 Ansible 中定义和组织被管理主机的清单。它包含主机列表、组、变量和连接参数,是 Ansible 执行任务的基础。
静态 Inventory
INI 格式
# inventory/hosts.ini
# 直接定义主机
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
db1 ansible_host=192.168.1.20
# 定义主机组
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
[dbservers]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21
# 组变量
[webservers:vars]
ansible_user=deploy
ansible_ssh_private_key_file=~/.ssh/id_rsa
http_port=80
[dbservers:vars]
ansible_user=dbadmin
ansible_ssh_private_key_file=~/.ssh/db_rsa
mysql_port=3306
# 嵌套组(父组)
[production:children]
webservers
dbservers
# 所有主机
[all:vars]
ansible_python_interpreter=/usr/bin/python3
YAML 格式
# inventory/hosts.yml
---
all:
children:
webservers:
hosts:
web1:
ansible_host: 192.168.1.10
web2:
ansible_host: 192.168.1.11
vars:
ansible_user: deploy
http_port: 80
dbservers:
hosts:
db1:
ansible_host: 192.168.1.20
db2:
ansible_host: 192.168.1.21
vars:
ansible_user: dbadmin
mysql_port: 3306
production:
children:
webservers:
dbservers:
vars:
ansible_python_interpreter: /usr/bin/python3
主机变量
在 Inventory 中定义
[webservers]
web1 ansible_host=192.168.1.10 http_port=8080
web2 ansible_host=192.168.1.11 http_port=8081
使用 group_vars 和 host_vars
inventory/
├── hosts.yml
├── group_vars/
│ ├── all.yml # 所有主机的变量
│ ├── webservers.yml # webservers 组的变量
│ └── dbservers.yml # dbservers 组的变量
└── host_vars/
├── web1.yml # web1 主机的变量
└── db1.yml # db1 主机的变量
group_vars/webservers.yml
---
http_port: 80
https_port: 443
app_name: myapp
nginx_worker_processes: auto
host_vars/web1.yml
---
http_port: 8080
app_version: 1.0.0
custom_config: true
动态 Inventory
AWS EC2 动态清单
# 安装动态清单插件
pip install boto boto3
# 配置 AWS 凭据
export AWS_ACCESS_KEY_ID=your_key
export AWS_SECRET_ACCESS_KEY=your_secret
export AWS_REGION=us-west-2
ec2.yml 配置
# inventory/ec2.yml
---
plugin: amazon.aws.aws_ec2
regions:
- us-west-2
- us-east-1
filters:
"tag:Environment":
- production
instance-state-name:
- running
keyed_groups:
- key: tags.Role
prefix: role
- key: placement.availability_zone
prefix: az
hostnames:
- private-ip-address
compose:
ansible_host: private_ip_address
使用动态清单
# 测试动态清单
ansible-inventory -i inventory/ec2.yml --list
# 执行 Playbook
ansible-playbook -i inventory/ec2.yml playbook.yml
# 限制主机
ansible-playbook -i inventory/ec2.yml playbook.yml --limit "role_web"
其他动态清单插件
# Azure 动态清单
plugin: azure.azcollection.azure_rm
# Google Cloud 动态清单
plugin: google.cloud.gcp_compute
# Docker 动态清单
plugin: community.docker.docker_containers
连接参数
常用连接参数
[webservers]
web1 ansible_host=192.168.1.10 ansible_port=22 ansible_user=deploy
[dbservers:vars]
ansible_connection=ssh
ansible_ssh_private_key_file=~/.ssh/id_rsa
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
使用 sudo
[webservers:vars]
ansible_become=yes
ansible_become_method=sudo
ansible_become_user=root
ansible_become_pass={{ sudo_password }}
Inventory 管理命令
# 列出所有主机
ansible-inventory --list
# 显示主机图
ansible-inventory --graph
# 列出组
ansible-inventory --list --group webservers
# 测试连通性
ansible all -m ping
# 执行 Ad-hoc 命令
ansible webservers -m shell -a "uptime"
ansible dbservers -m mysql_db -a "name=mydb state=present"
实践案例
多环境 Inventory
# inventory/production.yml
---
all:
children:
production:
children:
prod_webservers:
hosts:
prod-web1:
ansible_host: 10.0.1.10
prod-web2:
ansible_host: 10.0.1.11
prod_dbservers:
hosts:
prod-db1:
ansible_host: 10.0.1.20
vars:
env: production
ansible_user: deploy
staging:
children:
staging_webservers:
hosts:
staging-web1:
ansible_host: 10.0.2.10
staging_dbservers:
hosts:
staging-db1:
ansible_host: 10.0.2.20
vars:
env: staging
ansible_user: deploy
development:
children:
dev_webservers:
hosts:
dev-web1:
ansible_host: 192.168.1.10
dev_dbservers:
hosts:
dev-db1:
ansible_host: 192.168.1.20
vars:
env: development
ansible_user: vagrant
使用 Inventory 执行任务
# 在生产环境执行
ansible-playbook -i inventory/production.yml deploy.yml
# 仅在 staging 环境测试
ansible-playbook -i inventory/staging.yml deploy.yml --limit staging
# 在所有环境检查状态
ansible all -i inventory/ -m ping
常见问题
SSH 连接失败
# 检查 SSH 配置
ansible all -m ping -vvv
# 测试 SSH 连接
ssh -i ~/.ssh/id_rsa deploy@192.168.1.10
# 检查 known_hosts
ssh-keyscan 192.168.1.10 >> ~/.ssh/known_hosts
主机未找到
# 检查 Inventory
ansible-inventory --list
# 检查主机变量
ansible-inventory --host web1
# 检查组
ansible-inventory --list --group webservers
最佳实践
- 使用 YAML 格式的 Inventory
- 使用 group_vars 和 host_vars 组织变量
- 为不同环境维护独立的 Inventory 文件
- 使用动态 Inventory 管理云资源
- 定期测试 Inventory 连通性
总结
Inventory 是 Ansible 的基础,合理组织 Inventory 可以简化管理、提高效率。掌握静态和动态 Inventory 的使用是 Ansible 自动化的关键。