25
2025
06
16:26:23

Ansible配置文件,由浅入深的全方位指南

在自动化运维的广阔天地里,Ansible就是我们披荆斩棘的得力宝剑,而Ansible配置文件无疑是握住这把剑剑柄的关键所在。那它到底是怎么回事呢?下面咱们就来一探究竟。

图片

文件位置和优先级

Ansible支持多级配置文件,优先级从高到低依次为:

  1. 1. 环境变量:通过 ANSIBLE_CONFIG 指定路径。
  2. 2. 当前目录:./ansible.cfg(项目级配置)。
  3. 3. 用户目录:~/.ansible.cfg(用户级配置)。
  4. 4. 系统默认:/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 = True

配置文件长啥样?

先看看Ansible配置文件的骨架。它像一个大仓库,里面划分了好几个不同功能的区域

root@code-server:~# grep -E '^\[' /etc/ansible/ansible.cfg
[defaults]
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
  • • [defaults]区,这里面装着Ansible操作通用的默认设置,就像是操作系统的初始配置,为后续各种操作奠定基础。
  • • [inventory]区也很重要,它专门负责和主机清单打交道,确定哪里去找那些等着被管理的主机。

  • • [privilege_escalation]区又是干啥的呢?它是用来设置特权升级相关选项的。打个比方,就像给特定人员发放“特殊通行卡”,可以让Ansible在执行任务时拥有更多权限。
  • • [paramiko_connection]区和[ssh_connection]区,分别对应着使用不同库(Paramiko库和OpenSSH库)来建立SSH连接的选项,好比是不同的工具开锁,选择合适的“钥匙”才能打开与受管主机通信的“大门” 。
  • • [persistent_connection]区负责配置持久连接选项,要是把和主机的连接看作是打电话,这里就是设置怎么保持“长线通话”不中断 。
  • • [accelerate]区能开启加速模式选项,给Ansible工作起来"踩踩油门",提升运行效率。
  • • [selinux]区针对SELinux相关内容进行设置,保障系统安全策略方面不出问题。
  • • [colors]区趣味性就比较强啦,它负责设置Ansible命令输出的颜色选项,给单调的运维工作增添点色彩。
  • • [diff]区决定是否打印任务前后的差异,这就像是在对比两份文件,明确哪里发生了改变 。

核心配置说明

以下是一个典型 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.log

[privilege_escalation] 模块:权限提升配置

[privilege_escalation]
# 是否启用 sudo/su 提权
become = True
# 提权使用的命令(sudo/su/doas)
become_method = sudo
# 提权后的用户(默认 root)
become_user = root
# 是否询问 sudo 密码
become_ask_pass = False

[ssh_connection] 模块:SSH 连接优化

[ssh_connection]
# 启用 SSH 管道加速(减少连接次数)
pipelining = True
# 超时时间(单位秒)
timeout = 30
# 启用 ControlPersist 长连接
control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r

[persistent_connection] 模块:连接复用

[persistent_connection]
# 连接保持时间(秒)
connect_timeout = 30
# 命令超时时间
command_timeout = 60

[inventory] 模块:库存扩展配置

[inventory]
enable_plugins = host_list,yaml,ini
# 启用动态库存缓存
cache = yes
# 缓存过期时间(秒)
cache_plugin = jsonfile
cache_timeout = 3600

[colors] 模块:输出颜色定制

[colors]
# 自定义输出颜色(如成功、错误状态)
highlight = white
verbose = blue
error = red

高级设置和优化方法

性能优化


  • • 并发与管道:通过 forks 增加并行任务数,启用 pipelining 减少SSH开销。
  • • Fact缓存:使用Redis或 JSON文件缓存facts,避免重复收集:
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts

插件管理

  • • 启用/禁用插件:如禁用 host_key_checking 插件加速测试环境执行。
  • • 自定义模块路径:
[defaults]
library = /usr/share/ansible/custom_modules/

环境变量注入

通过 [defaults] 的 environment 参数向远程主机传递变量:

[defaults]
environment = {"http_proxy": "http://proxy.example.com:8080"}

配置示例

[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 = False

总结

Ansible配置文件真的是Ansible自动化流程中“隐秘而伟大”的存在。只要咱们把配置文件这些弯弯绕绕的内容吃透,就能让整个自动化流程更加丝滑,提升效率又增加系统可靠性。还等什么?赶紧上手搞懂它,让Ansible为你的自动化运维助力腾飞。




推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

本文链接:https://www.hqyman.cn/post/11845.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:
打赏





休息一下~~


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

请先 登录 再评论,若不是会员请先 注册

您的IP地址是: