引言
在Ansible自动化运维体系中,主机清单文件(Inventory)不仅是定义管理目标的基础,更是实现差异化配置的重要载体。通过在清单文件中定义变量,我们可以为特定主机或整个主机组分配独特的配置参数,实现一套Playbook适配多种环境、不同主机的需求。相比其他变量来源,主机清单变量具有定义直观、维护便捷、环境隔离的优势,特别适合需要精细化控制不同主机配置的场景。
1 主机变量(Host Variables)
1.1 定义语法与特点
定义方式:在主机名后直接以空格分隔定义变量,多个变量用空格分隔
hostname var1=value1 var2=value2
特点:
- 为单个主机定义专属变量
- 适用于需要差异化配置的少量主机
- 优先级高于组变量(同名变量时)
1.2 完整示例
[websrvs]
www1.example.com http_port=80 maxRequestsPerChild=808
www2.example.com http_port=8080 maxRequestsPerChild=909
---
- hosts: websrvs
tasks:
- name: Configure web server
copy:
src: httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
vars:
# 根据主机变量动态配置
listen_port: "{{ http_port }}"
max_requests: "{{ maxRequestsPerChild }}"
Listen {{ listen_port }}
MaxRequestsPerChild {{ max_requests }}
1.3 主机变量定义流程
- 在主机清单文件中定义主机组
- 为每个主机指定专属变量
- 在Playbook中通过{{ variable_name }}调用变量
- 根据主机变量实现差异化配置
2 组变量(Group Variables)
2.1 定义语法与特点
定义方式:使用[groupname:vars]语法块定义组变量
[groupname:vars]
var1=value1
var2=value2
特点:
- 为整个组内的所有主机定义共享变量
- 适用于组内主机的公共配置
- 优先级低于主机变量(同名变量时)
2.2 完整示例
[websrvs]
www1.example.com
www2.example.com
[websrvs:vars]
http_port=8080
ntp_server=ntp.example.com
nfs_server=nfs.example.com
---
- hosts: websrvs
tasks:
- name: Configure NTP client
template:
src: ntp.conf.j2
dest: /etc/ntp.conf
vars:
ntp_server: "{{ ntp_server }}"
- name: Mount NFS share
mount:
path: /data
src: "{{ nfs_server }}:/data"
fstype: nfs
state: mounted
2.3 组变量优先级示意图
优先级说明:
- 当主机变量和组变量同名时,主机变量优先级更高
- 组变量适用于组内所有主机的公共配置
- 主机变量适用于特定主机的差异化配置
3 变量优先级体系
3.1 完整优先级顺序
从高到低优先级:
- 命令行变量(-e参数)
- 主机变量(Host Variables)
- 组变量(Group Variables)
- Playbook文件内变量(vars)
- Role变量
- Facts变量
3.2 优先级验证示例
[websrvs]
web1.example.com http_port=80
web2.example.com
[websrvs:vars]
http_port=8080
---
- hosts: websrvs
vars:
http_port: 9090
tasks:
- name: Show port
debug: msg="Port: {{ http_port }}"
结果分析:
- web1.example.com使用主机变量(80)
- web2.example.com使用组变量(8080)
- 命令行变量(-e)可以覆盖所有配置
4 案例:混合变量应用
4.1 项目结构
ansible-project/
├── inventory/
│ ├── production
│ ├── staging
│ └── development
├── group_vars/
│ ├── all.yml
│ ├── webservers.yml
│ └── dbservers.yml
├── host_vars/
│ ├── web1.example.com.yml
│ └── db1.example.com.yml
└── playbooks/
├── deploy_web.yml
└── deploy_db.yml
4.2 主机清单文件
- 生产环境清单(inventory/production):
[webservers]
web1.prod.example.com http_port=80 max_connections=1000
web2.prod.example.com http_port=80 max_connections=2000
web3.prod.example.com http_port=8080 max_connections=1500
[dbservers]
db1.prod.example.com db_port=3306 db_memory=8192
db2.prod.example.com db_port=3306 db_memory=16384
[webservers:vars]
app_name="production_app"
cache_enabled=true
ssl_enabled=true
[dbservers:vars]
app_name="production_db"
backup_enabled=true
4.3 组变量文件
- 公共变量(group_vars/all.yml):
---
# 公共配置
ansible_user: "deploy"
ansible_ssh_private_key_file: "~/.ansible/keys/deploy_key"
backup_dir: "/backup"
log_level: "INFO"
- Web服务器变量(group_vars/webservers.yml):
---
# Web服务器特定配置
nginx_version: "1.18.0"
php_version: "7.4"
max_workers: 4
4.4 主机变量文件
- Web1服务器变量(host_vars/web1.example.com.yml):
---
# Web1专属配置
http_port: 80
max_connections: 1000
ssl_cert: "/etc/ssl/certs/web1.crt"
ssl_key: "/etc/ssl/private/web1.key"
4.5 Playbook应用示例
- Web部署Playbook(playbooks/deploy_web.yml):
---
- name: Deploy Web Servers
hosts: webservers
vars_files:
- ../group_vars/all.yml
- ../group_vars/webservers.yml
tasks:
- name: Install web packages
yum:
name: "{{ item }}"
state=present
loop:
- "nginx-{{ nginx_version }}"
- "php-{{ php_version }}"
- "php-fpm"
- name: Configure nginx
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
vars:
listen_port: "{{ http_port }}"
max_conn: "{{ max_connections }}"
ssl_cert_path: "{{ ssl_cert | default('/etc/ssl/certs/default.crt') }}"
ssl_key_path: "{{ ssl_key | default('/etc/ssl/private/default.key') }}"
- name: Start services
service:
name: "{{ item }}"
state=started
enabled=yes
loop:
- nginx
- php-fpm
4.6 执行流程
5 总结
主机清单变量是Ansible实现精细化配置管理的关键特性,掌握其使用方法能够显著提升自动化运维的灵活性和可维护性。在实际应用中,应建立规范的变量管理体系,结合业务需求合理设计变量结构,同时注重变量安全和文档维护。