Ansible 配置文件,由浅入深的全方位指南
在自动化运维的广阔天地里,Ansible就是我们披荆斩棘的得力宝剑,而Ansible配置文件无疑是握住这把剑剑柄的关键所在。那它到底是怎么回事呢?下面咱们就来一探究竟。
Ansible支持多级配置文件,优先级从高到低依次为:
环境变量:通过 ANSIBLE_CONFIG 指定路径。当前目录:./ansible.cfg(项目级配置)。用户目录:~/.ansible.cfg(用户级配置)。系统默认:/etc/ansible/ansible.cfg(全局配置)。可以通过以下命令查看生效的ansible.cfg文件。
复制
root@code-server:~# ansible --version
ansible [core 2.17.10]
config file = /etc/ansible/ansible.cfg
configured module search path = [/root/.ansible/plugins/modules, /usr/share/ansible/plugins/modules]
ansible python module location = /root/.local/pipx/venvs/ansible-core/lib/python3.10/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /root/.local/bin/ansible
python version = 3.10.12 (main, Feb 4 2025, 14:57:36) [GCC 11.4.0] (/root/.local/pipx/venvs/ansible-core/bin/python)
jinja version = 3.1.6
libyaml = True1.2.3.4.5.6.7.8.9.10.
先看看Ansible配置文件的骨架。它像一个大仓库,里面划分了好几个不同功能的区域。
复制
root@code-server:~# grep -E ^\[ /etc/ansible/ansible.cfg
[defaults]
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]1.2.3.4.5.6.7.8.9.10.11.
以下是一个典型 ansible.cfg 的结构示例及关键参数解析:
[defaults] 模块:基础行为控制
复制
[defaults]
# 定义库存文件路径(支持多个文件或目录)
inventory = ./inventory/
# 默认远程用户(可被 playbook 覆盖)
remote_user = admin
# 是否收集主机 facts 信息
gathering = smart
# 是否检查主机 SSH 密钥
host_key_checking = False
# 并发进程数(提升执行速度)
forks = 50
# 日志记录路径
log_path = /var/log/ansible.log1.2.3.4.5.6.7.8.9.10.11.12.13.
[privilege_escalation] 模块:权限提升配置
复制
[privilege_escalation]
# 是否启用 sudo/su 提权
become = True
# 提权使用的命令(sudo/su/doas)
become_method = sudo
# 提权后的用户(默认 root)
become_user = root
# 是否询问 sudo 密码
become_ask_pass = False1.2.3.4.5.6.7.8.9.
[ssh_connection] 模块:SSH 连接优化
复制
[ssh_connection]
# 启用 SSH 管道加速(减少连接次数)
pipelining = True
# 超时时间(单位秒)
timeout = 30
# 启用 ControlPersist 长连接
control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r1.2.3.4.5.6.7.
[persistent_connection] 模块:连接复用
复制
[persistent_connection]
# 连接保持时间(秒)
connect_timeout = 30
# 命令超时时间
command_timeout = 601.2.3.4.5.
[inventory] 模块:库存扩展配置
复制
[inventory]
enable_plugins = host_list,yaml,ini
# 启用动态库存缓存
cache = yes
# 缓存过期时间(秒)
cache_plugin = jsonfile
cache_timeout = 36001.2.3.4.5.6.7.
[colors] 模块:输出颜色定制
复制
[colors]
# 自定义输出颜色(如成功、错误状态)
highlight = white
verbose = blue
error = red1.2.3.4.5.
(1) 性能优化
并发与管道:通过 forks 增加并行任务数,启用 pipelining 减少SSH开销。Fact缓存:使用Redis或 JSON文件缓存facts,避免重复收集:复制
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts1.2.3.
(2) 插件管理:
启用/禁用插件:如禁用 host_key_checking 插件加速测试环境执行。
自定义模块路径:
复制
[defaults]
library = /usr/share/ansible/custom_modules/1.2.
(3) 环境变量注入
通过 [defaults] 的 environment 参数向远程主机传递变量:
复制
[defaults]
environment = {"http_proxy": "http://proxy.example.com:8080"}1.2.
复制
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
inventory = ./inventory
fork = 20
ask_pass = False
remote_user = root
log_path = /var/log/ansible.log
host_key_checking = False
ansible_python_interpreter = /usr/bin/python3.9
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.
Ansible配置文件真的是Ansible自动化流程中“隐秘而伟大”的存在。只要咱们把配置文件这些弯弯绕绕的内容吃透,就能让整个自动化流程更加丝滑,提升效率又增加系统可靠性。还等什么?赶紧上手搞懂它,让Ansible为你的自动化运维助力腾飞。
阅读剩余
THE END