inventory-manager

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Inventory 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
undefined
INI格式:
ini
undefined

inventory/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格式:**
```yaml

inventory/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
undefined
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
undefined

Step 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.yml
group_vars/all.yml:
yaml
---
目录结构:
inventory/
├── production
├── staging
├── group_vars/
│   ├── all.yml
│   ├── webservers.yml
│   ├── databases.yml
│   └── production.yml
└── host_vars/
    ├── web1.yml
    └── db1.yml
group_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
undefined
environment: production backup_enabled: true monitoring_enabled: true
undefined

Step 3: Configure host-specific variables

步骤3:配置主机专属变量

host_vars/web1.yml:
yaml
---
nginx_worker_connections: 1024
server_id: 1
host_vars/web1.yml:
yaml
---

Step 4: Use inventory in playbooks

Web1主机的专属配置

bash
undefined
nginx_worker_connections: 1024 server_id: 1
undefined

Run playbook with specific inventory

步骤4:在Playbook中使用Inventory

ansible-playbook -i inventory/production site.yml
bash
undefined

Target 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
undefined
ansible-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_address
ansible-playbook -i inventory/production site.yml --limit web1
undefined

Azure 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_address

Custom 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 ping
Specific group:
bash
ansible webservers -i inventory/production -m ping
Multiple groups:
bash
ansible 'webservers:databases' -i inventory/production -m ping
Exclude group:
bash
ansible 'all:!databases' -i inventory/production -m ping
Intersection:
bash
ansible 'webservers:&production' -i inventory/production -m ping
Regex:
bash
ansible '~web.*' -i inventory/production -m ping
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))

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.yml
Usage:
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 ping

Production

多环境部署配置

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
undefined

Staging

生产环境

ansible-playbook -i inventory/staging site.yml
ansible-playbook -i inventory/production site.yml

Development

预发布环境

ansible-playbook -i inventory/development site.yml
undefined
ansible-playbook -i inventory/staging site.yml

Best Practices

开发环境

  1. Organize inventory by environment
  2. Use group_vars for shared configuration
  3. Use host_vars for host-specific settings
  4. Document inventory structure
  5. Use dynamic inventory for cloud resources
  6. Keep sensitive data in Ansible Vault
  7. Use meaningful group names
  8. Implement proper variable precedence
ansible-playbook -i inventory/development site.yml
undefined

最佳实践

  1. 按环境划分Inventory结构
  2. 使用group_vars存储共享配置
  3. 使用host_vars存储主机专属配置
  4. 为Inventory结构添加文档说明
  5. 针对云资源使用动态Inventory
  6. 敏感数据存储在Ansible Vault中
  7. 使用有意义的主机组名称
  8. 合理设置变量优先级