inventory-manager
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseInventory Manager
Inventory 管理器
Quick Start
快速开始
Organize Ansible inventory with proper host groups, variables, and dynamic inventory sources.
通过合理的主机组、变量和动态Inventory源来整理Ansible Inventory。
Instructions
操作步骤
Step 1: Create static inventory
步骤1:创建静态Inventory
INI format:
ini
undefinedINI格式:
ini
undefinedinventory/production
inventory/production
[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
[databases]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21
[loadbalancers]
lb1 ansible_host=192.168.1.30
[production:children]
webservers
databases
loadbalancers
[production:vars]
ansible_user=deploy
ansible_python_interpreter=/usr/bin/python3
environment=production
**YAML format:**
```yaml[webservers]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
[databases]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21
[loadbalancers]
lb1 ansible_host=192.168.1.30
[production:children]
webservers
databases
loadbalancers
[production:vars]
ansible_user=deploy
ansible_python_interpreter=/usr/bin/python3
environment=production
**YAML格式:**
```yamlinventory/production.yml
inventory/production.yml
all:
children:
production:
children:
webservers:
hosts:
web1:
ansible_host: 192.168.1.10
web2:
ansible_host: 192.168.1.11
databases:
hosts:
db1:
ansible_host: 192.168.1.20
db2:
ansible_host: 192.168.1.21
loadbalancers:
hosts:
lb1:
ansible_host: 192.168.1.30
vars:
ansible_user: deploy
environment: production
undefinedall:
children:
production:
children:
webservers:
hosts:
web1:
ansible_host: 192.168.1.10
web2:
ansible_host: 192.168.1.11
databases:
hosts:
db1:
ansible_host: 192.168.1.20
db2:
ansible_host: 192.168.1.21
loadbalancers:
hosts:
lb1:
ansible_host: 192.168.1.30
vars:
ansible_user: deploy
environment: production
undefinedStep 2: Organize group variables
步骤2:整理组变量
Directory structure:
inventory/
├── production
├── staging
├── group_vars/
│ ├── all.yml
│ ├── webservers.yml
│ ├── databases.yml
│ └── production.yml
└── host_vars/
├── web1.yml
└── db1.ymlgroup_vars/all.yml:
yaml
---目录结构:
inventory/
├── production
├── staging
├── group_vars/
│ ├── all.yml
│ ├── webservers.yml
│ ├── databases.yml
│ └── production.yml
└── host_vars/
├── web1.yml
└── db1.ymlgroup_vars/all.yml:
yaml
---Variables for all hosts
所有主机的通用变量
ansible_python_interpreter: /usr/bin/python3
ntp_servers:
- 0.pool.ntp.org
- 1.pool.ntp.org
**group_vars/webservers.yml:**
```yaml
---ansible_python_interpreter: /usr/bin/python3
ntp_servers:
- 0.pool.ntp.org
- 1.pool.ntp.org
**group_vars/webservers.yml:**
```yaml
---Variables for webservers group
Webservers组的专属变量
nginx_port: 80
nginx_worker_processes: 4
app_directory: /var/www/app
**group_vars/production.yml:**
```yaml
---nginx_port: 80
nginx_worker_processes: 4
app_directory: /var/www/app
**group_vars/production.yml:**
```yaml
---Variables for production environment
生产环境专属变量
environment: production
backup_enabled: true
monitoring_enabled: true
undefinedenvironment: production
backup_enabled: true
monitoring_enabled: true
undefinedStep 3: Configure host-specific variables
步骤3:配置主机专属变量
host_vars/web1.yml:
yaml
---
nginx_worker_connections: 1024
server_id: 1host_vars/web1.yml:
yaml
---Step 4: Use inventory in playbooks
Web1主机的专属配置
bash
undefinednginx_worker_connections: 1024
server_id: 1
undefinedRun playbook with specific inventory
步骤4:在Playbook中使用Inventory
ansible-playbook -i inventory/production site.yml
bash
undefinedTarget specific group
使用指定Inventory运行Playbook
ansible-playbook -i inventory/production site.yml --limit webservers
ansible-playbook -i inventory/production site.yml
Target specific host
目标指定主机组
ansible-playbook -i inventory/production site.yml --limit web1
undefinedansible-playbook -i inventory/production site.yml --limit webservers
Dynamic Inventory
目标指定主机
AWS EC2 Plugin
—
inventory/aws_ec2.yml:
yaml
---
plugin: amazon.aws.aws_ec2
regions:
- us-east-1
- us-west-2
filters:
tag:Environment: production
instance-state-name: running
keyed_groups:
- key: tags.Role
prefix: role
- key: tags.Environment
prefix: env
- key: placement.availability_zone
prefix: az
hostnames:
- tag:Name
- private-ip-address
compose:
ansible_host: private_ip_addressansible-playbook -i inventory/production site.yml --limit web1
undefinedAzure Plugin
动态Inventory
—
AWS EC2插件
inventory/azure_rm.yml:
yaml
---
plugin: azure.azcollection.azure_rm
include_vm_resource_groups:
- production-rg
keyed_groups:
- key: tags.role
prefix: role
- key: tags.environment
prefix: env
conditional_groups:
webservers: "'web' in tags.role"
databases: "'db' in tags.role"inventory/aws_ec2.yml:
yaml
---
plugin: amazon.aws.aws_ec2
regions:
- us-east-1
- us-west-2
filters:
tag:Environment: production
instance-state-name: running
keyed_groups:
- key: tags.Role
prefix: role
- key: tags.Environment
prefix: env
- key: placement.availability_zone
prefix: az
hostnames:
- tag:Name
- private-ip-address
compose:
ansible_host: private_ip_addressCustom Script
Azure插件
inventory/custom.py:
python
#!/usr/bin/env python3
import json
inventory = {
"webservers": {
"hosts": ["web1", "web2"],
"vars": {
"nginx_port": 80
}
},
"databases": {
"hosts": ["db1", "db2"]
},
"_meta": {
"hostvars": {
"web1": {"ansible_host": "192.168.1.10"},
"web2": {"ansible_host": "192.168.1.11"},
"db1": {"ansible_host": "192.168.1.20"},
"db2": {"ansible_host": "192.168.1.21"}
}
}
}
print(json.dumps(inventory))inventory/azure_rm.yml:
yaml
---
plugin: azure.azcollection.azure_rm
include_vm_resource_groups:
- production-rg
keyed_groups:
- key: tags.role
prefix: role
- key: tags.environment
prefix: env
conditional_groups:
webservers: "'web' in tags.role"
databases: "'db' in tags.role"Inventory Patterns
自定义脚本
All hosts:
bash
ansible all -i inventory/production -m pingSpecific group:
bash
ansible webservers -i inventory/production -m pingMultiple groups:
bash
ansible 'webservers:databases' -i inventory/production -m pingExclude group:
bash
ansible 'all:!databases' -i inventory/production -m pingIntersection:
bash
ansible 'webservers:&production' -i inventory/production -m pingRegex:
bash
ansible '~web.*' -i inventory/production -m pinginventory/custom.py:
python
#!/usr/bin/env python3
import json
inventory = {
"webservers": {
"hosts": ["web1", "web2"],
"vars": {
"nginx_port": 80
}
},
"databases": {
"hosts": ["db1", "db2"]
},
"_meta": {
"hostvars": {
"web1": {"ansible_host": "192.168.1.10"},
"web2": {"ansible_host": "192.168.1.11"},
"db1": {"ansible_host": "192.168.1.20"},
"db2": {"ansible_host": "192.168.1.21"}
}
}
}
print(json.dumps(inventory))Multi-Environment Setup
Inventory匹配模式
inventory/
├── production/
│ ├── hosts
│ ├── group_vars/
│ │ ├── all.yml
│ │ └── webservers.yml
│ └── host_vars/
├── staging/
│ ├── hosts
│ ├── group_vars/
│ │ ├── all.yml
│ │ └── webservers.yml
│ └── host_vars/
└── development/
├── hosts
└── group_vars/
└── all.ymlUsage:
bash
undefined所有主机:
bash
ansible all -i inventory/production -m ping指定主机组:
bash
ansible webservers -i inventory/production -m ping多个主机组:
bash
ansible 'webservers:databases' -i inventory/production -m ping排除主机组:
bash
ansible 'all:!databases' -i inventory/production -m ping交集主机组:
bash
ansible 'webservers:&production' -i inventory/production -m ping正则匹配:
bash
ansible '~web.*' -i inventory/production -m pingProduction
多环境部署配置
ansible-playbook -i inventory/production site.yml
inventory/
├── production/
│ ├── hosts
│ ├── group_vars/
│ │ ├── all.yml
│ │ └── webservers.yml
│ └── host_vars/
├── staging/
│ ├── hosts
│ ├── group_vars/
│ │ ├── all.yml
│ │ └── webservers.yml
│ └── host_vars/
└── development/
├── hosts
└── group_vars/
└── all.yml使用方式:
bash
undefinedStaging
生产环境
ansible-playbook -i inventory/staging site.yml
ansible-playbook -i inventory/production site.yml
Development
预发布环境
ansible-playbook -i inventory/development site.yml
undefinedansible-playbook -i inventory/staging site.yml
Best Practices
开发环境
- Organize inventory by environment
- Use group_vars for shared configuration
- Use host_vars for host-specific settings
- Document inventory structure
- Use dynamic inventory for cloud resources
- Keep sensitive data in Ansible Vault
- Use meaningful group names
- Implement proper variable precedence
ansible-playbook -i inventory/development site.yml
undefined—
最佳实践
—
- 按环境划分Inventory结构
- 使用group_vars存储共享配置
- 使用host_vars存储主机专属配置
- 为Inventory结构添加文档说明
- 针对云资源使用动态Inventory
- 敏感数据存储在Ansible Vault中
- 使用有意义的主机组名称
- 合理设置变量优先级